Chapter -2 Java Fundamentals
2.1 The Parts of a Java Program
Basic Program Structure
Java programs consist of classes and a main method.
Example:
public class Main {
public static void main(String[] args) {
out.println(“Hello, World!”);
}
}
Explanation:
Class Definition
public class Main: Defines a class named Main.
Every Java program must have at least one class.
Main Method
public static void main(String[] args):
Entry Point: The JVM starts execution from the main method.
String[] args: Array to hold command-line arguments.
Statements
Instructions inside {}.
Example: System.out.println(“Hello, World!”); outputs text to the console.
Comments in Java
Single-line comments: // This is a comment
Multi-line comments:
/*
This is a multi-line comment.
*/
Documentation comments:
/**
* This is a documentation comment.
*/
2.2 Data Types and Variables
Primitive Data Types
Integer Types
byte: 1 byte (-128 to 127)
short: 2 bytes (-32,768 to 32,767)
int: 4 bytes (-2± to 2± – 1)
long: 8 bytes (-2⁶± to 2⁶± – 1)
Floating-Point Types
float: 4 bytes (approx. 6-7 decimal digits)
double: 8 bytes (approx. 15-16 decimal digits)
Character Type
char: 2 bytes (stores a single Unicode character).
Boolean Type
boolean: 1 bit (true or false).
Declaring Variables
int age = 25; // Declare and initialize an integer variable.
float price = 12.99f; // Declare and initialize a float.
char grade = ‘A’; // Declare and initialize a character.
boolean isValid = true; // Declare and initialize a boolean.
2.3 Arithmetic Operators
Basic Operators
Addition (+): Adds two values.
Subtraction (–): Subtracts one value from another.
Multiplication (*): Multiplies two values.
Division (/): Divides one value by another.
Modulus (%): Returns the remainder of division.
Example
int a = 10, b = 3;
System.out.println(a + b); // Output: 13
System.out.println(a – b); // Output: 7
System.out.println(a * b); // Output: 30
System.out.println(a / b); // Output: 3
System.out.println(a % b); // Output: 1
2.4 Combining Input and Output
Using the Scanner Class
The Scanner class reads user input from the console.
Example
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter your name: “);
String name = scanner.nextLine(); System.out.println(“Hello, ” + name + “!”);
}
}
Explanation:
new Scanner(System.in): Creates a Scanner object for reading input.
nextLine(): Reads a string of text.
2.5 Type Casting
Implicit Casting
Smaller types automatically convert to larger types.
Example:
int num = 10;
double result = num; // Implicit casting from int to double.
Explicit Casting
Larger types explicitly convert to smaller types.
Example:
double price = 12.99;
int roundedPrice = (int) price; // Explicit casting from double to int.
2.6 Constants
Using final Keyword
Declare constants with final.
Example:
final double PI = 3.14159;
System.out.println(“Value of PI: ” + PI);
2.7 Java Escape Sequences
Common Escape Sequences
\n: Newline
\t: Tab
\\: Backslash
\”: Double quote
\’: Single quote
Example
System.out.println(“Line 1\nLine 2”); // Outputs:
// Line 1
// Line 2
2.8 Formatting Output
Using System.out.printf
Format strings for precise control.
Example
System.out.printf(“Name: %s, Age: %d\n”, “Alice”, 30);
Format Specifiers:
%s: String
%d: Integer
%f: Floating-point
2.9 Debugging Techniques
Common Errors
Syntax Errors: Missing semicolon or braces.
Runtime Errors: Division by zero.
Logic Errors: Incorrect results due to flawed logic.
Debugging Steps
Check syntax and spelling.
Use print statements to trace execution.
Utilize an IDE’s debugging tools.
Summary
Java programs consist of classes, methods, and statements.
Key concepts include data types, variables, and arithmetic operations.
Input/output and debugging are essential for interactive and error-free programs.