Let 's create the hello java program:
{
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.
- 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
- 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[])
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 ...
class Simple
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
output: we can apply semicolon in java
There are some valid java main method which we can declare
- public static void main(String[] args)
- public static void main(String args[])
- public static void main(String args[])
- public static void main(String...s)
- static public void main(String []args)
- public static final void main(String []args)
- final public static final void main(String []args)
- final strictfp public static final void main(String []args)
There are some invalid java main method which we can't declare
- static void main(String[] args)
- public void main(String[] args)
- public void static main(String[] args)
i dont understand
ReplyDelete