Java Basic Syntax

Java Basic Syntax, Java case sensitive, Java Program structure, Java coding standards, Java modifiers, Java comments, and Writing Java Programs.

Java Programming Syntax

The syntax of Java refers to the set of rules defining how a Java program is written and interpreted.

Basic components of Java Programming:

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

1. Class: The class is a blueprint(plan) of class objects and status.

Example: The blueprint of the house is class.

2. Object: The object is an instance of a class, has Behavior and state.

Example: A Car is an object

States of the Car object: brand, color, height, etc,

The behavior of the Car object: Running on the road.

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

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.

Basic Syntax Rules:

1. Case Sensitivity

Java is a case-sensitive language, there is a difference between upper case and lower case letters.

Examples:

Suppose Java Class Name – ‘MyClass’ is different from’ myclass’.

Java variable name – ‘NUM’ is different from ‘num’.

Note: Java keywords/Reserved words are small letters, and we can use upper case or lower case letters for Java identifiers. If we use upper or lower case letters for any identifier then use the same letters for the entire program.

2. Comments in Java

Inserting comments in Java programs is optional but best practice, it is not a syntax rule, coding standard.

We write comments in computer programs to increase the readability of programs and preventing some parts of the code from execution.

Java supports three types of comments:

1. Single-line Comments

Syntax:

// It is a single line comment

2. Multi-line Comments

Syntax-

/*Comment starts
continues
continues
.
.
.
Comment ends*/

3. Documentation Comments
This kind of Java comments is utilized by large code for a programming bundle since it produces a documentation page for reference.

/**Comment start
*
*tags are used in order to specify a parameter
*or method or heading
*HTML tags can also be used
*such as <h1>
*
*comment ends*/

3. Java Program File Name

The name of a Java program file should exactly match the Java class name with an extension of .java.

4. Java Class Names

a. The first letter of the class should be in Uppercase

b. If several words are used to form a name of the class, each inner word’s first letter should be in Upper Case, Underscore are allowed.

Example:
class Sample // valid syntax
class MyJavaProgram // valid syntax

class sample // invalid syntax
class myJavaProgram // invalid syntax

5. main() Method

Java program processing starts with the method main(), which is mandatory for every Java program.

6. Java Method Names

All the Java method names should start with a Lower Case letter.

7. Java Identifiers

a. All identifiers can begin with a letter (A to Z or a to z) or an underscore _.

b. The first character of identifiers can have any combination of characters.

c. Most importantly identifiers are case-sensitive.

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

Note: Class names, Variable names, and Method names, etc are the Identifiers in Java.

8. 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 identifiers.

Example:

Modifiers are keywords: public, private, protected, static, final, synchronized,

Primitive Data types are keywords: byte, short, int, long, float, double, char, boolean,

Control flow statements are keywords: if, esle, switch, for, while, do, break, continue, return,

Others:
package, import, this, super, throw, try, catch, void, etc,

9. White-spaces and Blanks lines in Java

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

10. Java Modifiers

These modifiers control the scope of classes, methods, and variables.

11. Java Statements and Code blocks

Every Java statement or step should end with a semicolon, and Java code blocks are enclosed with curly braces {}.

Example:

Java Statements:

int num1 =100;
int num2 =200;
System.out.println(num1+num2);

Java Code blocks:

if (num1>num2) {
System.out.println(“Num1 is Big Number”);
}
else {
System.out.println(“Num2 is Big Number”);
}
——————
for (int i=1; i<=5; i++){
System.out.println(i);
}

12. Explicit Declarations of Variables

Java supports the explicit declaration of variables, which means we need to declare first before using them.

Example:

int x;
x=100; //valid syntax
y=200; //invalid syntax


Java Programming Language Syllabus

Java Step by Step Videos

Follow me on social media: