Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Java Cheat Sheet

This Java cheat sheet provides a quick reference for fundamental concepts including data types, operators, control structures, loops, functions, arrays, and input/output. It also includes common coding patterns and exam tips for Java programming. Key elements such as syntax rules and method examples are highlighted for easy understanding.

Uploaded by

aneeba.naveed
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)
3 views2 pages

Java Cheat Sheet

This Java cheat sheet provides a quick reference for fundamental concepts including data types, operators, control structures, loops, functions, arrays, and input/output. It also includes common coding patterns and exam tips for Java programming. Key elements such as syntax rules and method examples are highlighted for easy understanding.

Uploaded by

aneeba.naveed
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 Cheat Sheet

1. Data Types
int → whole numbers (int x = 5;)
double → decimals (double pi = 3.14;)
char → one character (char letter = 'A';)
String → text (String name = "Aneeba";)
boolean → true/false (boolean flag = true;)

2. Operators
Arithmetic: +, -, *, /, %
Relational: ==, !=, <, <=, >, >=
Logical: && (and), || (or), ! (not)

3. If/Else Template
if (condition) {
// do this
} else if (anotherCondition) {
// do this instead
} else {
// fallback
}

4. Loops
For loop:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
While loop:
while (condition) {
// keep running
}
Do-While loop:
do {
// run once
} while (condition);

5. Functions (Methods)
public static int doubleIt(int x) {
return x * 2;
}
Call: int result = doubleIt(5); // 10

6. Arrays
int[] numbers = {10, 20, 30};
System.out.println(numbers[0]); // 10
numbers[2] = 99; // change value

7. Input/Output
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.println("Hello " + name);

8. Common Patterns
Sum of array:
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
Find max:
int max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}

■ Exam Tips
Always end statements with ;
Curly braces {} are mandatory for blocks
System.out.println() for output
== for comparing values, .equals() for Strings

You might also like