Essential Java Programming Concepts and Interview Questions

1. Event Handling Model in Java

Event Handling is a mechanism that controls events generated by user actions such as mouse clicks, key presses, or button clicks. Java utilizes the Delegation Event Model.

Components of Event Handling

  • Event Source: The object that generates an event (e.g., Button, TextField, Frame).
  • Event Object: An object containing information about the event (e.g., ActionEvent, MouseEvent, KeyEvent).
  • Event Listener: An interface that receives and handles events (e.g., ActionListener, MouseListener).

Steps for Event Handling

  1. Create GUI components.
  2. Implement the listener interface.
  3. Register the listener with the event source using methods like addActionListener() (e.g., button.addActionListener(this);).

2. Layout Manager in Java GUI

A Layout Manager controls the position and size of GUI components inside a container. It ensures the GUI is flexible, platform-independent, and adjusts automatically when the window size changes.

Types of Layout Managers

  • FlowLayout, BorderLayout, GridLayout, CardLayout, GridBagLayout

Example

Frame f = new Frame();
f.setLayout(new FlowLayout());
Button b = new Button("OK");
f.add(b);

3. ResultSet in JDBC

A ResultSet is an object that stores data returned from a database query, allowing programs to read records row by row.

Types of ResultSet

  • TYPE_FORWARD_ONLY: Cursor moves only forward.
  • TYPE_SCROLL_INSENSITIVE: Cursor moves forward and backward; does not reflect database changes.
  • TYPE_SCROLL_SENSITIVE: Cursor moves forward and backward; reflects database changes.

4. JavaBeans and Persistence

A JavaBean is a reusable software component following specific conventions: it must have a default constructor, implement the Serializable interface, and use getter/setter methods.

Persistence is the process of saving the state of a bean, achieved through Serialization, which stores object data for later restoration.

5. JSP vs. Servlet

  • JSP: Used for the presentation layer; HTML-centric; easier to write; automatically converted to a servlet.
  • Servlet: Used for business logic; Java-centric; more complex; is a standard Java class.

JSP Script Tags

  • Declaration Tag: <%! int a = 10; %> (Declare variables/methods).
  • Scriptlet Tag: <% out.println("Hello"); %> (Write Java code).
  • Expression Tag: <%= 5+5 %> (Print values directly).

6. Struts Action and RMI Registry

Struts is a Java web framework based on MVC architecture. An Action class handles user requests and processes business logic.

RMI Registry is a naming service used in Remote Method Invocation (RMI) that allows clients to locate remote objects using Naming.lookup().

Short Notes

1. Declaration Tag in JSP

Used to declare variables and methods that become part of the generated servlet class. Syntax: <%! declaration %>. These are defined inside the servlet class but outside the service() method.

2. Graphics Class

Used for drawing shapes, text, and images in java.awt. Common methods include drawLine(), drawRect(), drawOval(), and drawString().

3. RowSet in JDBC

An advanced version of ResultSet. Connected RowSet (e.g., JdbcRowSet) maintains a database connection, while Disconnected RowSet (e.g., CachedRowSet) works without an active connection.

Additional Concepts

1. WindowEvent Listener

Handles window-related events (opening, closing, minimizing). Methods include windowOpened(), windowClosing(), windowClosed(), windowActivated(), windowDeactivated(), windowIconified(), and windowDeiconified().

2. HttpServletRequest and Response

  • HttpServletRequest: Used to receive client data via getParameter(), getHeader(), and getCookies().
  • HttpServletResponse: Used to send data back to the client via setContentType(), getWriter(), and sendRedirect().

3. RMI vs. CORBA

  • RMI: Java-based, works only with Java, easier to implement.
  • CORBA: Language-independent, uses IDL, more complex, used in heterogeneous systems.