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:

  1. Class Definition

    • public class Main: Defines a class named Main.
    • Every Java program must have at least one class.
  2. 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.
  3. 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

  1. 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)
  2. Floating-Point Types

    • float: 4 bytes (approx. 6-7 decimal digits)
    • double: 8 bytes (approx. 15-16 decimal digits)
  3. Character Type

    • char: 2 bytes (stores a single Unicode character).
  4. 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

  1. Addition (+): Adds two values.
  2. Subtraction (): Subtracts one value from another.
  3. Multiplication (*): Multiplies two values.
  4. Division (/): Divides one value by another.
  5. 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

  1. \n: Newline
  2. \t: Tab
  3. \\: Backslash
  4. \”: Double quote
  5. \’: 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

  1. Syntax Errors: Missing semicolon or braces.
  2. Runtime Errors: Division by zero.
  3. Logic Errors: Incorrect results due to flawed logic.

Debugging Steps

  1. Check syntax and spelling.
  2. Use print statements to trace execution.
  3. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.