Basic Syntax of Java and First Java Program

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

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.

Object

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.

Variable

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.

Method

 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.

Say Hello to the world of Java

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!!");
	}
}

Steps for Compilation and Successful execution using command prompt

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.

Conventions

  • Java is case sensitive. The case-sensitivity means that words like code, Code, CODE, CoDe are not considered the same. They are considered different and this should be kept in mind while coding.
  • There are a series of keywords that are reserved and cannot be used to name any class, methods, or variables. Such keywords are known as reserved keywords.
  • Class Name should always start with the Uppercase. In case, the meaningful name is composed of multiple words, each word should start with Uppercase.

            For example,

Valid Names Invalid Names
Building building
HelloToJava Hellotojava
  • The method name should always start with the lowercase. In case, the meaningful name is composed of multiple words, each word should start with Uppercase except the first.

           For example,

Valid Names Invalid Names
validName InvalidName
  • The variables have a series of the convention to be followed while naming. Similar to class names and methods, they are case sensitive. The name should begin with alphabets, $(dollar symbol) or _(underscore). The use of numbers or any other special character is strictly prohibited.
Valid Names Invalid Names
Floor, area, _cost 123cost, *Var
  • The file name should match case specific with the name of the public class. The extension is .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.
  • It could be seen that our class 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.
  • Comments are part of any programming language to perform internal documentation and provide extra information for future reference. Both single and multi-line Comments are available in JAVA.

            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.