After installing java and setting the path for it now its turn to have some fun with codes. Now i am going to tell you how to print your text in java. Its a very simple program and basically for beginners. There are some important concept of packages in java and i m going to use that package concept in my program just to make you aware for this. We will see the details in their respective topics.
import java.lang.Object;
public class FirstProgram{
public static void main(String arg[]) {
System.out.println(" This is my first program in java " );
}
}
Write the above program in a text editor like notepad and save it as the name of your class_name , here it is FirstProgram. So you must save it as,
FirstProgram.java
After saving now you can compile and run the above code in command prompt like this,
First go to directory where you saved your program and then , run code like this,
javac FirstProgram.java
java FirstProgram
Output:- This is my first program in java
In the above program there is something interesting which you should know i.e ,
1) The use of import keyword ( Line 1 in the above program)
2) Use of public keyword before class ( Line 2 )
3) Use of main() method ( Line 3 )
4) Use of System.out.println() ( Line 4 )
Import keyword :- Import statement must be the first statement in java , it can't be the 2nd or any other because while compiling java code, compiler must know which class is going to import. By using import statement you are going to tell compiler to import the classes available on that package before writing any other statements. For now you just keep remember that package is the collection of predefined classes and within that classes there are many methods defined which we are going to use in our code for building the application. So compiler must know about which packages is to import. For details in Import statement Click Here !
Here, in the above program i used java.lang.Object , it means i m using java.lang which is a package and Object which is a class in this full import statement. And import keyword must be written in lower case.
You can have multiple import statement and must be terminated by semi-colon( ; ).
Syntax for import statement :- import <package_ name>;
Note: If you are not writing / maintaining any package then by default it will import java.lang.Object.
so we can say that java.lang.Object is a default package in java.
Public keyword :- In java, public is an access modifier. It tells about the accessibility scope, that scope may be for a variable/methods/class etc . And we are allowed to use only one public class in a single program.If there are two Public classes in a single program then it would be difficult for compiler to identify which public class name is used as to save as our program name. But it is not necessary that your program must contain at least one public class, but its mandatory that there should be only one public class if you are defining any public class in java and program name should be as the name of the public class. For details regrading this topic must read out saving java files topic later in this tutorial.
I you are not using any public access modifier before class that means it is be default " Default " which is one of the access modifier in java.
Syntax for using public access modifier:-
public class <class_name>{}
Main() method :- Main method in java is indicating as the starting point of the execution of your program. You can have multiple main() method but the point is that it should be in different class. Means one class can't accommodate two main() method, will give you a compilation error.
In short one class can have only one main() method. And a program may contain more then one class so we can define main() method for each classes, it means our java program can contain more than one main() method, it won't give any compilation error but the execution of the main() method depends on the program name which is one of the class you defined in your program.
If you have multiple class and in each class you have one main method then JVM is going to execute the main method of that class which is saved as a program name. But again it is going to execute the other classes main method only when you mention that class name while executing ( not compiling) your program by the JVM. If you have confused then will make it clear in execution of main() method topic later in this tutorial.
What is that string we are passing as an argument? and
Why we are defining static to the main() method?
To clear all these doubts regrading the main() method Click Here !
System.out.println :- Here in this statement, System is a class where as out is the print stream and println() is the method for printing the text defined withing inverted commas ( " " ).
Why we have used the above statement like this?
--> It is because to indicate the compiler that println() is a method of System class and out is the print stream of System class.
EmoticonEmoticon