Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Java Reviewer (Markor)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Reviewer (Markor)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Exam Reviewer

1. Strings in Java

Definition: Strings are objects of the String class in Java, used to store text. Strings are immutable (cannot be changed once created).

Creating Strings

String s1 = "Welcome"; // Using literals


String s2 = new String("Welcome"); // Using the 'new' keyword

Useful Methods

length() – Returns the length of the string.

System.out.println(s1.length()); // Output: 7

toUpperCase() / toLowerCase() – Converts case.

System.out.println(s1.toUpperCase()); // Output: WELCOME

trim() – Removes white spaces before/after the string.

compareTo(String) – Lexicographically compares two strings.


Returns:

Negative: First string is smaller.


Zero: Strings are equal.
Positive: First string is greater.

StringBuilder – Mutable version of String.

StringBuilder sb = new StringBuilder("Hello");


sb.append(" World");
System.out.println(sb); // Output: Hello World

2. Loops in Java

a. While Loop

Syntax:

while (condition) {
// Code block
}

Example:

int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}

b. Do-While Loop

Definition: Executes the block at least once.

Syntax:

do {
// Code block
} while (condition);

Example:
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);

c. For Loop

Syntax:

for (initialization; condition; update) {


// Code block
}

Example:

for (int i = 0; i < 5; i++) {


System.out.println(i);
}

d. For-Each Loop

Definition: Used exclusively to iterate over arrays.

Syntax:

for (type variableName : arrayName) {


// Code block
}

Example:

String[] cars = {"Volvo", "BMW", "Ford"};


for (String car : cars) {
System.out.println(car);
}

3. Arrays

Definition: Arrays are fixed-size, ordered collections of the same data type.

Syntax:

int[] numbers = new int[5]; // Array declaration


numbers[0] = 1; // Assigning value

Ragged Arrays

int[][] raggedArray = new int[3][];


raggedArray[0] = new int[4];
raggedArray[1] = new int[2];

Common Operations

1. Access:

System.out.println(numbers[0]);

2. Length:

System.out.println(numbers.length);

Multi-Dimensional Arrays

Syntax:
int[][] matrix = new int[3][3];

Accessing Elements:

System.out.println(matrix[0][1]);

4. Methods

Definition: Reusable blocks of code identified by a name.

Syntax:

returnType methodName(parameters) {
// Code block
}

Main Method

public static void main(String[] args) {


// Entry point
}

Common Methods

1. indexOf(String) – Returns the position of a substring.

System.out.println("Programming".indexOf("gram")); // Output: 3

2. charAt(int) – Returns the character at a specific position.

System.out.println("Java".charAt(1)); // Output: a

5. Key Terms

Immutable: Strings cannot be changed after creation.


Mutable: Objects like StringBuilder allow modifications.

You might also like