hello world…

//code for hello.java

class hello

{

public static void main(String args[])

{

System.out.println(”hello world”);

}

}

This is a simple hello world program. The most imp thing is that for every .java file there has to be atleast one class and the a class with the exactly same name as the .java file name. Like in this example the file name is hello.java and the name of the class is also hello ( please remember that even the case of the filename and the class name should match as java is a case sensitive language).

Next is the main function which is also imp as C/C++ but unlike C/C++ its in the main class (hello in this case).since main has to be accesed from outside when the program has to be executed so it has to be public ( which has same meaning as in C/C++ classes) and static so that its initialised only once, and void if it returns nothing.

The control flow of the program starts from the main function and goes line by line in sequence. “System.out.println” is a predefined function for console output. System.out.print is also a similar predefined function the difference is that println returns a newline at the end of the thing to be printed and print just prints the thing to be printed.

example :-

System.out.println(”1″);System.out.println(”2″);

this will print :

1

2

where as –

System.out.print(”1″);System.out.print(”2″);

will print :         1 2

Posted by: rishabh_kalra | Comments (0)
First things first…

To start java programming you need to have a java compiler/interpreter.

In linux GCC has a java frount end for compiling java programs namely “gjc” which unfortnately doesnt work on my openSUSE 11.0 (maybe i m missing something) so u need to do 1 thing, open “yast” and open “software management” and then choose “patterns” as “filter” and then under the “development” heading check “java development” and a command will be added to your linux system –> “javac” <– which is used to compile a java program.

And if you dont want to do all this you can simply go to the sun’s site and download JDK (java development kit) which is available for downlaod for every platform.The only thing is that is you download it u will have to update it manually and if you adapt the above method for openSUSE u will get updates automatically through the update manager.

Now that you have obtained the basic requirements now commands “javac” and “java” will be added.

For linux find these commands in the terminal and for windows open the command prompt.

javac command is used to compile a java program that has .java extention and the code is converted to an intermediate java code which is coppied to a new file with same name and having .class extenstion now use java command to run the program but just feeding the filename (without any extension) to the java command, this command will automatically find the .class file in the current working directly and the program is executed.

example –>

$> javac Example.java

$> java Example

this is a test program.

$> _

Posted by: rishabh_kalra | Comment (1)