0% found this document useful (0 votes)
2 views

corejava

Uploaded by

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

corejava

Uploaded by

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

Core Java Cheat Sheet

1. Basic Syntax
java
// Hello World Program
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Data Types
java
int age = 25;
double price = 10.5;
char grade = 'A';
boolean isJavaFun = true;
String name = "Praveen";
3. Operators
java
int sum = 10 + 5; // Addition
int diff = 10 - 5; // Subtraction
int product = 10 * 5; // Multiplication
int quotient = 10 / 5; // Division
int remainder = 10 % 5; // Modulus
4. Conditional Statements
java
if (age > 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
5. Loops
java
// For Loop
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}

// While Loop
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}

// Do-While Loop
int j = 1;
do {
System.out.println(j);
j++;
} while (j <= 5);
6. Arrays
java
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[0]); // Prints 1
7. Methods
java
public class Main {
static int add(int a, int b) {
return a + b;
}

public static void main(String[] args) {


System.out.println(add(5, 10)); // Prints 15
}
}
8. Object-Oriented Programming
java
class Person {
String name;

Person(String name) {
this.name = name;
}

void display() {
System.out.println("Name: " + name);
}
}

public class Main {


public static void main(String[] args) {
Person p = new Person("Praveen");
p.display();
}
}
9. Exception Handling
java
try {
int num = 10 / 0; // This will cause an exception
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero");
} finally {
System.out.println("Execution Completed");
}
10. Multithreading
java
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
}

public class Main {


public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}

You might also like