Java Cheat Sheet
1. Data Types
Primitive Types: byte , short , int , long , float , double , char , boolean
Non-Primitive Types: Arrays, Strings, Classes, Interfaces
2. Control Structures
If-Else: Decision-making.
if (condition) { ... } else { ... }
Switch: Multi-way branching.
switch (expression) { case value: ... break; }
Loops:
For: Fixed iteration.
for (int i = 0; i < n; i++) { ... }
While: Condition-based.
while (condition) { ... }
Do-While: Executes at least once.
do { ... } while (condition);
3. Arrays
Single-Dimensional:
int[] arr = {1, 2, 3};
Multi-Dimensional:
int[][] matrix = {{1, 2}, {3, 4}};
4. Strings
Immutable Strings:
String str = "Hello";
Common Methods:
length() , charAt() , substring() , toLowerCase() , toUpperCase()
5. Vectors
Dynamic Arrays:
Vector<Integer> v = new Vector<>();
v.add(1);
v.add(2);
6. Classes and Objects
Class Definition:
class MyClass {
int x;
void display() { ... }
}
Object Creation:
MyClass obj = new MyClass();
obj.display();
7. Inheritance
Single Inheritance:
class A { ... }
class B extends A { ... }
Method Overriding:
@Override
void methodName() { ... }
8. Packages
Creating a Package:
package myPackage;
Importing a Package:
import myPackage.ClassName;
9. Exception Handling
Try-Catch Block:
try { ... } catch (Exception e) { ... }
Throw: Manually throw exceptions.
throw new Exception("Error");
10. Multithreaded Programming
Creating a Thread:
By extending Thread :
class MyThread extends Thread {
public void run() { ... }
}
By implementing Runnable :
class MyRunnable implements Runnable {
public void run() { ... }
}
Starting a Thread:
MyThread t = new MyThread();
t.start();
11. Applets
Basic Structure:
public class MyApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello", 20, 20);
}
}
12. AWT Controls
Common Components:
Button , Label , TextField , Checkbox
Event Handling:
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { ... }
});
13. Layout Manager
FlowLayout: Default layout for components.
setLayout(new FlowLayout());
GridLayout: Arrange in a grid.
setLayout(new GridLayout(rows, cols));
14. Networking
Datagram Socket (UDP):
DatagramSocket socket = new DatagramSocket();
TCP/IP Server Socket:
ServerSocket server = new ServerSocket(port);
15. JDBC (Java Database Connectivity)
Steps to Connect:
1. Load Driver:
Class.forName("com.mysql.jdbc.Driver");
2. Establish Connection:
Connection conn = DriverManager.getConnection(url, user, pas
3. Execute Queries:
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM table");
16. Servlets
Life Cycle: init() , service() , destroy()
Example Servlet:
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletRes
res.getWriter().println("Hello, Servlet");
}
}
17. JSP (Java Server Pages)
Basic Structure:
<%@ page language="java" %>
<html>
<body>
<h1>Hello, JSP!</h1>
<p>Current Date: <%= new java.util.Date() %></p>
</body>
</html>
Tags: Directives ( <%@ ), Scriptlets ( <% ), Expressions ( <%= )