Java


       AboutMe        MyCurrentLocation        Blog      GuestBook         Story of a Techie cum fighter in the Real World      HOME 

<!-- ==========Java Environment Setup ============---->


Install Java2 SDK
Make sure you change the installation path from c://Program Files/Java to C://Java
Once this is done set the Environment variables.
If you don't know what are Environment variable Go 2 Google and see
There are two types of Variables you will see : User Variables and System Variables
User variables are meant only for the current user whereas System variables are meant for all the variables using the machine

Go to path and add C:\\<JAVA_INSTALLATION_DIRECTORY>\bin
Make a new variable JAVA_HOME and set its value to c:\<C://<JAVA_INSTALLATION_DIRECTORY>
Got to CLASSPATH variable and add C:\\<JAVA_INSTALLATION_DIRECTORY>\lib\tools.jar

Now once all this is done go to the command line and type java -version ..
You should see  

Now that you have set everything to be a Great DEVELOPER just note down two more things.
There is a directory caled demo in <JAVA_INSTALLATION_DIRECTORY>.
"C:\<JAVA_INSTALLATION_DIRECTORY>Java\jdk1.5.0_11\demo"
Just Open it and see through the amazing examples.
There is a file called index.html in each corresponding which you can open in the Browser and navigate seemlessly.
Make a shortcut to to 2 your desktop and Enjoy the Learning Ahead.

Note : This seems like stupid mentioning here but just have a look at range of primitive types.I gave a stupid test for a MNC today where i missed out by a whisker of 1 mark .Hopefully this would never happen for you.

Byte 1 byte  –128 to 127
Short 2 bytes  –32,768 to 32,767
Int 4 bytes  –2,147,483,648 to 2,147,483, 647 (just over 2 billion)
Long 8 bytes –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes
double 8 bytes

Dont remember these Bloody numbers : just remember the corresponding bytes for each and that 1 Byte = 8bits and (2 to the power n-1) formula.
So say for byte the range will be 1 byte * 8 = 8 and the formula gives (2 to the power 8 - 1) = 2^7 = 128

Also in java the size of all numeric types is platform independent   
In case the precision of of integer and  floating types is still not sufficient for you JAVA provides

BigDecimal and BigInteger classes which come to your rescue.Just remember two things 

1. To convert a integer value 1 into a BigInteger you ust have to say

BigInteger - = BigInteger.valueOf(1);

2. You do not directly use operators sunch as + ,- ,* bla bla.You will have to use the add,subtract ,multiply and divide methods in these classes. For e.g

i = i. .multiply(BigInteger.valueOf(n - i + 1)) .divide(BigInteger.valueOf(i));

Enough of this number stuuff now.

Now let's pay with some java code

Code Illustration 1:
The following code illustrates a java program which picks up a picture file (.jpg) from My Pictures folder of ur Desktop and prints it on the screen :

 ImageViewer.java


Code flow and Some APIs Used :
We make a ImageViewerFrame Object and do all operations on it in its Constructor.
Steps:
Set the Title and size of the frame by the methods setTitle(String) and setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT)
We make a JLabel and add it to the frame object by calling the method getContentPane.add(label).
It is in this label where image will be displayed later.
We then create a JFileChooser which provides a simple mechanism for the user to choose a file.
Further we set the directory which will be displayed by default.This is done by the setCurrent Directory method.In my code i have hardcoded it as     chooser.setCurrentDirectory(new File("C:\\Documents and Settings\\Palu\\My Documents\\My Pictures"));
After this create the menubar object by the API JMenubar and use the method setJMenuBar(JMenuBar object).
Once menubar is created we now make the menu by JMenu API.It's usage is pretty simple     JMenu menu = new JMenu("File");
After creating th menu we add it to the menubar component by saying menuBar.add(menu).
Once menu is created we have reached the final stage of creating the final menu item which is done by the API JMenuItem.
Usage is again piece of cake as always in java     JMenuItem openItem = new JMenuItem("Open");
Once you are done with creating this component add it to the menu by saying menu.add(openItem).

Now you are all set to handlthe action event.Just say
<JMenuItem Object>.addActionListener(new ActionListener {
    public void actionPerformed(ActionEvent event){
        //YOUR LOGIC GOES HERE
    }
});

This finishes off your code.Now execute it and enjoy the Output.


Scanner class :
If you want to read data from the command line then Scanner class is meant for you.
It is present in  java.util package adn there is not ,much 2 learn it it except the nextLine(),nextInt() etc methods:
Just see this basic example and move on

Also look for the usage of JOptionValue API in the example which pops up a modal box.
 
ScannerDemo.java

<!-----------------For C++ Developers only ------------------>


In C++ you can redefine a variable in a nested block.
However you cant do the same in java. for instance u can't say

public static void main(String[] args){
    int i=10;
    {
        int i =11; //This will result in a compile time error in java
    }
}
 

In C++ there is no cosmic class.However in JAVA Object is the cosmic superclass 

 

Mind Boggling Program :--
I think it's better to memorize this.
No explanations from my side here
if you pick k numbers out of n there will be n * n-1 * n-2 *----(n - k + 1)/1*2*3*---*k possible outcomes.
The PossibleOutcomes.java Program does this in java after asking for the values of n and k.Have a look

PossibleOutcomes.java
 


 
What are the differences between checked and unchecked exceptions?  

Answer
A checked exception is any subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.

Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream's read() method

Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked.

With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method.

Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be, as they tend to be unrecoverable

 

You can use a try with catch but for that you need to have a try with finally

class Test11 {
    public static void main(String[] args){
        new Test11().testMethod();
        //The newly created Object calls the testmethod() in withch we have a rty without catch
        //try without catch block is possible only if you have finally with it
    }
    void testMethod(){
        try {
            System.out.println("java");
            return;
        }finally{
            System.exit(0);
            System.out.println("Tiger");
        }
    }
}

This code executes fine and prints java on the command line

<!--=====================To be Published================================ -->

Usage of MesageFormat class

Usage of Scanner class

Usage of Pattern class