Java Notes By Rayyaan Shetty XC
Java Revision Sheet
Basics
import [Link].*; // always add for Scanner and Arrays
Scanner sc = new Scanner([Link]);
int x = [Link]();
String s = [Link]();
double d = [Link]();
Data Types
Primitive Data Types:
int → whole numbers (e.g., 5, -3)
double → decimal numbers (e.g., 3.14, -2.0)
char → single character (e.g., 'A', '5')
boolean → true or false
float → smaller decimal type (e.g., 2.3f)
byte → small integer (-128 to 127)
short → small integer range
long → large integer
Non-primitive (Objects):
String → text, group of characters (e.g., "Hello")
Array → group of same data type elements
Data-Type Conversion
String → int : [Link]("25") // gives 25
int → String : [Link](25) // gives "25"
String → double : [Link]("3.14") // gives 3.14
double → int : (int) 3.9 // gives 3
char → int : (int) 'A' // gives 65
int → char : (char) 65 // gives 'A'
Page 1 of 2
Java Notes By Rayyaan Shetty XC
String Methods You Actually Use
[Link]() → length of string
[Link](i) → character at index i
[Link]() → convert to uppercase
[Link]() → convert to lowercase
[Link]() → remove spaces at ends
[Link](s2) → check equality (case-sensitive)
[Link](s2) → equality ignoring case
[Link]("A") → check beginning
[Link]("z") → check ending
[Link]("ab") → check if substring exists
[Link](0,3) → slice (start inclusive, end exclusive)
[Link]("a","@") → replace text
[Link]("a") → first occurrence
[Link]("a") → last occurrence
Bubble Sort (1D Array)
for(int i = 0; i < [Link] - 1; i++) {
for(int j = 0; j < [Link] - 1 - i; j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
2D Array
int[][] matrix = new int[3][3];
// Input
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
matrix[i][j] = [Link]();
}
}
// Display
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
[Link](matrix[i][j] + "\t");
}
[Link]();
}
Page 2 of 2