JAVA Cheat Sheet
Here's a cheat sheet summarizing some of the common concepts, syntax, and features of the Java programming language:
java
// Java Cheat Sheet
// Hello World
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
// Variables
int age = 25; // Declare and initialize an integer variable
double pi = 3.14; // Declare and initialize a double variable
char letter = 'A'; // Declare and initialize a character variable
boolean isTrue = true; // Declare and initialize a boolean variable
String name = "John"; // Declare and initialize a string variable
// Constants
final int MAX_VALUE = 100; // Declare a constant variable
// Data Types
int // Integer
double // Double precision floating-point number
char // Character
boolean // Boolean
String // String (sequence of characters)
// Operators
+, -, *, /, % // Addition, Subtraction, Multiplication, Division, Modulus
++, -- // Increment, Decrement
=, +=, -=, *=, /=, %= // Assignment
// Control Flow
if (condition) {
// Code executed if condition is true
}
else if (condition2) {
// Code executed if condition2 is true
}
else {
// Code executed if no previous conditions are true
}
switch (variable) {
case value1:
// Code executed if variable is equal to value1
break;
case value2:
// Code executed if variable is equal to value2
break;
default:
// Code executed if no previous cases match
}
while (condition) {
// Code executed repeatedly while condition is true
}
do {
// Code executed at least once, then repeatedly while condition is true
} while (condition);
for (initialization; condition; increment/decrement) {
// Code executed repeatedly until condition is false
}
// Arrays
int[] numbers = {1, 2, 3}; // Array declaration and initialization
int length = numbers.length; // Length of the array
int element = numbers[index]; // Accessing an element
// Classes and Objects
class MyClass {
private int x; // Private member variable
public void setX(int value) {
x = value; // Setter method
}
public int getX() {
return x; // Getter method
}
}
MyClass obj = new MyClass(); // Object instantiation
obj.setX(5); // Accessing member functions
int value = obj.getX();
// Inheritance
class DerivedClass extends BaseClass {
// Derived class definition
}
// Interfaces
interface MyInterface {
void myMethod(); // Method declaration
}
class MyClass implements MyInterface {
public void myMethod() {
// Method implementation
}
}
// Exceptions
try {
// Code that may throw an exception
}
catch (ExceptionType e) {
// Code to handle the exception
}
finally {
// Code that always executes, regardless of whether an exception is thrown
}
// Input and Output
import java.util.Scanner; // Import Scanner class
Scanner scanner = new Scanner(System.in); // Create a Scanner object
int number = scanner.nextInt(); // Read an integer input
System.out.println("Hello, World!"); // Output to console
// Packages
import package_name.ClassName; // Import a specific class
import package_name.*; // Import all classes in a package
// Multithreading
class MyThread extends Thread {
public void run() {
// Code executed in the thread
}
}
MyThread thread = new MyThread(); // Create a thread object
thread.start(); // Start the thread
// Generics
class MyClass<T> {
T myMethod(T parameter) {
// Method implementation
return parameter;
}
}
// File Handling
import java.io.File; // Import File class
import java.io.FileReader; // Import FileReader class
import java.io.FileWriter; // Import FileWriter class
import java.io.IOException; // Import IOException class
File file = new File("filename.txt"); // Create a File object
try {
FileReader reader = new FileReader(file); // Create a FileReader object
int character = reader.read(); // Read a character from the file
reader.close(); // Close the reader
FileWriter writer = new FileWriter(file); // Create a FileWriter object
writer.write("Hello, World!"); // Write content to the file
writer.close(); // Close the writer
}
catch (IOException e) {
// Handle IOException
}
// JDBC (Java Database Connectivity)
import java.sql.Connection; // Import Connection class
import java.sql.DriverManager; // Import DriverManager class
import java.sql.Statement; // Import Statement class
import java.sql.ResultSet; // Import ResultSet class
Connection connection = DriverManager.getConnection(url, username, password); // Establish a connection
Statement statement = connection.createStatement(); // Create a Statement object
ResultSet resultSet = statement.executeQuery("SELECT * FROM table"); // Execute a query
while (resultSet.next()) {
// Process the result set
}
connection.close(); // Close the connection
// Java API Documentation
// Official documentation: https://docs.oracle.com/en/java/javase/14/docs/api/index.html
This cheat sheet covers some of the commonly used concepts, syntax, and features of the Java programming language. It serves as a handy reference for quick look-ups and reminders while programming in Java.
Comments
Post a Comment