Java supports various data types to handle different kinds of data. These are categorized into primitive and non-primitive types.
Example: int age = 25; double price = 99.99; char grade = 'A';
Control structures determine the flow of program execution. Java offers conditional and looping constructs:
Example: if(score > 50) { System.out.println("Passed"); }
Arrays store multiple values of the same type.
Strings are immutable sequences of characters.
Vectors are dynamic arrays in Java.
Example: int[] marks = {90, 85, 78}; String name = "Java"; Vectorv = new Vector<>(); v.add(10);
Classes are blueprints for objects. Inheritance allows a class to
inherit properties from another class using extends
.
Example: class Animal { void sound() { System.out.println("Sound"); } } class Dog extends Animal { void bark() { System.out.println("Bark"); } }
Packages group related classes. Java provides built-in packages like
java.util
, and we can also create user-defined packages.
Example: package mypack; public class MyClass { ... }
Exception Handling manages runtime errors using try-catch blocks to prevent program crashes.
Example: try { int a = 5 / 0; } catch (ArithmeticException e) { System.out.println("Cannot divide by zero"); }
Java allows multithreading, enabling multiple threads to run concurrently using the
Thread
class or Runnable
interface.
Example: class MyThread extends Thread { public void run() { System.out.println("Thread running"); } } MyThread t = new MyThread(); t.start();
Applets are small Java programs that run in a web browser. They extend the
Applet
class and override methods like init()
, start()
,
stop()
, and paint()
.
Example: public class MyApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello Applet", 20, 20); } }
AWT (Abstract Window Toolkit) provides GUI components such as:
Example: Button b = new Button("Click Me"); Label l = new Label("Welcome"); Choice cb = new Choice(); cb.add("Option 1");
Listeners are interfaces used to handle user interactions:
Example: b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Clicked"); } });
Layout Managers control the positioning of components. Common types:
Example: setLayout(new FlowLayout()); add(new Button("OK"));
String handling in Java includes built-in methods for processing text:
length()
– returns lengthcharAt()
– returns character at indexsubstring()
– extracts substringindexOf()
– finds index of a characterequals()
– compares stringsExample: String s = "Java"; System.out.println(s.length()); System.out.println(s.charAt(1)); System.out.println(s.substring(1,3));
Java provides powerful networking capabilities through classes in the java.net
package. Two
key approaches include:
Example (Datagram): DatagramSocket ds = new DatagramSocket(); Example (TCP/IP): ServerSocket server = new ServerSocket(5000); Socket client = server.accept();
Event handling allows Java programs to respond to user interactions such as clicks, keypresses, etc. It involves event sources, listeners, and event objects.
Example: button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Button clicked"); } });
JDBC is an API that allows Java applications to interact with databases. Major components include:
DriverManager.getConnection()
Example: Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mydb", "user", "pass"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM users");
HTML is used to structure web pages. Comments are ignored by browsers and help explain code.
Example: <!-- This is a comment -->
Headers use <h1>
to <h6>
. Text styling uses <b>
,
<i>
, and <u>
.
Example: <h1>Main Heading</h1> <b>Bold</b> <i>Italic</i> <u>Underline</u>
<img>
adds images, <br>
creates line breaks,
<hr>
adds horizontal lines.
Example: <img src="logo.png" alt="Logo"> <hr><br>
<font>
was used to style fonts (deprecated in HTML5). Use entities for special
characters.
Example: <font color="blue">Blue Text</font> Copyright symbol: ©
<table>
displays tabular data. <form>
collects user input.
Example: <table border="1"> <tr><th>Name</th><th>Age</th></tr> <tr><td>Alice</td><td>22</td></tr> </table> <form> Name: <input type="text" name="name"> <input type="submit"> </form>
Image maps allow linking specific areas of an image.
Example: <img src="map.jpg" usemap="#planetmap"> <map name="planetmap"> <area shape="rect" coords="34,44,270,350" href="sun.html"> </map>
<meta>
provides metadata. <frameset>
divides browser window into
frames (deprecated in HTML5).
Example: <meta charset="UTF-8"> <frameset cols="50%,50%"> <frame src="left.html"> <frame src="right.html"> </frameset>
Common file types:
A Java Servlet is a server-side Java program that handles requests and responses in a web application. Servlets run on a web server and provide dynamic web content.
HTTP Servlets extend HttpServlet
class and handle HTTP requests via doGet()
and
doPost()
methods.
Example: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("Hello from Servlet"); }
The lifecycle of a servlet consists of:
init()
- called once when servlet is first loaded.service()
- called for each request.destroy()
- called before servlet is removed.Request parameters can be retrieved using request.getParameter("name")
.
Example: String user = request.getParameter("username");
Servlets can send HTML responses by writing to response.getWriter()
.
Example: response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<h1>Welcome User</h1>");
Session tracking allows storing user data across multiple pages using:
Servlets can connect to databases using JDBC to perform CRUD operations.
Example: Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mydb", "user", "pass"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM users");
Java Server Pages (JSP) is a server-side technology that enables the creation of dynamic, platform-independent web content. JSP combines HTML with Java code.
JSP files have a .jsp
extension and are translated into servlets at runtime. They allow embedding Java code directly into HTML using special JSP tags like <% %>
.
Example: <% out.println("Welcome to JSP!"); %>
To run JSP pages, a servlet container like Apache Tomcat is required. Pages are deployed inside the web application structure under the webapps
folder.
JSP allows inserting Java expressions, scriptlets, and declarations within HTML to generate content based on user inputs or database values.
Example: <h1>Hello <%= request.getParameter("name") %></h1>
Custom Tag Libraries allow encapsulating Java code into reusable tags. JSTL (JSP Standard Tag Library) provides common functions like loops and conditionals.
Example: <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c:if test="${user != null}"> <p>Welcome, ${user}!</p> </c:if>
Input from users is processed using request.getParameter()
, and output is sent to the browser via out
object or embedded expressions.
Example: <% String email = request.getParameter("email"); %> <p>Your email is: <%= email %></p>