First Program in Java
Writing code
You will need to write your Java programs using a text editoror we can use Notepad for this.
When you type the examples that follow you must make sure that you use capital and small letters in the right places because Java is case sensitive.
Writing code
You will need to write your Java programs using a text editoror we can use Notepad for this.
When you type the examples that follow you must make sure that you use capital and small letters in the right places because Java is case sensitive.
We must create the main method which is the section that a program starts.
class abc
{
public static void main(String p[] )
{
System.out.print("my first java program");
}
}
Make sure that you use a capital S in System because it is the name of a class. println is a method that prints the words that you put between the brackets after it on the screen. When you work with letters like in Hello World you must always put them between quotes. The semi-colon is used to show that it is the end of your line of code.
Compiling the Code
What we have just finished typing is called the source code.we have to save this source code file with the extension .java such as ' abc.java'.
Note: The file name must always be the same as the class name.
now we have to compile this code,for this open cmd(command prompt) and enterthis
javac abc.java

If you did everything correct then you will see no errors message and your program will be compiled.
Running the program
Once your program has been compiled you will get a file called abc.class. This is not like normal programs that you just type the name to run but it is actually a file containing the Java bytecode that is run by the Java interpreter. To run your program with the Java interpeter use the following command:
java abc

Do not add .class on to the end of Hello. You will now see the following output on the screen:
my first java program
Congratulations! You have just made your first Java program.
No comments:
Post a Comment