1. Using Notepad:- Typing the code in notepad and running it through command prompt. Although it is a lengthy process but this approach is good for beginners for learning each step thoroughly.
2. Using an IDE like Netbeans, Eclipse etc:- This approach should be used after we have understood the basic working of java . Because as an IDE hides all the basic steps which are very important to understand.
Developing Java Programs using Notepad:-
Developing and running a java program requires three main steps:-
1. Writing the source code
2. Compilation of the source code(Conversion from source code to byte code)
3. Execution of the source code(Interpretation of the byte code)
Writing the source code:-
Go to search bar and type Notepad and click it by "run as administrator". Now type the code in the text editor:-
First Java Program:-
Understanding of the source code:-
First of all we must remember that java is a highly case sensitive language. It means that we have to be very careful about uppercase and lowercase letters while typing the code.
For example, in the above code, three letters are Compulsorily in uppercase and they are-
"T" of Test, "S" of String and "S" of System.
This is because in java class names begin with uppercase letter.
The first statement of our code is-
class Test:- Since Java is an object oriented programming language and it strictly supports Encapsulation, so every java program must always contain at-least one class and whatever we write must appear within the opening and closing braces of the class.
The second statement of our code is-
public static void main(String [ ] args):- In Java also(like c/c++) the entry point of execution of our program is the method main( ) which is called by the JVM. The word shown in green colors are keywords and each has a different meaning and purpose for the method main().
Let's understand each of them in detail:-
Why main( ) is public?
public is an access modifier in java and in object oriented programming any method or variable is declared public can be accessible from outside of that class.
Since main( ) is public in Java. So, JVM can easily access and execute it.
Why main( ) is static?
static is an storage specifier or non access modifier in java and every class can have two kinds of methods, static and non-static.
A method which is non-static can only be called using object of that class, while a static method can be called without any object, simply using class name.
When the JVM makes are call to the main( ) method, there is not object existing for the class being called therefore it has to have static method to allow invocation from the class.
Why main( ) has return type void?
The keyword void is called return type which indicates that no value will be returned by the method to it’s caller.
Since main( ) method in Java is not supposed to return any value to the JVM, its made void which simply means main( ) is not returning anything.
Can we change or remove the keywords used with main( )?
No, not at all.
This is because main( ) method is called by the JVM and to allow JVM to successfully call main( ) method, these keywords are important.
If we forget to write these keywords then although the code will compile but will fail to run.
All we can do change the order of public and static but we can not drop them.
What is String [ ] args?
String is a predefined class in java.
So the statement String [ ] args is declaring args to be an array of Strings. It is called command line argument and we will discuss it later.
For now, just remember that the statement String [ ] args has to be present with method main( ) otherwise code will not run.
Can we change or remove String [ ] args?
No, not at all.
Just like keywords used with main( ) method are compulsorily. similarly, String [ ] args is also compulsory.
All we can do is change the name from args to something else and also we can interchange the array name and [ ].
For example:- String args[ ], String [ ] args, String [ ] str, String str [ ] all are valid.
Understanding System.out.println( )?
Now let's understand code in the body of the main() method, which will print a message on the console.
Syntax:-
System.out.println("Message");
1. System is a predefined class in Java.
2. out is an object reference(not object).
3. println( ) is a method.
Together all three are used for displaying text on console.
We will discuss this part in detail once we have covered basics of Java.
Does every method has to be public, static and void?
No, it is not a compulsion.This is only with the method main( ) that we have to make it public, static and void. All the other methods have declarations as decided by the programmer.
Will a Java program compile without main( )?
Yes, because main( ) method is not needed for compilation. But It is used for execution of the code. So we can compile a Java program without main( ), but we can not run it.
Saving the source code:-
Once we have written the code, the next step is to save and compile it.
We can save our code in two locations:-
1. within "bin" sub-directory of jdk.
2. at any location in your device(This requires setting path variables also)
we will start with first approach and then migrate to second approach while learning about packages.
Saving the source code within "bin":-
1. To save the code in “bin”, just choose jdk’s bin as the "location to save" in Notepad.
2. In the "file name" option provide any name you like but with .java extension.
3. Generally we prefer giving the same name to our source code as the name of our class.(Remember it is a general choice not a rule!)
4. Also remember to give the filename in "double quotes" as otherwise notepad might add the extension .txt
5. Now since our class name is Test, so we would save our file by the name "Test.java"
Compilation of the source code:-
To compile our code we have to do the following:-1. open the command prompt by right clicking and selecting "run as administrator" option
2. Migrate to the jdk's bin folder
3. Type the command to compile the code
To compile a java program the command to be used is javac.
The general syntax of compilation is-
...bin><javac> <full name of .java file>
javac is the name of java compiler which takes the name of our source code as argument and generates the byte code.
For example:-
...bin> javac Test.java
Remember this command has to be given from jdk's bin folder as we have saved the file there only!
What happens when we compile our code?
Whenever we compile a java program, which have following important properties:-
1. It checks the syntax errors(like- missing semicolons, wrong class or method names etc).
2. If syntax error is found the compilation stops.
3. Otherwise if no syntax errors are there, the compiler generates the byte-code of our source code.
Points to remember about byte code:-
1. Byte-codes are generated as separate file.
2. The extension of byte-code files is .class.
3. The name of the byte-code file is always same as the name of the class defined by the programmer in his program. so in our case the byte-code file will be called as Test.class.
4. Number of .class files generated is always equal to number of classes defined by the programmer in his program.
For example- If our program contains three classes called "College", "Faculty" and "Student" then three byte-code files would be generated called as:-
1. College.class
2. Faculty.class
3. Student.class
Execution of the source code:-
To run or execute a java program the command to be used is java.The general syntax of compilation is-
...bin><java> <name of the class conating main() method >
java is the Java interpreter which takes .class file as argument (note:- do not write the extension .class)
The .class file should contain main() method that is executed by the Java Interpreter.
For example:-
...bin> java Test
Important Interview questions:-
How many .class files would be generated for the following code?
class A
{
}
class B
{
}
class C
{
}
{
}
What should be the name of the program?
class A
{
}
class B
{
}
class C
{
}
{
}
Answer:- Although we can give any name but it is preferred to give the same name as the class which contains main() method.
0 Comments