0% found this document useful (0 votes)
6 views2 pages

Java Notes

Uploaded by

BOOKreads
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Java Notes

Uploaded by

BOOKreads
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Notes By Rayyaan Shetty XC

Java Revision Sheet


Basics
import java.util.*; // always add for Scanner and Arrays
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
String s = sc.nextLine();
double d = sc.nextDouble();

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 : Integer.parseInt("25") // gives 25
int → String : String.valueOf(25) // gives "25"
String → double : Double.parseDouble("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


s.length() → length of string
s.charAt(i) → character at index i
s.toUpperCase() → convert to uppercase
s.toLowerCase() → convert to lowercase
s.trim() → remove spaces at ends
s.equals(s2) → check equality (case-sensitive)
s.equalsIgnoreCase(s2) → equality ignoring case
s.startsWith("A") → check beginning
s.endsWith("z") → check ending
s.contains("ab") → check if substring exists
s.substring(0,3) → slice (start inclusive, end exclusive)
s.replace("a","@") → replace text
s.indexOf("a") → first occurrence
s.lastIndexOf("a") → last occurrence

Bubble Sort (1D Array)


for(int i = 0; i < arr.length - 1; i++) {
for(int j = 0; j < arr.length - 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] = sc.nextInt();
}
}
// Display
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}

Page 2 of 2

You might also like