As previously mentioned, Java is object-oriented and the overall approach of programming is a collection of objects, their interaction fulfilling the purpose and intent. Across this lecture, we are going to use a few words very frequently. It is better if we get familiar with them.
Class is like a blueprint that has no physical existence but has a prototype and set of state followed by behaviors. As per chronology, before creating any building, we create a blueprint. This blueprint contains all the details, but it is not a building actually.
The physical representation of class, in short, the instance of the class is called an object. Extending our previous example, the blueprint is class, and the building made adhering to the blueprint is the object, let us name it Elite Heights. It has all the states and behaviors as stated in class building.
Each object has a unique set of variables that represent the state. An object's state is created by the values assigned to these variables. Extending our previous example, the state of classes includes height, area, number of families, etc. Each and every building has a different set of values for these variables.
The behavior of the class is technically termed as a method. A class can have many behaviors and these behaviors are a series of logic defined to utilize and manipulate the state. Extending our previous example, say there is a method of booking in class building. Upon booking, the state number of families gets increased.
Now we are going to write our first JAVA program.
public class HelloToJava
{
/*
Program Name: HelloToJava
Purpose: This is program1.0 and prints Hello World!! at console.
*/
public static void main(String [] args)
{
System.out.println("Hello World!!");
}
}
Step 1: Write the code using Editor
You can choose any known text editor like notepad, notepad++ to write the code.
Step 2: Save the file(extension .java)
The file is to be saved with the name HelloToJava.java.
Step 3: Open Command Prompt
You need to open the command prompt and change the directory to the location where HelloToJava.java
is saved.
Step 4: Compile
The code first needs to get compiled, to generate the bytecode as discussed in the previous lecture. Type javac HelloToJava.java
. Upon execution and assuming no error occurred, the HelloToJava.class
gets generated and is ready to be interpreted.
Step 5: Interpret
Now type java HelloToJava
to finally run your first program.
After successful completion of the steps Hello World!! will be printed.
For example,
Valid Names | Invalid Names |
---|---|
Building | building |
HelloToJava | Hellotojava |
For example,
Valid Names | Invalid Names |
---|---|
validName | InvalidName |
Valid Names | Invalid Names |
---|---|
Floor, area, _cost | 123cost, *Var |
.java
(even the word java is case sensitive). The file may or may not contain a public class. In case the public class is available, the file name is bound to be the same, else not.HelloToJava
has a method main
. This method serves as the starting point of the program execution. Hence it is mandatory to have the main method. Also, note that the main method has a very specific signature public static void main(String args[])
. Any variation will result in an error, as the starting point of the program cannot be found. In future lessons, we will understand the significance of each and every keyword mentioned here.Example:
//This is Single line Comment
System.out.println("Hello World");
/* This is MultiLine Comment.
The below code will print 'Hello world' words
*/
System.out.println("Hello World");
The comments are strictly for the programmer's use and are not considered by compilers while generating the bytecode.
In the future classes, we will learn what happens behind, when we compiled and executed our code. The concept of JDK, JVM, and JRE will be clear in the upcoming lectures.