Sure, I can provide you with a concise Java cheat sheet. Here are some key points:
1. **Hello World**:
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
2. **Variables**:
```java
int age = 25;
double price = 19.99;
String name = "John";
boolean isTrue = true;
```
3. **Conditional Statements**:
```java
if (condition) {
// Code block executed if the condition is true
} else if (anotherCondition) {
// Code block executed if anotherCondition is true
} else {
// Code block executed if none of the above conditions are true
}
```
4. **Loops**:
```java
for (int i = 0; i < 5; i++) {
// Code block executed 5 times
}
while (condition) {
// Code block executed while the condition is true
}
do {
// Code block executed at least once, then repeatedly while the condition is true
} while (condition);
```
5. **Arrays**:
```java
int[] numbers = {1, 2, 3, 4, 5};
String[] fruits = new String[3];
fruits[0] = "Apple";
fruits[1] = "Banana";
fruits[2] = "Orange";
```
6. **Methods**:
```java
public int add(int a, int b) {
return a + b;
}
```
7. **Classes and Objects**:
```java
public class Car {
String make;
String model;
public Car(String make, String model) {
this.make = make;
this.model = model;
}
}
Car myCar = new Car("Toyota", "Corolla");
```
8. **Inheritance**:
```java
public class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
public class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
```
9. **Exception Handling**:
```java
try {
// Code that may throw an exception
} catch (ExceptionType e) {
// Code to handle the exception
} finally {
// Code that runs regardless of whether an exception was thrown
}
```
10. **Interfaces**:
```java
interface Shape {
void draw();
}
class Circle implements Shape {
public void draw() {
// Code to draw a circle
}
}
```
Remember, this is just a concise overview. Java is a vast language with many features, but this cheat sheet should cover the basics to get you started. If you have any specific questions or need more details, feel free to ask!
Comments
Post a Comment