First Java Program
After setting the path of java, we all set to write our first java program.
It's a very basic program for printing messages on the console.
public class Demo {
public static void main(String[] args) {
System.out.println("Hello Himanshu");
}
}
Output :
Hello Himanshu
before going to the description of the program, first, we will see how we execute this program and see the output in the console.
1) Make a folder "firstprogram" in any drive let it be E drive on your computer.
2) Open notepad in that folder and copy the above program in it.
3) Go to 'file', select 'Save As...' then select 'Save as type' as All Files, then give file name same that of the class name i.e., Demo.java, and save it. It will create a java file.
4) Now open command prompt and go to E drive, for this just write E: and press enter
5)Now go to the folder by writing cd<space> E:\firstprogram and press enter.
6) Now write javac<space>Demo.java and press enter, it will create Demo.class file in the same folder. It is basically the compiled file created by the java compiler.
7) Now write java<space>Demo and press enter.this will run the java interpreter for decoding and executing the .class file for JVM.
it will generate the required output:- Hello Himanshu
Description of above program
first of all, to write any java program we need to define a class. the class can be defined by using the class keyword as prefix followed by class name. a 'public' access modifier should be used before the class keyword which defines the accessibility of the class. we will discuss access modifiers in our later tutorials.
Moving to the second line, main() is required for the execution of the program.in it, the public keyword is defined as the accessibility of this method. static is a keyword that denotes the memory will be allotted only once when class loads, basically static keyword deals with memory management. void is the return type of main().it must be void and we have to pass string arguments in the method.
in syntax, we can also write static public instead of public static but in the standard way, we should write public static.
Now moving towards the third line, System is a built-in class in java, and out is a member of System class of type PrintStream.println() is a method in PrintStream class that prints data on the console.
it accepts expressions as arguments and prints on the console.
Lets have another java program for printing numbers 1 to 5 :
public class PrintNumbers {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
System.out.println(5);
}
}
Output :
1
2
3
4
5
Here is another program for printing some integer, string, and character data :
public class PrintData {
public static void main(String[] args) {
int i =1;
String str ="himanshu";
char c ='x';
System.out.println(i);
System.out.println(str);
System.out.println(c);
}
}
Output :
1
himanshu
x
Here is a program for printing special characters in the form of string :
public class PrintSpecialChar {
public static void main(String[] args) {
System.out.println("#!m@n$#u");
}
}
Output :
#!m@n$#u
No comments:
Post a Comment