First Java Program
Getting Started with Java: Here is a simple Java program that just displays a custom message on screen say “Hello this is my first Java Program”.
I first write the program and then discuss the various parts of it.
//first java program
public static void main(String args)
System.out.println("This is my first java program");
first save the program with the name "FirstJava.java".If you wrote the code using notepad then save the filename is double quotes otherwise the program will be saved as FirstJava.java.txt which will be a txt file not a .java file.
Then go to command prompt by typing cmd in the Run dialog box or in the search bar.
Now at the command prompt:
Change the directory using cd command to the directory where you saved the file FirstJava.java
then type javac FirstJava.java
if the code contains errors the list of errors will be displayed, otherwise the command prompt will appear.
It means that the code has been compiled and it contains no errors.
You can also check that a file FirstJava.class has been created in the directory where you saved your program. This is because when you compile the code the Java compiler converts the source code into intermediate "Byte Code" which is saved in a .class file.
And this Byte code can be run on any platform thus facilitating the Platform In-dependency feature of Java.
Running the code:
To Run the code:
type java FirstJava at the command from the same directory where you have saved the file.
The output will appear as:
This is my first java program
Naming Conventions to Follow in Java
For Naming Methods:
Follow Camel Rule : The rule is that function name must begin with a small letter and if the function name consists of two or more words, then the first letter of every other word must begin with a Capital letter.
Examples of some method names:
For Naming Classes:
Follow Pascal Rule : The rule is that a class name must begin with a capital letter and if the name of the class contains two or more than two words then first letter of every word must be capital.
Examples of some class names:
For Naming Variables:
Variable naming follows the same rule as methods naming.
But name for a constant variable must be in all capitals.
Posted by Harish Sharma