Developing Java Programs
Developing Java Programs
[vc_row][vc_column][vc_column_text]Java Program Development can occur in two ways:
1.Developing Java Programs-Using Notepad:
Typing the code in notepad and running it through command prompt. This approach is good for beginners for learning each step thoroughly
2.Developing Java Programs-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 program requires three main steps:
(i) Writing the source code
(ii) Compiling the code
Conversion from source code to bytecode
(iii) Executing the code
Interpretation of the bytecode
Step 1-Writing the Source Code
Select notepad.exe from the list shown in pop up menu of run command.
Right click and choose “run as administrator” option.
Now type the very first Java code given below[/vc_column_text][/vc_column][/vc_row][vc_row css=”.vc_custom_1537255741659{background-color: #e0e0e0 !important;}”][vc_column][vc_column_text]class Test
{
public static void main(String [ ]args)
{
System.out.println(“Hello User”);
}
}[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]For detail Explanation of the given above program Click this → Understanding The First Java Program
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:
Within “bin” subdirectory of jdk
OR
At any location in our machine
(This requires setting “PATH” variable also)
→ We will start with first approach and then migrate to second approach while learning about packages.
→ To save the code in “bin” , just choose jdk’s bin as the “location to save” in notepad.
→ In the “file name” option provide any name you like but with .java extension.
→ 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!)
→ Also remember to give the filename in “double quotes” as otherwise notepad might add the extension .txt
→ Now since our class name is Test , so we would save our file by the name “Test.java”
Step2-Compiling the code
→ To compile our code we have to do the following:
→ Open the command prompt by right clicking and selecting “run as administrator” option
→ Migrate to the jdk’s bin folder
C:\cd ProgramFiles
C:\ProgramFiles>cd Java
C:\ProgramFiles>Java>cd Jdk 1.8.0
C:\ProgramFiles>Java>jdk 1.8.0>cd Bin
C:\ProgramFiles>Java>jdk 1.8.0>bin>
→ Type the command to compile the code
→ The general syntax of compilation is:
<javac > <full name of .java file>
javac is the name of java’s compiler which takes the name
of our source code as argument and generates the bytecode
→ For example:
javac Test.java
→ Remember this command has to be given from jdk’s “bin” folder as we have saved the file there only!
[/vc_column_text][vc_custom_heading text=”What happens when we compile our code ?” font_container=”tag:h2|text_align:left|color:%23606060″ google_fonts=”font_family:Montserrat%3Aregular%2C700|font_style:700%20bold%20regular%3A700%3Anormal”][vc_column_text]→ Whenever we compile our java code , the compiler does the following:
→ It checks for syntax errors (like missing semicolons,wrong class or method names etc)
→ If any syntax error is found the compilation stops.
→ Otherwise if no syntax errors are there the compiler generates the “bytecode” of our “source code”[/vc_column_text][vc_custom_heading text=”Points To Remember About “bytecodes”” font_container=”tag:h2|text_align:left|color:%23606060″ google_fonts=”font_family:Montserrat%3Aregular%2C700|font_style:700%20bold%20regular%3A700%3Anormal”][vc_column_text]→ Bytecodes are generated as separate files.
→ These files have the extension .class and their name is same as the name of the class defined by the programmer.
→ For example if class name is Test , the bytecode name will also be “Test.class”
→ Number of bytecode files generated is same as number of programmer defined classes. So if our program contains three class called “College” , “Faculty” and “Student” , then three bytecode files would be generated called:
→ College.class
→ Faculty.class
→ Student.class[/vc_column_text][vc_column_text]
Step 3-Executing The Code
→ The general syntax to run our code is:
java <Name of the class containing main method>
→ java is the Java interpreter which takes .class file as argument (note: do not write the extension .class).
→ This class file should contain main() method that is executed by the Java interpreter.
→ Execution command is case sensitive while compiling is not. So we should be cautious with class name.
For example, if class “Test” has the main() method then our command would be:
java Test
[/vc_column_text][/vc_column][/vc_row]