Java Syntax and Program Structure

Java Syntax and Program Structure, Java Environment setup, Java Language Fundamentals, Writing Java statements, Java coding standards, and Java code blocks.

Java Syntax

1. Java is a case sensitive language

Note: All Java keywords and reserved words are small letters
(if, for, public, main, true, false, null…)

2. First letter of the Java class name should be in upper case

sample //Incorrect
Sample //Correct
Firstprogram //Correct
FirstProgram ////Correct

3. Java Method names should start with lower case



4. Java program file name should exactly match with the class name

5. Java program execution starts from main method, which is mandatory in every Java program

6. Every statement /step should end with a semicolon (;)

7. Code blocks (conditional, Loop, Method, etc.) enclosed with {},

8. Java supports Explicit declaration of Data Types

In Java,
int sno =123;//Correct
int x;//Correct
char a=’A’; //Correct
boolean y=true;
abc =100; //Incorrect

In VBScript
Dim city
city =100
city =”India”

9. Java supports Explicit declaration of Variables

In Java:

int a, b;

In VBScript

Dim a
b=200 ‘Correct

Java Syntax and Program Structure

Java Program Structure

1. Documentation Section

It includes comments to tell the program’s purpose, it improves the readability of the Program

2. Package Statement

package abcd;

3. Import statement/s

We import predefined and user-defined libraries using “import” keyword


import java.util.Scanner;
java – Project
util – Package
Scanner – Class

import java.util.*; // Importing all classes from a package

4. Class Definition

public class Sample{


5. Interface Section

In this section, we create/declare Static variables, and create/declare Methods

6. main() Method

Java program execution starts from main() method, which is mandatory in every Java program.

public static void main (String [] args){

public – Access Modifier
static – Non Access Modifier (use main method without invoking an object)
void – Returns Nothing
main – Predefined Identifier(Method name)
//Within Main method

main method{
//Main Program
Creating Object/s
ClassName objectName = new ClassName();

Declarations….(Variable with Data Types, Constants/Final Variables)
Normal Statements
int a;//Declaration
int b=a;//Reading
System.out.println(10+20); //Print Statement
final int x=100;

if (){ //Condition Code Block

for (){ //Loop Code Block


Java Syntax and Program Structure

Sections of Java Program

1. Documentation Section – Optional

2. Package Statement – Mandatory

3. Import Statement/s – Depends on our program requirement

4. Class Definition { //Mandatory

5. Interface Section //Depends on our program requirement

6. main method(){
//Main Program contains normal statements and code blocks

Object Creation //Depends on our program requirement

Declarations/Initialization/Reading – normal statements

Operations, Display statements, etc…

Code Blocks, Conditions/Decision-making blocks, Loop Blocks, Constructor Block., Etc…
Interface Section //Depends on our program requirement

java tutorial

Core Java Step by Step Tutorial

Java Program Example

//This program is for explaining Java Syntax and program Structure.

package abcd;

import java.util.Scanner;

public class Sample {
//Create a Non static method with arguments and return a value
public int add(int a, int b) {
int result;
return result;
//Create a Non static method without arguments and returns Nothing
public void sub() {
int x=100;
int y=50;
//Create a Static method with arguments and return a value
public static int multiply(int num1, int num2) {
int result= num1*num2;
return result;
//Create a Static method without arguments and returns Nothing
public static void comparision() {
int x=100, y=50;

if (x>y) {
System.out.println(“X is Big Number”);
else {
System.out.println(“Y is Big Number”);

public static void main (String [] args){
Scanner scan = new Scanner (;

System.out.println(“Enter Your Name”);
String myName = scan.nextLine();
System.out.println(“My Name is: ” + myName);
int a; //Variable Declaration
int b=200; // Variable Declaration with Initialization
char x=’Y’;
String myCountry = “India”;
int c, d, e; //Declaring multiple variables
int f=20, g=40, h=60; //Declaring multiple variables with Initialization
final int price=100;

if (a>b)
System.out.println(“A is Big Number”);
System.out.println(“B is Big Number”);

for (int i=1; i<=10; i++)
{ // Print 1 to 10 Numbers

int i=1;
while (i<=5) {
System.out.println(“My Output”);
//Print 1 to 5 Numbers except 4
for (int j=1; j<=5; j++) {
if (j!=4) {

//Create Object and call nonstatic methods
Sample obj = new Sample();
int val = obj.add(100, 200);

System.out.println(obj.add(10, 20));//30


//Call Static methods

val = multiply(10, 40);

comparision();//X is Big Number

Java Quick Tutorial for Beginners

G C Reddy

G C Reddy, Working as a Software Testing Trainer in Hyderabad, India.

You may also like...