Java Fundamentals: Variables, Operators, Control Structures, Arrays, and Classes

Java Development Kit and Runtime

JDK includes JRE plus development tools, while JRE consists of JVM and core libraries. Execution involves loading .class files into JVM for processing. Documetnation for classes and methods should use javadoc format with /** */ comments.

Variables and Data Types

Variables represent memory storage locations. The + operator performs concatenation when either operand is a string, and addition when both are numeric.

Java data types:

  • Primitive types:
    • Numeric: byte (1), short (2), int (4), long (8)
    • Floating-point: float (4), double (8)
    • Character: char (2)
    • Boolean: boolean (1)
  • Reference types: class, interface, array

Type Convesrion

Java performs automatic type promotion from smaller to larger precision types:

  • char → int → long → float → double
  • byte → short → int → long → float → double

Boolean doesn't participate in automatic conversion. Mixed-type operations promote to the highest precision type. byte, short, and char convert to int during calculations.

float result = 2 + 2.2f;  // Correct: explicit float literal
int value = (int)1.1;     // Requires explicit cast
byte data = 10;
int number = 1;
// byte storage = number;  // Error: requires casting

String Conversion

Convert primitives to strings using concatenation with empty string:

int value = 100;
String text = value + "";

Convert strings to primitives using parse methods:

String input = "123";
int converted = Integer.parseInt(input);
char firstChar = input.charAt(0);

Operators

Modulus operation: a % b = a - a / b * b. For floating-point: a % b = a - (int)a / b * b

Increment operators:

int counter = 8;
int preIncrement = ++counter;  // First increment, then assign
int postIncrement = counter++; // First assign, then increment

Logical operators include short-circuit && and || plus standard & and |. Compound assignment operators perform implicit casting:

byte data = 2;
data += 2;     // Valid: equivalent to data = (byte)(data + 2)
// data = data + 2;  // Error: requires explicit cast

Ternary operator:

int first = 59;
int second = 2;
int maximum = first > second ? first : second;

Control Structures

Conditional Statements

if (age > 18) {
    System.out.println("Adult");
} else if (age > 12) {
    System.out.println("Teen");
} else {
    System.out.println("Child");
}

Switch Statement

switch(dayCode) {
    case 1: 
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    default:
        System.out.println("Unknown day");
}

Loop Structures

For loop:

for (int index = 1; index < 10; index++) {
    System.out.println("Iteration: " + index);
}

While loop:

int count = 1;
while (count < 10) {
    System.out.println("Count: " + count);
    count++;
}

Do-while loop:

int value = 1;
do {
    System.out.println("Value: " + value);
    value++;
} while (value < 10);

Arrays

Arrays store multiple elements of the same type and are reference types.

// Array declaration and initialization
int[] numbers = new int[5];
int[] values = {1, 2, 3, 4, 5};

// Array copying
int[] source = {1, 2, 4, 5};
int[] destination = new int[source.length];
for (int i = 0; i < source.length; i++) {
    destination[i] = source[i];
}

Two-dimensional arrays:

int[][] matrix = new int[2][3];
int[][] data = {{1, 2, 3}, {4, 5, 6}};

Classes and Objects

Classes define custom data types, while objects are instances of classes.

class Animal {
    // Properties
    String name;
    int age;
    String color;
    
    // Method
    public void makeSound() {
        System.out.println("Sound produced");
    }
}

// Object creation
Animal pet = new Animal();
pet.name = "Buddy";
pet.age = 5;
pet.makeSound();

Constructors

class Person {
    String name;
    int age;
    
    // Constructor
    public Person(String personName, int personAge) {
        name = personName;
        age = personAge;
    }
}

Method Overloading

class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
    
    public double add(double a, double b) {
        return a + b;
    }
}

Variable Arguments

class MathOperations {
    public int sum(int... numbers) {
        int total = 0;
        for (int num : numbers) {
            total += num;
        }
        return total;
    }
}

Tags: java Variables Operators ControlFlow Arrays

Posted on Mon, 27 Jul 2026 17:00:27 +0000 by sgt.wolfgang