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

Java Cheat Sheet

This Java Cheat Sheet provides an overview of essential Java concepts including data types, control structures, arrays, strings, classes, inheritance, exception handling, multithreading, applets, AWT controls, networking, JDBC, servlets, and JSP. It includes syntax examples for each topic, such as primitive and non-primitive data types, control flow statements, and methods for handling exceptions. The document serves as a quick reference for Java programming fundamentals and common practices.

Uploaded by

utkarshbedii
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)
21 views

Java Cheat Sheet

This Java Cheat Sheet provides an overview of essential Java concepts including data types, control structures, arrays, strings, classes, inheritance, exception handling, multithreading, applets, AWT controls, networking, JDBC, servlets, and JSP. It includes syntax examples for each topic, such as primitive and non-primitive data types, control flow statements, and methods for handling exceptions. The document serves as a quick reference for Java programming fundamentals and common practices.

Uploaded by

utkarshbedii
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/ 5

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 ( <%= )

You might also like