Java Basics Guide
Topics Covered
• Java Variables
• Java Type Casting
• Java Operators
• Java Strings
• Boolean Expressions
• If-Else, Else If
• Switch Conditions
• Java User Input (Scanner)
• For Loop, Nested For Loop, For Each
• While Loop
• Do While Loop
• Exception Handling
• Arrays
About this Guide
This guide provides a beginner-friendly overview of core Java concepts.
Each topic includes clear explanations and code examples to help learners build a strong
foundation in Java programming.
It is designed for:
• Students starting with Java
• Beginners preparing for interviews
• Professionals who want a quick Java revision
LinkedIn :- https://www.linkedin.com/in/navneet-kushwaha-2b59191b5/
Java Variables
Variables are containers for storing data values.
In Java, there are different types of variables, for example:
• String - stores text, such as "Hello". String values are surrounded by double quotes
• int - stores integers (whole numbers), without decimals, such as 123 or -123
• float - stores floating point numbers, with decimals, such as 19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
• boolean - stores values with two states: true or false
➢ Syntax
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = ‘D’;
boolean myBool = true;
String my Text = “Hello”;
Java Type Casting
Type casting is when you assign a value of one primitive data type to another type.
Widening Casting
Widening casting is done automatically when passing a smaller size type to a larger size type:
Narrowing Casting
Narrowing casting must be done manually by placing the type in parentheses () in front of the value:
Java Operators
Java Strings
A String variable contains a collection of characters surrounded by double quotes:
Example :- String greeting = "Hello";
Length of String :- greeting.length();
Concatenation :-
String firstName = “Navneet”
String lastName = “Kushwaha”
System.out.println(firstName.concat(lastName));
Boolean Expression
A Boolean expression returns a boolean value: true or false.
Int x = 10;
Int y = 9;
System.out.println(x>y); // true;
If … Else
Java Conditions and If Statements
You already know that Java supports familiar comparison conditions from mathematics, such as:
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
• Equal to a == b
• Not Equal to: a != b
You can use these conditions to perform different actions for different decisions.
Java has the following conditional statements:
• Use if to specify a block of code to be executed, if a specified condition is true
• Use else to specify a block of code to be executed, if the same condition is false
• Use else if to specify a new condition to test, if the first condition is false
• Use switch to specify many alternative blocks of code to be executed
Else if Execution
Java Switch
Instead of writing many if else statements, you can use the switch statement.
The switch statement selects one of many code blocks to be executed:
Expression :-
Output :- not 1 or 2 day.
Java User Input (Scanner)
The Scanner class is used to get user input, and it is found in the java.util package.
To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation.
Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine () Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user
Output
While Loop in Java
he while loop loops through a block of code as long as a specified condition is true:
The Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true. Then it will repeat the
loop as long as the condition is true.
Java For Loop
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
Syntax
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
output : 1 2 3 4 5
Nested For Loop
It is also possible to place a loop inside another loop. This is called a nested loop.
The "inner loop" will be executed one time for each iteration of the "outer loop":
The for-each Loop
There is also a "for-each" loop, which is used exclusively to loop through elements in an array.
Syntax :-
For( type variableName: arrayName){
//code block to be executed
}
Java Break
The break keyword in Java is used to terminate the execution of a loop.
Continue Keyword in Java
The continue keyword is used to skip the current iteration of a loop (for, while, or do-while) and proceed to the next iteration.
What is Exception Handling?
• An exception is an unwanted or unexpected event that disrupts normal program flow (like dividing by zero, invalid input, file not found, etc.).
• Java uses try-catch blocks to handle exceptions gracefully without crashing the program.
Syntax of try-catch
try {
// Code that may throw an exception
} catch (ExceptionType e) {
// Code to handle exception
}
throw in Java
• The throw keyword is used to explicitly throw an exception (usually when you know a certain condition is a failure).
• It can be used with both checked and unchecked exceptions.
What is throws?
• The throws keyword indicates what exception type may be thrown by a method.
• There are many exception types available in
Java: ArithmeticException, ClassNotFoundException, ArrayIndexOutOfBoundsException, SecurityException, etc.
What is an Array in Java?
• An array is a collection of elements of the same type, stored in contiguous memory locations.
• It is a fixed-size data structure (size cannot change after creation).
• Array elements are accessed using index numbers (starting from 0).
What is an Array?
• An array is a container that stores multiple values of the same type in one variable.
• Instead of writing:
int num1 = 10;
int num2 = 20;
int num3 = 30;
We can store them in one array:
int[] numbers = {10, 20, 30};
Array Declaration Types
1. Declaration Only
int[] arr; // Recommended
int arr[]; // Also valid (C-style)
This only creates a reference, no memory is allocated yet.
2. Declaration + Memory Allocation
int[] arr = new int[5]; // creates an array of size 5
Default values are assigned (0 for int, null for objects, false for boolean).
Example:
int[] arr = new int[5]; // [0, 0, 0, 0, 0]
arr[0] = 10;
arr[1] = 20;
System.out.println(arr[0]); // 10
3. Declaration + Initialization (Shortcut)
int[] arr = {10, 20, 30, 40, 50};
Memory is created automatically, size = number of elements.
4. Declaration → Then Later Initialization
int[] arr; // declare
arr = new int[]{5, 10, 15}; // initialize later
Different Data Types in Arrays
Arrays can be of any data type.
int[] numbers = {1, 2, 3, 4}; // int array
double[] marks = {92.5, 88.0, 76.5}; // double array
char[] vowels = {'a', 'e', 'i', 'o', 'u'}; // char array
String[] names = {"Navneet", "Rahul", "Amit"}; // String array
boolean[] status = {true, false, true}; // boolean array
Accessing Array Elements
Array index starts at 0 and ends at length-1.
int[] arr = {10, 20, 30, 40};
System.out.println(arr[0]); // 10
System.out.println(arr[3]); // 40
System.out.println(arr.length); // 4
If you try arr[4] → ArrayIndexOutOfBoundsException.
Looping through Arrays
1. Normal for loop
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
2. Enhanced for-each loop
for (int num : arr) {
System.out.println(num);
}
Multi-Dimensional Arrays
An array of arrays (like a table or matrix).
Declaration
int[][] matrix = new int[3][3]; // 3x3 matrix
Initialization
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Access
System.out.println(matrix[1][2]); // 6
Loop
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
System.out.println();
Key Points for Beginners
1. Arrays store fixed size data.
2. Arrays can be of any type (primitive or objects).
3. Use .length to find size.
4. Index starts from 0.
5. For dynamic size → use ArrayList instead of Array.
Output