Understanding Java Syntax and Basic Structure

On Day 2, the focus will be on the fundamental structure of a Java program and understanding the essential building blocks of Java syntax. We will learn about variables, data types, and how to write simple programs that handle basic input and output operations.
1. Basic Structure of a Java Program
Every Java program follows a specific structure. To understand this structure, consider the following basic example:
public class MyClass {
public static void main(String[] args) {
// code to be executed
System.out.println("Hello, World!");
}
}
Key Components:
Class Declaration:
public class MyClass: In Java, every program is enclosed within a class. The class name must match the file name. In this case, the file name should beMyClass.java.Classes in Java are the blueprint for creating objects and contain variables and methods that define the behavior of the objects.
Main Method:
public static void main(String[] args): This is the entry point of any Java program. The JVM looks for this method when executing the program. Theargsparameter allows you to pass command-line arguments to the program.
Code Block:
- The code to be executed is written inside the curly braces
{}of themainmethod. In the example,System.out.println("Hello, World!");is a statement that prints output to the console.
- The code to be executed is written inside the curly braces
Key Points:
Java is case-sensitive:
myClassandMyClassare different.Every Java statement ends with a semicolon (
;).Indentation is used for better readability but does not affect the execution of the program.
2. Variables and Data Types
Variables are used to store data in a Java program. They are declared with specific data types that define what kind of data the variable can hold.
Types of Variables:
Primitive Data Types: Java has 8 primitive data types that can store basic data values.
byte- 8-bit integer.short- 16-bit integer.int- 32-bit integer.long- 64-bit integer.float- Single-precision 32-bit floating point.double- Double-precision 64-bit floating point.char- Single 16-bit Unicode character.boolean- Representstrueorfalse.
Example:
int age = 25;
double salary = 50000.50;
char grade = 'A';
boolean isEmployed = true;
Reference Data Types:
- Unlike primitive types, reference types store references to objects. This includes arrays, classes, interfaces, etc.
Declaring Variables:
int num; // Declaration
num = 10; // Assignment
int anotherNum = 20; // Declaration and initialization
Naming Conventions:
Variable names should be meaningful and follow the camelCase style (e.g.,
studentName,totalMarks).Names can contain letters, digits, underscores, and dollar signs but must not start with a digit.
Avoid using reserved words (e.g.,
class,int,new).
3. Data Type Casting
Casting refers to converting a variable from one type to another. There are two types of casting in Java:
Widening Casting (Automatic): Converting a smaller data type to a larger one, like
inttodouble.int myInt = 9; double myDouble = myInt; // Automatic casting: int to double System.out.println(myDouble); // Output: 9.0Narrowing Casting (Manual): Converting a larger data type to a smaller one, like
doubletoint.double myDouble = 9.78; int myInt = (int) myDouble; // Manual casting: double to int System.out.println(myInt); // Output: 9
4. Input and Output in Java
Java provides the Scanner class for handling user input and System.out for printing output.
Output Using
System.out.println:The
System.out.println()method is used to print text or variables to the console.Example:
System.out.println("Hello, World!"); // Output: Hello, World!
Taking User Input:
- To take input from the user, the
Scannerclass is used:
- To take input from the user, the
import java.util.Scanner; // Import the Scanner class
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Create a Scanner object
// Taking input
System.out.print("Enter your name: ");
String name = scanner.nextLine(); // Read user input as string
System.out.print("Enter your age: ");
int age = scanner.nextInt(); // Read user input as integer
System.out.println("Hello " + name + ", you are " + age + " years old.");
}
}
Explanation:
Scanner scanner = new Scanner(System.in);: This creates a newScannerobject to take input from the console.nextLine(): Reads a full line of input.nextInt(): Reads an integer value.
Key Points:
Always close the
Scannerafter use to prevent resource leaks:scanner.close();For floating-point numbers, you can use
nextFloat()ornextDouble()methods.
5. Operators in Java
Java supports several types of operators, which are used to perform operations on variables and values.
Arithmetic Operators: Used for performing mathematical operations like addition, subtraction, multiplication, division, etc.
int x = 10; int y = 5; System.out.println(x + y); // Output: 15 System.out.println(x - y); // Output: 5 System.out.println(x * y); // Output: 50 System.out.println(x / y); // Output: 2Assignment Operators: Used to assign values to variables.
int x = 10; // Assigns value 10 to x x += 5; // x = x + 5 (now x is 15) x -= 3; // x = x - 3 (now x is 12)Comparison Operators: Used to compare two values.
int a = 10; int b = 5; System.out.println(a == b); // Output: false System.out.println(a > b); // Output: trueLogical Operators: Used for logical operations and often used in conditional statements.
boolean condition1 = true; boolean condition2 = false; System.out.println(condition1 && condition2); // Output: false System.out.println(condition1 || condition2); // Output: true
6. Writing Simple Programs
At the end of Day 2, we should be able to write simple Java programs, such as a calculator or a program that takes user input and performs arithmetic or logical operations.
Example: Simple Calculator
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
System.out.println("Sum: " + (num1 + num2));
System.out.println("Difference: " + (num1 - num2));
System.out.println("Product: " + (num1 * num2));
System.out.println("Quotient: " + (num1 / num2));
scanner.close();
}
}
By the end of Day 2, we will have a comprehensive understanding of Java’s syntax, basic structure, and how to write and execute simple Java programs. Stay tuned !




