Lab Java Bloc
Lab Java Bloc
Keep this summary reading available as a reference as you progress through your course and refer to this reading as you begin coding with Java after this course!
System.out.println("Hello, World!")
/* This is a multi-line comment It can be used to explain a block of code or provide detailed information
/* int sum = 0;
This variable will hold the sum of numbers */.
about:blank 1/13
10/18/25, 9:42 AM about:blank
Creating a package
To create a package, use the package keyword at the top of your Java source file.
/src
└── com
└── example
└──>└──>
└──>└── └──>myapp
└──>└── └──>└── MyClass.java
The folder structure on your filesystem should match the package declaration. For instance, if your package is
com.example.myapp, your source file should be located in the following path example.
package com.example.library;
public class Library
{
private List <Book> books; // Private attribute to hold books
public Library()
{
books = new ArrayList<>(); // Initialize the list in the constructor
}
public void addBook(Book book) {
In Java, a class is a blueprint for creating objects and can contain methods books.add(book); // Method to add a book to the library
(functions) that define behaviors. For instance, the second line of this code }
}
displays the public class library, with methods like calculatePrice() or
applyDiscount().
about:blank 2/13
10/18/25, 9:42 AM about:blank
Creating methods
package com.example.library;
MyProject/
└── src/ # Source code goes here
└── lib/ # External libraries/JARs
└── resources/ # Configuration files, images, and others
└── doc/ # Documentation
└── test/ # Test files
As your project grows, organizing your source files into directories can keep your
code manageable. The following example illustrates typical Java organizaton.
Using imports
import java.util.List;
Use the byte data type when you need to save memory in large arrays where the memory
savings are critical, and the values are between -128 and 127.
Use the short small integers data type for numbers from −32,768 to 32,767.
Use the int integer data type to store whole numbers larger than what byte and short can
hold. This is the most commonly used integer type.
Use the long data type when you need to store very large integer values that exceed the
range of int.
Use the float when you need to store decimal numbers but do not require high precision
(for example, up to 7 decimal places).
Use the double data type when you need to store decimal numbers and require high
precision (up to 15 decimal places).
Use the char data type when you need to store a single character such as a single letter or
an initial.
about:blank 4/13
10/18/25, 9:42 AM about:blank
Use boolean when you need to represent a true/false value. Boolean is often used for
conditions and decisions.
A string data type is a sequence of characters. The string data type is very
useful for handling text in your programs.
class Car {
String color;
int year;
void displayInfo() {
System.out.println("Color: " + color + ", Year: " + year);
}
}
The reference data type class is like a blueprint for creating objects. You can see
the class identified in the first line of code.
When you create an interface, you only declare the methods without providing // The interface class
their actual code. All methods in an interface are empty by default. Here's an interface MyInterfaceClass {
void methodExampleOne();
example of an interface called MyInterfaceClass with three methods. void methodExampleTwo();
void methodExampleThree();
}
about:blank 5/13
10/18/25, 9:42 AM about:blank
enum DaysOfWeek {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
An enum is a special data type that defines a list of named values. An enum is
useful for representing fixed sets of options, such as days of the week or colors.
Description Example
The percentage symbol % performs modulus (percentage) operations. ystem.out.println("Modulus: " + (a % b)); // 0
about:blank 6/13
10/18/25, 9:42 AM about:blank
Description Example
Relational operators
You use relational operators to compare two values. These operators return a boolean result (true or false).
Description Example
The greater than symbol > checks for the greater than result.
The less than symbol < checks if the result for a less than state.
The greater than equal symbols >= compares the values to check
for a greater than or equal to result.
The less than equal symbols <= compares the values to check for System.out.println("Is a less than or equal to b? " + (a <= b)); // false
a less than or equal to result.
about:blank 7/13
10/18/25, 9:42 AM about:blank
Description Example
Logical operators
You can use logical operators to combine boolean expressions. The following code examples use boolean x = true; and boolean y = false;.
Description Example
The double ampersands && combines two boolean expressions (both must be true).
The double vertical bar symbols || used with boolean values always evaluates both expressions, even if the first one is
true.
The single exclamation point symbol ! operator in Java is the logical NOT operator. This operator is used to negate a
boolean expression, meaning it reverses the truth value.
Description Example
a = 10
a += 5
The plus sign and equal symbols combined += adds and assigns values to variables
The minus sign and equal symbols combined -= subtracts values to variables a -= 2
about:blank 8/13
10/18/25, 9:42 AM about:blank
Description Example
a *= 3
The asterisk sign and equal symbols combined *= multipies and assigns values to variables
a /= 2
The forward slash sign and equal symbols combined /= divides and assigns values to variables
a %= 4
The percentage and equal symbols combined %= take the modulus (percentage) and assigns values to the variables
Unary operators
A unary operation is a mathematical or programming operation that uses only one operand or input. Developers use unary operations to manipulate data and perform
calculations. You would use the following assignment operators to assign values to variables.
Description Example
Ternary operators
Ternary operators are a shorthand form of the conditional statement. They can use three operands.
about:blank 9/13
10/18/25, 9:42 AM about:blank
Description Example
int a = 10;
int b = 20;
int max = (a > b) ? a : b; // If a is greater than b, assign a; otherwise assign b
System.out.println("Maximum value is: " + max); // 20
Description Example
int[] numbers;
To declare an array in Java, you use the following syntax: dataType[] arrayName;. The following doce displays the data type of int and
creates an array named numbers.
Initializing an array
After you declare an array, you need to initialize the array to allocate memory for it.
Description Example
These code samples display three methods used to create an array of 5 integers. You can initialize an array using the new
keyword. You can also declare and initialize an array in a single line. Alternatively, you can create an array and directly
assign values using curly braces.
Accessing an array
about:blank 10/13
10/18/25, 9:42 AM about:blank
You can access individual elements in an array by specifying the index inside square brackets.
Description Example
System.out.println(numbers[0]);System.out.println(numbers[0]); // Outputs: 1
Description Example
Description Example
Description Example
You can use a for loop for to iterarate through and array. Here's an example that prints all elements in the for (int i = 0; i < numbers.length; i++) {
numbers array. The for loop includes this code for (int i = 0 in the following code snippet. System.out.println(numbers[i]);
}
about:blank 11/13
10/18/25, 9:42 AM about:blank
Description Example
Description Example
You can also use the enhanced for loop, known as the "for-each" loop. The enhanced for each loop code include this
portion, for (int of the following code snippet.
Description Example
int[][] matrix = {
{1,{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Here's how to declare and initialize a 2D array. You will declare the integer data type and create the
matrix array.
System.out.println(matrix[0][1]); // Outputs: 2
You can access elements in a two-dimensional array by specifying both indices, which are shown in
this code inside of the square brackets.
Description Example
about:blank 12/13
10/18/25, 9:42 AM about:blank
Author(s)
Ramanujam Srinivasan
Lavanya Thiruvali Sunderarajan
about:blank 13/13