Posts

When thread synchronization is necessary ? Explain with suitable example.

 Java Thread synchronization is necessary when we want to allow only one thread to access the shared resource. So Synchronization in Java gives the capability to control the access of multiple threads to any shared resource. //example of java synchronized method class Table{ synchronized void printTable(int n){//synchronized method for(int i=1;i<=5;i++){ System.out.println(n*i); try{ Thread.sleep(400); }catch(Exception e){System.out.println(e);} } } } class MyThread1 extends Thread{ Table t; MyThread1(Table t){ this.t=t; } public void run(){ t.printTable(5); } } class MyThread2 extends Thread{ Table t; MyThread2(Table t){ this.t=t; } public void run(){ t.printTable(100); } } public class TestSynChronization{ public static void main(String args[]){ Table obj = new Table();//only one object MyThread1 t1=new MyThread1(obj); MyThread2 t2=new MyThread2(obj); t1.start(); t2.start(); } }

How CORBA differs from RMI ? Discuss the concepts of IDL briefly.

  The difference between COBRA & RMI are as follows:-  COBRA CORBA has an implementation for many languages. It uses Interface Definition Language (IDL) to separate interface from implementation. CORBA objects are not garbage collected because it is language independent and some languages like C++ do not support garbage collection. CORBA does not support this code-sharing mechanism. CORBA passes objects by reference. CORBA is a peer-to-peer system. CORBA uses Internet Inter-ORB Protocol as its underlying remoting protocol. The responsibility of locating an object implementation falls on the Object Adapter either Basic Object Adapter or Portable Object Adapter. RMI RMI is a Java-specific technology. It uses a Java interface for implementation. RMI objects are garbage collected automatically.    RMI programs can download new classes from remote JVMs. RMI passes objects by remote reference or by value. Java RMI is a server-centric model. RMI uses the Java Remote Method Proto

What are different ways of writing servlet programs ? Write a sample Servlet program using any one way.

   Writing Servlets Program There are three ways to create the servlet 1. By implementing the Servlet interface 2. By inheriting the GenericServlet class 3. By inheriting the HttpServlet class 1.  By implementing the Servlet interface/ Servlet Interface The Servlet interface provides common behavior to all the servlets. The servlet interface needs to be implemented for creating any servlet (either directly or indirectly). It provides 3 life cycle methods that are used to initialize the servlet, service the requests, and destroy the servlet, and 2 non-life cycle methods that are used to get servlet information and servlet configurations. Example import java.io.*; import javax.servlet.*; public class ServletInt implements Servlet { ServletConfig config=null; public void init(ServletConfig config) { this.config=config; System.out.println("servlet is initialized"); } public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException { res.setContent

How JavaFX differs from Swing ? explain steps of creating GUI using JavaFX.

  JavaFX vs. SWING Swing, AWT, and JavaFX all are a part of JDK and are used to create Graphical User Interface (GUI) with JavaFX being one of the latest entrants in this list. Key differences between JavaFX and Swing are provided below. Swing 1. Swing is the standard toolkit for Java developers in creating GUI 2. Swing has a more sophisticated set of GUI components 3. Swing is a legacy library that fully features and provides pluggable UI components 4. Swing has a UI component library and acts as a legacy 5. Swing does not have support for customization using CSS and XML 6. With Swing, it is very difficult to create beautiful 3-D applications. JavaFX 1. JavaFX provides platform support for creating desktop applications. 2. JavaFX has a decent number of UI components available but lesser than what Swing provides. 3. JavaFX has UI components that are still evolving with a more advanced look and feel. 4. JavaFX has several components built over Swing 5. JavaFX has support for customizati

What causes SQL exception ? How it can be handled ? Explain with example.

 SQL exceptions can occur either if the driver is missing or information about the database is wrong or the SQL query is wrong. SQL exception can be handled by utilizing the information available from the Exception object, which can be caught with an exception. Example import java.sql.*; public class DemoSQLException { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/emp"; static final String user = "root"; static final String pass = "$Arjun12"; public static void main(String[] args) { Connection connection = null; PreparedStatement statement = null; try { Class.forName(JDBC_DRIVER); connection = DriverManager.getConnection(DB_URL, user, pass); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("select employee"); //Invalid SQL Statement while (rs.next()) System.out.println(rs.getInt(1) + " " + rs.getString(2) ); connection.close(); } c

What is the use of action command in event handling ? Explain with example.

Action command is used to handle event caused by the Buttons. Example  import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ActionDemo extends JFrame implements ActionListener { JLabel l1, l2, l3; JTextField t1, t2, t3; JButton b1, b2; public ActionDemo() { super("Handling Action Event"); l1 = new JLabel("Click on button to get result"); t1 = new JTextField(20); b1 = new JButton("Demo Button"); b1.addActionListener(this); setLayout(new FlowLayout(FlowLayout.CENTER, 150, 10)); add(l1); add(t1); add(b1); setSize(400, 300); setVisible(true); } public void actionPerformed(ActionEvent actionEvent) { String str = actionEvent.getActionCommand(); //using action command t1.setText("You have clicked " + str); } public static void main(String[] args) { new ActionDemo(); } } In the above example if clicked on the Demo Button, with the help of the getActionCommand() method we will be ab

What are layout managers? Explain Gridbag layout with suitable example.

Image
  A layout manager   A layout manager is an object that controls the size and position of the components in the container. Every container object has a layout manager object that controls its layout. Actually, layout managers are used to arranging the components in a specific manner.  It is an interface that is implemented by all the classes of layout managers.  There are some classes that represent the layout managers.  The LayoutManagers are used to arrange components in a particular manner. The Java LayoutManagers facilitate us to control the positioning and size of the components in GUI forms. LayoutManager is an interface that is implemented by all the classes of layout managers. Some of the most commonly used layout managers of java are : Flow Layout Border Layout Grid Layout Gridbag layout Card Layout Group Layout G ridbag layout The Gridbag Layout is a flexible layout manager that aligns components vertically and horizontally, without requiring that the components be of the sam