Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday 21 November 2016

First Java Program

 

Let 's create the hello java program:

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

save it by HelloJava.java//you can save it other name but it is good

To Compile: javac  HelloJava.java

To Run: java  HelloJava

Output : Hello Java Java


👉: If you make a java class as a public class, you have to save it as the class name only, you cannot save it to other names than a class name.


For example:


public class  HelloJava

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

you have to save it by HelloJava.java

To Compile: javac  HelloJava.java
To Run: java  HelloJava

Output : Hello Java Java


Understanding first java program

  • class keyword is used to declare a class in java.
  • public is an access modifier, it is visible to all. we will learn in later.
  • static is a keyword if we declare any method as static, it is known as a static method. it saves a memory.
  • void is the return type of the method, it means it doesn't return any value.
  • main represents the startup of the program.
  • String args[] it is used for command line argument.
  • System.out.println() is used print statement in java programs.




Steps to write, compile and run the program in java:

To see clear image, click on image

  • To write the program in java open notepad by start menu->All Programs->Accessories->notepad and write a java program.

java program

as displayed in the above diagram, write a java program in notepad and save it as HelloJava.java.

  • After writing and saving first java program, To compile and run this program you need to open command prompt go start menu->All Programs->Accessories->command prompt

How to run java from cmd


  • To compile and run java program you have to go current directory where you save java file i.e HelloJava.java (we saved this file in C:\java new) and then type javac HelloJava.java for compiling and then enter and then type java HelloJava for running java program.

There are some other ways, we can write a java program .

(1) We can change the sequence of modifiers in the main method.

  • static public void main(String args[])

For example:

class Simple

{
static public void main(String args[])
{
System.out.println("hello java");
}
}

output: hello java


(2) We can change subscript notation in java in the main method.

  • public static void main(String[] args)

For example:

class Simple
{
public static void main(String[] args)
{
System.out.println("how are you");
}
}

output: how are you


  • public static void main(String []args)


For example:

class Simple
{
public static void main(String []args)
{
System.out.println("I am good");
}
}

output: I am good


  • public static void main(String args[])


For example:

class Simple
{
public static void main(String args[])
{
System.out.println("Thank you");
}
}

output: Thank you

(3) We can use 3 ellipses(dots) in the main method.

  • public static void main(String...s)


For example:

class Simple
{
public static void main(String...s)
{
System.out.println("3 ellipses ...");
}
}

output: 3 ellipses ...


We can apply semicolon (;) at the end of a class in java program. It is optional.

For example:

class Simple

{
public static void main(String []args)
{
System.out.println("we can apply semicolon in java");
}
};

output: we can apply semicolon in java



There are some valid java main method which we can declare

  1. public static void main(String[] args)
  2. public static void main(String args[])
  3. public static void main(String args[])
  4. public static void main(String...s)
  5. static public void main(String []args)
  6. public static final void main(String []args)
  7. final public static final void main(String []args)
  8. final strictfp public static final void main(String []args)

There are some invalid java main method which we can't declare 

  1. static void main(String[] args)
  2. public void main(String[] args)
  3. public void static main(String[] args)
Share:

1 comment:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate