Java 1.1 Features


Java 1.1 was released in 1997 and introduced several new features and improvements to the language. Some of the notable features of Java 1.1 are:

1. Inner classes: 

Java 1.1 introduced the concept of inner classes, which are classes defined within other classes. Inner classes can be used to encapsulate implementation details, improve code organization, and provide better control over access to class members.

here's an example of using inner classes in Java 1.1


public class OuterClass {
    private int x = 10;

    public void outerMethod() {
        InnerClass inner = new InnerClass();
        inner.innerMethod();
    }

    public class InnerClass {
        public void innerMethod() {
            System.out.println("The value of x is: " + x);
        }
    }

    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        outer.outerMethod();
    }
}


In this example, we define an OuterClass that contains an inner class named InnerClass. The OuterClass has a private variable x with a value of 10, and a method named outerMethod() that creates an instance of InnerClass and calls its innerMethod().

The InnerClass has a single method named innerMethod() that prints the value of x.

In the main() method, we create an instance of OuterClass and call its outerMethod(). This will create an instance of InnerClass and call its innerMethod(), which will print the value of x.

Note that in Java 1.1, inner classes had to be declared as non-static inner classes, which means that they are associated with an instance of the outer class. This allows the inner class to access the private members of the outer class. In later versions of Java, static inner classes were introduced, which can be declared without an instance of the outer class.

2. JDBC: 

Java Database Connectivity (JDBC) API was introduced in Java 1.1, which provides a standard API for connecting to databases and executing SQL queries. JDBC made it easier for Java developers to interact with relational databases.

example of using JDBC in Java 1.1 to connect to a MySQL database


import java.sql.*;

public class JdbcExample {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            // Load the JDBC driver for MySQL
            Class.forName("com.mysql.jdbc.Driver");

            // Establish a connection to the database
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");

            // Create a statement object to execute SQL queries
            stmt = conn.createStatement();

            // Execute a SELECT statement and retrieve the results
            rs = stmt.executeQuery("SELECT * FROM mytable");

            // Process the results
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                System.out.println("ID: " + id + ", Name: " + name);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            // Release resources
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}


In this example, we first load the MySQL JDBC driver using Class.forName(). Then, we establish a connection to a MySQL database using DriverManager.getConnection(), passing in the database URL, username, and password.

Next, we create a Statement object using conn.createStatement(), which we use to execute an SQL SELECT statement on a table named mytable. We then retrieve the results of the query using rs = stmt.executeQuery(), and process the results using a while loop.

Finally, we release the resources by closing the ResultSet, Statement, and Connection objects in a finally block to ensure they are properly cleaned up even if an exception occurs.

Note that this example assumes that you have a MySQL database running locally on port 3306 with a table named mytable containing columns id and name. You will need to replace the database URL, username, and password with your own values to connect to your own database.

3.Reflection API: 

Java 1.1 added the Reflection API, which allows Java programs to introspect on their own structures and manipulate classes, methods, and fields at runtime. Reflection is useful for developing tools such as debuggers, profilers, and code generators.

 here's an example of using the Reflection API in Java 1.1


import java.lang.reflect.*;

public class ReflectionExample {
    public static void main(String[] args) throws Exception {
        // Get the class object for a String
        Class stringClass = String.class;

        // Get the constructor for the String class that takes a char array as an argument
        Constructor constructor = stringClass.getConstructor(new Class[] { char[].class });

        // Create a new String object using the constructor
        String str = (String) constructor.newInstance(new Object[] { new char[] { 'h', 'e', 'l', 'l', 'o' } });

        // Get the method named "length" for the String class
        Method lengthMethod = stringClass.getMethod("length", new Class[] {});

        // Call the length method on the String object
        int length = (Integer) lengthMethod.invoke(str, new Object[] {});

        // Print the length of the String object
        System.out.println("Length of string: " + length);
    }
}


In this example, we use the Reflection API to dynamically create a new String object and call its length() method.

First, we get the Class object for the String class using String.class. Then, we use the getConstructor() method of the Class object to get the constructor that takes a char[] argument. We create a new String object using this constructor by calling newInstance() on the Constructor object.

Next, we use the getMethod() method of the Class object to get the length() method of the String class. We then call this method on the String object using the invoke() method of the Method object, passing in the String object as the first argument and an empty array as the second argument (since the length() method takes no arguments).

Finally, we print the length of the String object to the console.

4. RMI:

 Remote Method Invocation (RMI) was introduced in Java 1.1, which allows Java objects to be invoked remotely over a network. RMI made it easier to develop distributed applications in Java.

here's an example of using RMI (Remote Method Invocation) in Java 1.1:

First, let's create a simple Remote interface that defines a single method that takes a String argument and returns a String:


import java.rmi.*;

public interface MyRemote extends Remote {
    public String sayHello(String name) throws RemoteException;
}

Next, let's create a class that implements the MyRemote interface:


import java.rmi.*;
import java.rmi.server.*;

public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
    public MyRemoteImpl() throws RemoteException {}

    public String sayHello(String name) throws RemoteException {
        return "Hello, " + name + "!";
    }
}
 

In this example, we extend the UnicastRemoteObject class to create a remote object that implements the MyRemote interface. We provide an implementation for the sayHello() method that simply returns a greeting string with the provided name.

Finally, we need to create a server that exports the MyRemoteImpl object and registers it with the RMI registry. Here's an example:


import java.rmi.*;

public class MyRemoteServer {
    public static void main(String[] args) {
        try {
            // Create a remote object
            MyRemote remoteObj = new MyRemoteImpl();

            // Export the remote object and bind it to a name in the RMI registry
            Naming.rebind("MyRemoteObject", remoteObj);

            System.out.println("Server ready...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



In this example, we create an instance of the MyRemoteImpl class and use the Naming.rebind() method to register it with the RMI registry under the name "MyRemoteObject". The Naming.rebind() method exports the remote object and binds it to the specified name in the RMI registry.

Once the server is running, we can create a client that looks up the remote object in the RMI registry and invokes its methods. Here's an example:


import java.rmi.*;

public class MyRemoteClient {
    public static void main(String[] args) {
        try {
            // Look up the remote object in the RMI registry
            MyRemote remoteObj = (MyRemote) Naming.lookup("MyRemoteObject");

            // Call the remote method and print the result
            String result = remoteObj.sayHello("Alice");
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

In this example, we use the Naming.lookup() method to look up the remote object in the RMI registry by its name ("MyRemoteObject"). We then cast the returned object to the MyRemote interface type and call its sayHello() method with the argument "Alice". Finally, we print the result ("Hello, Alice!").

5. Collections framework: 

Java 1.1 added the Collections framework, which provides a set of interfaces and classes for representing and manipulating collections of objects. The Collections framework made it easier to work with data structures such as lists, sets, and maps.

here is an example of how to use lists, sets, and maps in Java 1.1:

Lists:

Lists in Java 1.1 were implemented using the Vector class, which is part of the java.util package. Here is an example of how to create a list of strings using Vector:


import java.util.Vector;

public class ListExample {
    public static void main(String[] args) {
        Vector<String> names = new Vector<String>();
        
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        
        System.out.println("List of names:");
        for (int i = 0; i < names.size(); i++) {
            System.out.println(names.get(i));
        }
    }
}

This code creates a list of strings using the Vector class, adds three names to the list, and then prints out the contents of the list using a for loop.

Sets:

Sets in Java 1.1 were implemented using the HashSet class, which is part of the java.util package. Here is an example of how to create a set of integers using HashSet:


import java.util.HashSet;
import java.util.Iterator;

public class SetExample {
    public static void main(String[] args) {
        HashSet<Integer> numbers = new HashSet<Integer>();
        
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(2); // adding duplicate element
        
        System.out.println("Set of numbers:");
        Iterator<Integer> iterator = numbers.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

This code creates a set of integers using the HashSet class, adds four numbers to the set (one of which is a duplicate), and then prints out the contents of the set using an iterator.

Maps:

Maps in Java 1.1 were implemented using the Hashtable class, which is part of the java.util package. Here is an example of how to create a map of strings to integers using Hashtable:


import java.util.Hashtable;
import java.util.Enumeration;

public class MapExample {
    public static void main(String[] args) {
        Hashtable<String, Integer> ages = new Hashtable<String, Integer>();
        
        ages.put("Alice", 25);
        ages.put("Bob", 30);
        ages.put("Charlie", 35);
        
        System.out.println("Map of ages:");
        Enumeration<String> keys = ages.keys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            System.out.println(key + ": " + ages.get(key));
        }
    }
}


This code creates a map of strings to integers using the Hashtable class, adds three key-value pairs to the map, and then prints out the contents of the map using an enumeration.

6. Swing: 

Java 1.1 introduced the Swing framework, which provides a set of GUI components that are platform-independent and highly customizable. Swing made it easier to develop cross-platform graphical applications in Java.

Here's an example of a simple Swing application in Java 1.1 that creates a window with a button:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingExample extends JFrame {
    private JButton button;

    public SwingExample() {
        setTitle("Swing Example");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        button = new JButton("Click me!");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(SwingExample.this, "Hello, world!");
            }
        });

        getContentPane().add(button, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        SwingExample example = new SwingExample();
        example.setVisible(true);
    }
}


In this example, we create a class called SwingExample that extends the JFrame class to create a window. We set the window's title, size, and location, and specify that the application should exit when the window is closed.

We create a JButton object and add an ActionListener to it that displays a message when the button is clicked using a JOptionPane.

We then add the button to the window's content pane using the getContentPane() method and the BorderLayout class.

Finally, we create an instance of the SwingExample class and make it visible by calling the setVisible() method.

Note that Java 1.1's Swing API had some differences compared to modern Swing, so this example may not work exactly the same way in a newer version of Java.

Overall, Java 1.1 introduced several significant features and improvements that helped to establish Java as a popular language for enterprise development.

Post a Comment

0 Comments