Java Basic Syntax

A Java program is a collection of objects, and these objects communicate through method calls to each other to work together.

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 template which describes the data and behaviour associated with its instance.

  • Example: Blueprint of the house is class.

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

  • Example: A car is an object whose states are: brand, colour, number-plate.
  • Behaviour: Running on the road.

3. Method: The behaviour 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.

 

 

The Basic Syntax:

1. Comments in Java

There are three types of comments in Java. 

 i. Single line Comment

// System.out.println("Hello");

ii. Multi-line Comment

/*

System.out.println("Hello");

System.out.println("Hi");

*/

 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 MYCLASS.

 

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("Prasanjeet"); // valid syntax
system.out.println("Prasanjeet"); // invalid syntax
 

4. Class Names

The first letter of the class should be in Uppercase (lowercase is allowed, but not discouraged).

class MyJavaProgram    // valid syntax
 class 1Program         // invalid syntax
 class My1Program       // valid syntax 
 

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

i. All the method names should start with a lowercase letter.

ii. 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, labels, but also the names for classes, packages, modules and methods. All Unicode characters are valid, not just the ASCII subset. 

i. 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.

ii. 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.

iii. Most importantly identifiers are case-sensitive.

iv. A keyword cannot be used as an identifier since it is a reserved word and has some special meaning.

 

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, strictfp

 

 

No comments:

Post a Comment