Browsed by
Tag: complete notes on core java in java

Explain the methods, rebind() and lookup() in Naming class?-

Explain the methods, rebind() and lookup() in Naming class?-

rebind() of the Naming class(found in java. rmi) is used to update the RMI registry on the server machine. Naming. rebind(”AddSever”, AddServerImpl); lookup() of the Naming class accepts one argument, the rmi URL and returns a reference to an object of type AddServerImpl. I’m sorry, but there seems to be a misunderstanding. As of my last knowledge update in January 2022, there are no rebind() and lookup() methods in the Naming class in Core Java. In Java, the Naming class…

Read More Read More

Why Calling System.setProperty() does not Affect the Classpath at Run-Time

Why Calling System.setProperty() does not Affect the Classpath at Run-Time

You can easily set any system properties in java using System.setPropoerty method, However it may not have any effect in case of CLASSPATH property. This is mainly because the Java system class loader is initialized very early in the JVM startup sequence. The class loader copies the classpath into its own data structures, and the classpath property is not read again. Therefore changing it after its already copied does not affect anything. There are mainly two reasons for this –…

Read More Read More

How do you pass data (including JavaBeans) to a JSP from a servlet?-

How do you pass data (including JavaBeans) to a JSP from a servlet?-

?– (1) Request Lifetime: Using this technique to pass beans, a request dispatcher (using either “include” or forward”) can be called. This bean will disappear after processing this request has been completed. Servlet: request. setAttribute(”theBean”, myBean); RequestDispatcher rd = getServletContext(). getRequestDispatcher(”thepage. jsp”); rd. forward(request, response); JSP PAGE:<jsp: useBean id=”theBean” scope=”request” class=”. . . . . ” />(2) Session Lifetime: Using this technique to pass beans that are relevant to a particular session (such as in individual user login) over a…

Read More Read More

Is it Possible to Customize the Serialization Process? How can we Customize the Serialization Process

Is it Possible to Customize the Serialization Process? How can we Customize the Serialization Process

Yes, the serialization process can be customized. When an object is serialized, objectOutputStream.writeObject (to save this object) is invoked and when an object is read, ObjectInputStream.readObject () is invoked. What most people do not know is that Java Virtual Machine provides you with an option to define these methods as per your needs. Once this is done, these two methods will be invoked by the JVM instead of the application of the default serialization process. Classes that require special handling…

Read More Read More

Can a lock be acquired on a class?

Can a lock be acquired on a class?

Yes, a lock can be acquired on a class. This lock is acquired on the class’s Class object. Yes, in Java, it is possible to acquire a lock on a class. In Java, every class and object has an associated lock, also known as a monitor. This lock is used to control access to the critical section of code to ensure that only one thread can execute the code block at a time. To acquire a lock on a class, you can…

Read More Read More

How can we make Hashmap Synchronized

How can we make Hashmap Synchronized

HashMap can be synchronized by Map m = Collections.synchronizedMap(hashMap); In Core Java, you can make a HashMap synchronized by using the Collections.synchronizedMap() method. This method returns a synchronized (thread-safe) map backed by the specified map. Here’s an example: java import java.util.Collections; import java.util.HashMap; import java.util.Map;public class SynchronizedHashMapExample { public static void main(String[] args) { // Creating a HashMap Map<String, Integer> hashMap = new HashMap<>(); // Populating the HashMap hashMap.put(“One”, 1); hashMap.put(“Two”, 2); hashMap.put(“Three”, 3); // Making the HashMap synchronized Map<String, Integer>…

Read More Read More

Can an anonymous class be declared as implementing an interface and extending a class

Can an anonymous class be declared as implementing an interface and extending a class

An anonymous class may implement an interface or extend a superclass, but may not be declared to do both. Yes, in Java, an anonymous class can be declared as implementing an interface and extending a class simultaneously. This allows you to create a class on the fly that provides a specific implementation for an interface while also inheriting from a class. Here is an example: java interface MyInterface { void interfaceMethod(); } class MyBaseClass { void baseClassMethod() { System.out.println(“Base class…

Read More Read More

What is difference between String, StringBuffer and StringBuilder? When to use them?

What is difference between String, StringBuffer and StringBuilder? When to use them?

The main difference between the three most commonly used String classes as follows. StringBuffer and StringBuilder objects are mutable whereas String class objects are immutable. StringBuffer class implementation is synchronized while StringBuilder class is not synchronized. Concatenation operator “+” is internally implemented by Java using either StringBuffer or StringBuilder. Criteria to choose among String, StringBuffer and StringBuilder If the Object value will not change in a scenario use String Class because a String object is immutable. If the Object value…

Read More Read More

How many times may an object’s finalize() method be invoked by the garbage collector?

How many times may an object’s finalize() method be invoked by the garbage collector?

An object’s finalize() method may only be invoked once by the garbage collector. In Java, there is no guarantee on when or how many times the finalize() method of an object will be invoked by the garbage collector. The finalize() method is called by the garbage collector before it reclaims the memory occupied by an object, but there is no certainty about the exact timing or number of invocations. It’s important to note that relying on the finalize() method for critical resource…

Read More Read More