Blog

Understanding The First Java Program

Java / Java Tutorials

Understanding The First Java Program

[vc_row][vc_column][vc_column_text]

Let’s start with Understanding The First Java Program to Print Hello world in Java.
A basic Hello world program in Java, that displays “Hello, World!”. It’s mostly used to illustrate the syntax of the language.

class Test{
	public static void main(String args[])
	{
		System.out.println("Hello World");
	}
}

Output

Hello World!

Learn how to Write, Compile & Run a Java Program by learning this Tutorial :→ Developing Java Programs
 

Understanding The First Java Program

First of all we must remember that java is a highly case sensitive
language.

It means that we have to be very careful about uppercase and lowercase letters while typing the code.
In our very first java program three letters are compulsorily in uppercase and they are
“T” of Test ,
“S” of String and
“S” of System.
This is because in java class names begin with upper case

→ The first statement of our code is:

class Test
Since java is an Object Oriented Language and it strictly supports Encapsulation so every java program must always contain at-least one class and whatever we write must appear within the opening and closing braces of the class

→ The second statement of our code is:

public static void main(String [ ] args)
In java also(like C/C++) the entry point of execution of our program is the main( ) method which is called by the JVM.
The words shown in pink are keywords and each has a different meaning and purpose for the method main( ).
Lets understand each of them in detail[/vc_column_text][vc_custom_heading text=”Why main( ) is public ?” font_container=”tag:h2|text_align:left|color:%23606060″ google_fonts=”font_family:Montserrat%3Aregular%2C700|font_style:700%20bold%20regular%3A700%3Anormal”][vc_column_text]→ “public” is an access modifier and in Object Oriented Programming any method or variable which is declared public can be accessible from outside of that class.
→ Since main( ) method is public in Java, JVM can easily access and execute it.[/vc_column_text][vc_custom_heading text=”Why main( ) is static ?” font_container=”tag:h2|text_align:left|color:%23606060″ google_fonts=”font_family:Montserrat%3Aregular%2C700|font_style:700%20bold%20regular%3A700%3Anormal”][vc_column_text]→ Every class can have two kinds of methods , nonstatic and static.
→ A method which is nonstatic can only be called using object of that class , while a static method can be called without any object , simply using class name.
→ When the JVM makes are call to the main( ) method there is not object existing for the class being called therefore it has to have static method to allow invocation from class.
[/vc_column_text][vc_custom_heading text=”Why main( ) has return type void ?” font_container=”tag:h2|text_align:left|color:%23606060″ google_fonts=”font_family:Montserrat%3Aregular%2C700|font_style:700%20bold%20regular%3A700%3Anormal”][vc_column_text]→ The keyword void is called return type which indicates that no value will be returned by the method to it’s caller.
→ Since main() method in Java is not supposed to return any value to the JVM, its made void which simply means main() is not returning anything.[/vc_column_text][vc_custom_heading text=”Can we change/remove the keywords used with main () ?” font_container=”tag:h2|text_align:left|color:%23606060″ google_fonts=”font_family:Montserrat%3Aregular%2C700|font_style:700%20bold%20regular%3A700%3Anormal”][vc_column_text]→ No , not at all.
→ This is because main( ) is called by JVM and to allow JVM to successfully call main( ) these keywords are important.
→ If we forget to write these keywords then although the code will compile but will fail to run.
All we can do is change the order of public and static but we can’t drop them[/vc_column_text][vc_custom_heading text=”What is String `{` `}` args ?” font_container=”tag:h2|text_align:left|color:%23606060″ google_fonts=”font_family:Montserrat%3Aregular%2C700|font_style:700%20bold%20regular%3A700%3Anormal”][vc_column_text]→ String is a predefined class in java
→ So the statement String [ ] args is declaring args to be an array of Strings.
→ It is called command line argument and we will discuss it later.
→ For now, just remember that the statement String[ ] args has to be present with main( ) otherwise code will not run[/vc_column_text][vc_custom_heading text=”Can we change/drop String `{` `}` args ?” font_container=”tag:h2|text_align:left|color:%23606060″ google_fonts=”font_family:Montserrat%3Aregular%2C700|font_style:700%20bold%20regular%3A700%3Anormal”][vc_column_text]→ No , just like keywords used with main are compulsorily , similarly String [ ] args is also compulsory.
→ All we can do is change the name from args to something else.
→ Also we can interchange the array name and [ ] .
For example: String args[ ], String [ ] args, String [ ]str,String str[ ] all are valid[/vc_column_text][vc_custom_heading text=”Understanding System.out.println( )” font_container=”tag:h2|text_align:left|color:%23606060″ google_fonts=”font_family:Montserrat%3Aregular%2C700|font_style:700%20bold%20regular%3A700%3Anormal”][vc_column_text]→ Now let’s understand code in the body of the main( ) method, which will print a message on the console.
Syntax: System.out.println(“ Hello World ”);
System is a predefined class .
out is an object reference (not object).
println( ) is a method.
→ Together all three are used for displaying text on console.
→ We will discuss this part in detail once we have covered basics of java[/vc_column_text][/vc_column][/vc_row]

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare