Java Note
Java Note
import java.util.Scanner*;
// Class declaration
public class ClassName {
// Fields (variables)
private int field1;
private String field2;
// Constructor
public ClassName() {
// Initialization code
}
// Methods
public void someMethod() {
// Method code
}
// Read a String
String name = scanner.nextLine();
// Read an integer
int age = scanner.nextInt();
// Read a double
double gpa = scanner.nextDouble();
// Read a boolean
boolean isFullTime = scanner.nextBoolean();
Loops
For :
While:-
while (condition) {
// Code to be executed
}
int i = 0;
while (i < 5) {
System.out.println(i); // Prints numbers from 0 to 4
i++;
}
do – while :-
do {
// Code to be executed
} while (condition);
int i = 0;
do {
System.out.println(i); // Prints numbers from 0 to 4
i++;
} while (i < 5);
File handling
Key Classes in File Handling:
1.File Class: Used to create, delete, and get
information about a file or directory.
2.FileWriter Class: Used to write data to a file.
3.FileReader Class: Used to read data from a
file.
4.BufferedReader & BufferedWriter: Used to
efficiently read from or write to a file.
These libraries needs to be import
(When creates file/ Delete file)
import java.io.File;
5. 1. Creating a File
6. The File class can be used to create a file.
Writing
FileWriter writer = new FileWriter("filename.txt");
writer.write("Hello, World!");
writer.close();
Behavior:
1. File Doesn't Exist: If "example.txt" doesn't exist, FileWriter will create it.
2. File Already Exists: If the file exists, the contents will be overwritten (unless appending
mode is specified).
Appending Mode:
To prevent overwriting and append data to the file instead, you can use FileWriter in append
mode:
Reading
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileReader;
Using throws:
If you don't want to handle the exception in your method, you can pass the
responsibility to the calling method using the throws keyword.
import java.io.FileReader;
import java.io.IOException;
String Class
Immutable: Once a String object is created, it cannot be changed. Any
operation that modifies a string creates a new object.
String Creation:
charAt(int index):
String Builder
Declaration:
Common Methods:
replace(start, end, str): Replaces the characters between start and end
with the given string.
sb.replace(5, 10, "Java"); // sb = "Hello Java"
Arrays
2-D Arrays
Array List
Key Points:
Generic: You can specify the type of elements it stores, ensuring type
safety (e.g., ArrayList<Integer>, ArrayList<String>).
ArrayLists are widely used for dynamic data storage when the number of
elements can vary during runtime.
Map
Key Points:
Efficiency: HashMap offers constant-time performance (O(1)) for basic operations like
put() and get().
Sorted Map: Use TreeMap if you need sorted keys.
Iteration Order: Use LinkedHashMap to maintain insertion order.
Map is a powerful data structure for associating keys with values, and it is widely used for
lookups, counting occurrences, or representing relationships between entities.
Speed comparison
Time
Definition: An algorithm has O(n) time complexity if its running time grows linearly with the
size of the input. In other words, if the input size doubles, the running time will also double.