728x90 AdSpace

Monday, 2 October 2017

First Java Program

In this tutorial we are going to see about details of "First Java Program. To read how to create and run First Java Application in Eclipse

HelloWorld.java
package com.javatutorialcorner.java;

public class HelloWorld {

 public static void main(String[] args) {
  System.out.println("Welcome to Java Tutorial Corner");

 }

}

package : package keyword used to declare path of java class from src folder.

class : class keyword is used to declare classes in Java. First letter of class name must be start with caps. First letter of each word should be in caps. Example : HelloWorld.java

public : public is an access specifier. Public means this function is visible to all.

static : static is again a keyword used to make a function static. To execute a static function you do not have to create an Object of the class. The main() method here is called by JVM, without creating any object for class.

void : It is the return type, meaning this function will not return anything.

main : main() method is the most important method in a standalone Java program. This is the method which is executed, hence all the logic must be inside the main() method.

String[] args : This represents an array whose type is String and name is args. This String[] used to pass argument from command line.

System.out.println : This is used to print anything on the console like printf in C language.

Compile & Run the Java Program :

If you are using IDE, then you can execute using IDE GUI. Here we will see how to compile & execute Java program manually.

Step 1: Navigate to path where java class created in command prompt.  

Step 2: Type javac HelloWorld.java and press Return(Enter KEY) to compile your code. This command will call the Java Compiler asking it to compile the specified file. If there are no errors in the code the command prompt will take you to the next line. Compiler will convert your Java program to machine readable byte code.

Step 3: Now type java HelloWorld on command prompt to run your program.

After compiling when you will try to run the byte code(.class file), the following steps are performed at runtime:-
  1. Class loader loads the java class. It is subsystem of JVM Java Virtual machine.
  2. Byte Code verifier checks the code fragments for illegal codes that can violate access right to the object.
  3. Interpreter reads the byte code stream and then executes the instructions, step by step.
Step 4: You will be able to see output of the program printed on your command prompt.


Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: First Java Program Rating: 5 Reviewed By: Annamalai Thangaraj