Java Program Structure, OOP Concepts, Collections & Java 8 Features
1. Structure of a Java Program
A Java source file is organized into specific sections. Understanding each part is essential before writing
any Java program.
1.1 Documentation Section
The documentation section consists of comments that improve the readability of the program. They may
include basic information such as a method's usage or functionality, making it easier for programmers to
understand while reviewing or debugging. Comments can appear anywhere in the code and are
completely ignored by the compiler during execution.
Java supports three types of comments:
• Single-line comment: // comment text
• Multi-line comment: /* comment spanning multiple lines */
• Documentation comment: /** starts with /** and ends with */
// a single line comment
/* a multi-line comment
can span multiple lines */
/** a documentation comment */
1.2 Package Statement
Java allows classes to be organized into a named collection called a package. There can be only one
package statement in a Java program, and it must appear at the very beginning of the source file, before
any class or interface declaration. This statement is optional.
• Declares that all classes and interfaces in the source file belong to that package.
package student;
1.3 Import Statement
Many predefined classes are stored in packages. An import statement is used to refer to classes stored in
other packages. It is always written after the package statement but before any class declaration.
import java.util.Date; // imports only the Date class
import java.applet.*; // imports all classes from the applet package
import java.io.*; // imports all classes from the IO package
1.4 Interface Section
This optional section is used to declare interfaces in Java. An interface is similar to a class but contains
only constants and method declarations (no implementation). It is mainly used to implement multiple
inheritance in Java.
interface stack {
void push(int item);
void pop();
}
,1.5 Class Definition
The class definition section contains the actual class. The class name must start with a capital letter. The
keyword public means the class is accessible from any other class.
class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
1.6 Complete Example – Hello Java
A complete Java program illustrating package, class, and method sections:
package basicPrograms;
public class HelloJava {
public static void main(String[] args) {
// Prints Hello, Java! on standard output.
System.out.println("Hello Java!");
}
}
2. Scanner Class – Taking User Input
The Scanner class (from java.util package) is used to read input from the user via the keyboard. It
provides methods for reading different data types.
Method Reads
sc.nextInt() Integer value
sc.nextFloat() Float value
sc.nextDouble() Double value
sc.next().charAt(0) Single character
sc.nextLine() Full string (including spaces)
Note: After reading an integer or float, call sc.nextLine() to clear the input buffer before reading a String.
import java.util.Scanner;
class Mains {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = sc.nextInt();
System.out.print("Enter a float value: ");
float f = sc.nextFloat();
System.out.print("Enter a character: ");
char c = sc.next().charAt(0);
sc.nextLine(); // Clear the buffer
System.out.print("Enter your name: ");
, String name = sc.nextLine();
System.out.println("Integer: " + n);
System.out.println("Float: " + f);
System.out.println("Character: " + c);
System.out.println("Name: " + name);
}
}
3. Classes and Objects
A class is a blueprint or template for creating objects. An object is an instance of a class. The class
defines attributes (data members) and behaviors (methods).
Example – Car class:
class Car {
String brand;
String model;
int year;
void display() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
public static void main(String[] args) {
Car c = new Car(); // creating object
c.brand = "Toyota";
c.model = "Corolla";
c.year = 2022;
c.display();
}
}
Example – Student class:
class Student {
String name;
int age;
void display(String a, int b) {
name = a;
age = b;
System.out.println("Name is " + name);
System.out.println("Age is " + age);
}
}
public class Details {
public static void main(String args[]) {