002 (6 1st Module Topic

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 6

Remote Method Invocation:

Remote Method Invocation (RMI) is a Java technology that


allows objects to communicate with each other in a distributed
environment. It enables a Java program to invoke methods on
remote objects located on different machines, as if they were
local objects. Swing applets are graphical user interface
(GUI) components that can be embedded in a web page and
executed on the client-side. To use RMI in a Swing applet, you
need to follow these steps: 1.Create a remote interface that
extends the java.rmi.Remote interface and declares the methods
that can be invoked remotely. The interface and its
implementations should be available to both the server and
client machines. 2.Implement the remote interface on the
server side, which involves creating a class that implements
the methods declared in the remote interface.3.Register the
server object with the RMI registry on the server
machine, which allows the client to locate and invoke the
remote methods. 4.On the client side, create a stub object
that implements the same remote interface as the server
object. The stub object is responsible for sending method
calls to the server object over the network.
5.Use the stub object on the client side to invoke the remote
methods on the server object, as if it were a local object.
Here's an example of how to use RMI in a Swing applet:
import java.rmi.*;
import javax.swing.*;
public class RemoteApplet extends JApplet {
private RemoteObject remoteObj;
public void init() {
try {
String serverURL = "rmi://localhost/RemoteObject";
remoteObj = (RemoteObject)Naming.lookup(serverURL);
String result = remoteObj.sayHello("World");
JOptionPane.showMessageDialog(this, result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
In this example, the RemoteApplet class extends the JApplet
class and implements the init() method. The init() method
creates a stub object for the RemoteObject class and invokes
the sayHello() method on the server object, passing it the
string "World". The result of the method call is then
displayed in a dialog box using the JOptionPane class.
Note that to use RMI in a Swing applet, you need to sign the
applet and grant it the necessary permissions to access the
network and use RMI.
Simple Type Wrappers
Simple type wrappers in Java are classes that provide an
object#oriented representation of the eight primitive data
types in Java: boolean, byte, char, short, int, long, float,
and double. These classes are used to represent primitive
values as objects and provide additional functionality, such
as methods for converting between different types, and for
performing mathematical operations.Here are the simple type
wrappers in Java: Boolean: Represents a boolean value (true or
false) as an object. Byte: Represents an 8-bit signed integer
value (-128 to 127) as an object. Character: Represents a
single Unicode character as an object.Short: Represents a 16-
bit signed integer value (-32,768 to 32,767) as an object.
Integer: Represents a 32-bit signed integer value (-2^31 to
2^31-1) as an object. Long: Represents a 64-bit signed integer
value (-2^63 to 2^63-1) as an object. Float: Represents a
single-precision 32-bit floating-point value
as an object. Double: Represents a double-precision 64-bit
floating-point value as an object.All of these classes have a
corresponding static method that can be used to convert a
string representation of the value to the corresponding
object. For example, the Integer class has a static method
called parseInt() that can be used to convert a
string to an integer.In addition to these simple type
wrappers, there is also a Void class, which is used to
represent the absence of a value. It is often used as the
return type for methods that do not return a value.
--Object Fusing using clone() and the cloneable interface
In Java, the Object class provides a method called clone()
that can be used to create a copy of an object. However, not
all objects can be cloned, as the clone() method is protected
by default and must be overridden in the subclass of an object
in order to make it cloneable. In order to indicate that an
object can be cloned, the Cloneable interface must be
implemented.The Cloneable interface does not have any methods,
but it serves as a marker interface, indicating that the
object implementing it can be cloned. If an attempt is made to
clone an object that does not implement Cloneable, a
CloneNotSupportedException will be thrown at runtime.When the
clone() method is called on an object that implements
Cloneable, a new instance of the object is created with the
same values as the original object, and the new instance is
returned. The new instance is a shallow copy, meaning that any
object references in the original object are also copied over
to the new object, but they still refer to the same underlying
objects. If a deep copy is required, where all objects
referenced by the original object are also copied, then
the clone() method must be overridden in the subclass to
provide a deep copy implementation.
It is important to note that the clone() method provided by
the Object class is protected, meaning that it can only be
called from within the same package or by a subclass of the
object being cloned. This can make using the clone() method
somewhat cumbersome in practice, as it requires creating a
public clone() method in the subclass that calls the protected
clone() method of the Object class.In summary, the clone()
method and Cloneable interface provide a way to create a copy
of an object in Java. However, their use requires careful
consideration and implementation, particularly when dealing
with object references and deep copies
--Java Core API
The Core Java API package provides a set of classes and
interfaces that are used for developing applications in Java.
The package includes classes and interfaces for handling
input/output operations, working with collections, networking,
threading, GUI programming, and much more. Some of the
important classes and interfaces in the Core Java API package
are: java.lang - This package contains fundamental classes and
interfaces that are used by all Java applications. Some of the
classes in this package include Object, String, Integer, and
Exception. java.util - This package provides a set of
interfaces and classes for working with collections, such as
lists, sets, and maps. Some of the classes in this package
include ArrayList, HashMap, and LinkedList.
java.io - This package provides classes and interfaces for
input/output operations, such as reading and writing files,
sockets, and streams. Some of the classes in this package
include FileInputStream, FileOutputStream, and
BufferedReader. java.net - This package provides classes and
interfaces for networking operations, such as creating and
using sockets, working with URLs, and handling HTTP requests.
Some of the classes in this package include Socket, URL, and
HttpURLConnection. java.util.concurrent - This package
provides classes and interfaces for working with concurrent
programming constructs, such as locks, semaphores, and
threads. Some of the classes in this package include
ExecutorService, Future, and ConcurrentHashMap.
java.awt and javax.swing - These packages provide classes and
interfaces for developing GUI applications, such as creating
windows, buttons, menus, and other UI components. Some of
the classes in these packages include JFrame, JButton, and
JMenu.These are just a few of the many classes and interfaces
included in the Core Java API package. By mastering the usage
of these classes and interfaces, developers can create
powerful and efficient Java applications
Java’s Byte Code: Bytecode is a highly optimized set of
instructions designed to be executed by the Java run-time
system, which is called the Java Virtual Machine (JVM).
What is JVM:A Java Virtual Machine (JVM) is a program that
interprets Java bytecode to run as a program by providing a
runtime environment that executes this process.
Furthermore, this is separate from its operating environment,
supporting the “write once, run anywhere” philosophy.
Role of JVM: The JVM has two primary functions: to allow Java
programs to run on any device or operating system (known as
the "write once, run anywhere" principle), and to manage and
optimize program memory.
Explanation of public static void main(String args[]):
•The public keyword is an access specifier, which allows the
programmer to control the visibility of class members. When a
class member is preceded by public, then that member may
be accessed by code outside the class in which it is declared.
•The keyword static allows main ( ) to be called without
having to instantiate a particular instance of the class.
•The keyword void simply tells the compiler that main ( ) does
not return a value.
•As stated, main ( ) is the method called when a Java
application begins. Keep in mind that Java is case-sensitive.
Thus, Main is different from main.
•String args[ ] declares a parameter named args, which is an
array of instances of the class String. (Arrays are
collections of
similar objects.) Objects of type String store character
strings.
In this case, args receives any command-line arguments
present when the program is executed.
-What makes Java, most powerful language:
(i)Simple :Java was designed to be easy for the professional
programmer to learn and use effectively. Because Java
inherits the C/C++ syntax and many of the object-oriented
features of C++, most programmers have little trouble
learning Java. Also, some of the more confusing concepts
from C++ are either left out of Java or implemented in a
cleaner, more approachable manner. In Java, there are a small
number of clearly defined ways to accomplish a given task.
(ii) Security: When we use a Java-compatible Web browser,
we can safely download Java applets without fear of viral
infection or malicious intent. Java achieves this protection
by confining a Java program to the Java execution environment
(sand box area) and not allowing it access to other parts of
the computer. This control is exercised by JVM.
(iii) Portability: Many types of computers and operating
systems are in use throughout the world and many are
connected to the Internet. For programs to be dynamically
downloaded to various types of platforms connected to the
Internet, some portable executable code is needed that can
run on any environment i.e. the Byte Code.(iv) Object-Oriented
Like C++ : java uses all the OOP concepts like encapsulation,
inheritance and polymorphism etc. Java is called a purely
object oriented language as everything we write inside class
in a java program. (v) Robust The multiplatform environment of
the Web places extraordinary demands on a program, because the
program must execute reliably in a variety of systems. Thus,
the ability to create robust programs was given a high
priority in the design of Java. To gain reliability, Java
restricts us in a few key areas, to force you to find our
mistakes early in program development.(v) Multithreaded: Java
was designed to meet the real-world requirement of creating
interactive, networked programs. To accomplish this, Java
supports multithreaded programming, which allows us to write
programs that do many things simultaneously. The Java run-time
system comes with an elegant yet sophisticated solution for
multiprocess synchronization that enables us to construct
smoothly running interactive systems. (vi) Architecture-
Neutral: A central issue for the Java designers was that of
code longevity and portability. One of the main problems
facing programmers is that no guarantee exists that if you
write a program today, it will run tomorrow, even on the same
machine. Operating system upgrades, processor upgrades, and
changes in core system resources can all combine to make a
program malfunction. The Java designers made several hard
decisions in the Java language and the Java Virtual Machine in
an attempt to alter this situation. Their
-- What is constructor?
A constructor in Java is a special method that is used to
initialize objects. The constructor is called when an object
of a class is created.
Types of Constructors Default Constructors – The constructor
having nil parameters and no arguments. They are compiler-
generated implicit constructors. Parameterized Constructors are
those through which you can pass arguments. The arguments
initialize an object that was created. Create a parameterized
constructor simply by adding parameters to it, similar we do for
any other function. Use the parameters in the constructor’s body
to initialize the object. This type of constructor is commonly
used for overloading and also for initializing various data
elements of objects with different initial values. Copy
Constructor – It is a member function used to initialize an object
using another object of the same class. The
compiler, by default, creates a copy constructor for each class,
following a member-wise copy between objects.
--Constructor Overloading
Constructor Overloading is a phenomenon of a constructor
overloading another constructor. Overloaded constructors
follow the same name as that of the class. However, it varies
in several arguments. While creating an object, the type of
arguments passed determines which constructor is being
called. The constructor overloading can be defined as the
concept of having more than one constructor with different
parameters so that every constructor can perform a different
task.
-- Method Overriding :
Method overriding is a feature of object-oriented
programming that allows a subclass or child class to provide a
specific implementation of a method that is already provided
by one of its superclasses or parent classes. In other words,
when a subclass provides its own implementation of a method
that is already present in its parent class, it is called method
overriding.
Method overriding is used to provide a specific
implementation of a method that is already provided by its
superclass². This allows the subclass to provide its own
implementation of the method that is more appropriate for its
context
-- String Comparison
The String class includes a number of methods that compare
strings or substrings within strings. Several are examined here.
equals( ) and equalsIgnoreCase( ) To compare two strings for
equality, use equals( ). It has this
general form:boolean equals(Object str) Here, str is the String
object being compared with the invoking String object. It returns
true if the strings contain the same characters in the same
order,and false otherwise. The comparison is case-sensitive.
To perform a comparison that ignores case differences, call
equalsIgnoreCase( ). When
it compares two strings, it considers A-Z to be the same as a-z.
It has this general form:
boolean equalsIgnoreCase(String str)
// Demonstrate equals() and equalsIgnoreCase().
class equalsDemo {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = "HELLO";
System.out.println(s1 + " equals " + s2 + " -> " +
s1.equals(s2));
System.out.println(s1 + " equalsIgnoreCase " + s2 + " -> " +
s1.equalsIgnoreCase(s2));
}
}
output:
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true -x-

You might also like