Pages

Content of this Website is not optimized for Mobile📵. It is recommended that the Website be viewed on a Laptop💻 or Desktop🖳🖥.

Java Basic Syntax

Java program is an object-oriented programming language, that means java is the collection of objects, and these objects communicate through method calls to each other to work together.

Here is a brief discussion on the Classes and Objects , Method , Instance variables , Syntax, and Semantics of Java.

Basic Terminologies in Java



1. Class: The class is a blueprint (plan) of the instance of a class (object). It can be defined as a logical template that share common properties and methods.

Example1: Blueprint of the house is class.

Example2: In real world, Alice is an object of the “Human” class.

2. Object: The object is an instance of a class. It is an entity that has behavior and state.

Example: Dog, Cat, Monkey etc. are the object of “Animal” class.

Behavior: Running on the road.

3. Method: The behavior of an object is the method.

Example: The fuel indicator indicates the amount of fuel left in the car.

4. Instance variables: Every object has its own unique set of instance variables. The state of an object is generally created by the values that are assigned to these instance variables.

Syntax in Java



1. Comments in Java:

There are three types of comments in Java.

i. Single line Comment

// System.out.println("This is an comment.");

ii. Multi-line Comment

/*

    This is the first line comment.

    This is the second line comment.

*/

iii. Documentation Comment. Also called a doc comment .

/** Documentation */

2. Source File Name

The name of a source file should exactly match the public class name with the extension of .java

The name of the file can be a different name if it does not have any public class. 

Assume you have a public class TestClass.

TestClass.java // valid syntax

Test.java // invalid syntax

3. Case Sensitivity:

Java is a case-sensitive language, which means that the identifiers AB, Ab, aB, and ab are different in Java.

System.out.println("Hello world!"); // valid syntax

system.out.println("Hello world!"); // invalid syntax 

because of the first letter of System keyword is always uppercase. 

4. Class Names:

  1. The first letter of the class should be in Uppercase (lowercase is allowed but discouraged).
  2. If several words are used to form the name of the class, each inner word’s first letter should be in Uppercase. Underscores are allowed, but not recommended. Also allowed are numbers and currency symbols, although the latter are also discouraged because they are used for a special purpose (for inner and anonymous classes).

class MyJavaProgram    // valid syntax

class 1Program         // invalid syntax

class My1Program       // valid syntax

class $Program         // valid syntax, but discouraged

class My$Program       // valid syntax, but discouraged (inner class Program inside the class My)

class myJavaProgram    // valid syntax, but discouraged

5. public static void main(String [] args):

The method main() is the main entry point into a Java program; this is where the processing starts. Also allowed is the signature public static void main(String… args).

6. Method Names:

  1. All the method names should start with a lowercase letter (uppercase is also allowed but lowercase is recommended).
  2. If several words are used to form the name of the method, then each first letter of the inner word should be in Uppercase. Underscores are allowed, but not recommended. Also allowed are digits and currency symbols.

public void employeeRecords() // valid syntax

public void EmployeeRecords() // valid syntax, but discouraged

7. Identifiers in Java:

Identifiers are the names of local variables, instance and class variables, and labels, but also the names for classes, packages, modules and methods. All Unicode characters are valid, not just the ASCII subset.

  1. All identifiers can begin with a letter, a currency symbol or an underscore ( _ ). According to the convention, a letter should be lower case for variables.
  2. The first character of identifiers can be followed by any combination of letters, digits, currency symbols and the underscore. The underscore is not recommended for the names of variables. Constants (static final attributes and enums) should be in all Uppercase letters.
  3. Most importantly identifiers are case-sensitive.
  4. A keyword cannot be used as an identifier since it is a reserved word and has some special meaning.

Legal identifiers: minNumber, total, ak74, hello_world, $amount, _under_value

Illegal identifiers: 74ak, -amount

8. White spaces in Java:

A line containing only white spaces, possibly with the comment, is known as a blank line, and the Java compiler totally ignores it.

9. Access Modifiers: These modifiers control the scope of class and methods.

Access Modifiers: default, public, protected, private.

Non-access Modifiers: final, abstract, static, transient, synchronized, volatile, native. 


11. Java Keywords:

Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as variable names or objects.

 

Java Hello world Program



Implementation of a Java application program involves the following steps. They include:

  1. Creating the program
  2. Compiling the program
  3. Running the program

1. Creating Programs in Java: We can create a program using Text Editor (Notepad) or IDE (Eclipse / NetBeans)
Create the program by typing it into a text editor or with IDE and save it to a file – HelloWorld.java.

2. Compiling the Program in Java: To compile the program, we must run the Java compiler (javac), with the name of the source file on the “command prompt” by typing “javac HelloWorld.java”.

If everything is OK, the “javac” compiler creates a file called “Test.class” containing the byte code of the program.

3. Running the Program in Java: We need to use the Java Interpreter to run a program. Execute (or run) it by typing “java HelloWorld” in the terminal window.

The below-given program is the most simple program of Java printing “Hello World” to the screen. Let us try to understand every bit of code step by step.

// This is a simple Java program.
public class HelloWorld {
    /* 
    Your program begins with a call to main().
    Prints "Hello, World" to the terminal window.
    */
    public static void main(String[] args)
    {
        System.out.println("Hello, World");
    }
}

1. Class Definition: This line uses the keyword class to declare that a new class is being defined.

public class HelloWorld {
   //Statements
}

2. HelloWorld: It is an identifier that is the name of the class. The entire class definition, including all of its members, will be between the opening curly brace “ { ” and the closing curly brace“ }” .

3. main Method: In the Java programming language, every application must contain a main method. The main function(method) is the entry point of your Java application, and it’s mandatory in a Java program. whose signature in Java is:

public static void main(String[] args)

Explanation of the main method syntax:
  • public: So that JVM can execute the method from anywhere. 
  • static: The main method is to be called without an object. The modifiers are public and static can be written in either order. 
  • void: The main method doesn’t return anything. 
  • main(): Name configured in the JVM. The main method must be inside the class definition. The compiler executes the codes starting always from the main method/function. 
  • String[]: The main method accepts a single argument, i.e., an array of elements of type String.

No comments:

Post a Comment

Kings Classes: Quick Links!