Browsed by
Tag: java 15 features

Which Java operator is right associative?

Which Java operator is right associative?

The = operator is right associative. In Core Java, none of the operators are right-associative. All Java operators are left-associative, which means that they are evaluated from left to right when they have the same precedence. Right-associative operators would be evaluated from right to left, but Java does not have such operators. In Core Java, the assignment operator (=) is right-associative. Right-associative means that when multiple assignment operators of the same precedence appear in an expression, they are evaluated from right to…

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 are Java source code files named

How are Java source code files named

within the file. A source code file may contain at most one public class or interface. If a public class or interface is defined A Java source code file takes the name of a public class or interface that is defined within a source code file, then the source code file must take the name of the public class or interface. If no public class or interface is defined within a source code file, then the file must take on…

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

What is the use if “instanceof” keyword ?

What is the use if “instanceof” keyword ?

instanceof” keyword is used to check what is the type of object. F The instanceof keyword in Java is used to test whether an object is an instance of a particular class, interface, or subclass. It is a binary operator that returns a boolean value – true if the object is an instance of the specified type, and false otherwise. Here’s a basic example: java public class Example { public static void main(String[] args) { String str = “Hello, World!”;//…

Read More Read More

What value does read() return when it has reached the end of a file

What value does read() return when it has reached the end of a file

The read() method returns -1 when it has reached the end of a file. In Core Java, the read() method of the InputStream class returns an integer value. When it reaches the end of a file, it typically returns -1 to indicate the end of the stream. Here’s a simple example: java import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class ReadFileExample { public static void main(String[] args) { try (InputStream inputStream = new FileInputStream(“example.txt”)) { int data; while ((data = inputStream.read()) != -1) {…

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

How much subclasses you can maximum in Inheritance?

How much subclasses you can maximum in Inheritance?

In one of our old JAVA projects we had an inheritance depth of five. Believe us we never liked that code. It’s bad to have a huge inheritance depth. A maintainable inheritance depth should be maximum 5. Anything above that is horrible. There is no limit as such specified anywhere that there is some limit on the inheritance sub classing but depending on environments you will get stack over flow error.

When can an object reference be cast to an interface reference

When can an object reference be cast to an interface reference

An object reference be cast to an interface reference when the object implements the referenced interface. In Java, an object reference can be cast to an interface reference when the object’s class implements that interface. This is possible because Java supports polymorphism through interfaces. Here’s a brief explanation: When the Object Implements the Interface: If a class implements an interface, you can cast an object of that class to a reference of the interface type. java interface MyInterface { void…

Read More Read More

Write your own Strategy with Interceptor.isUnsaved()

Write your own Strategy with Interceptor.isUnsaved()

When you reattach detached objects, you need to make sure that the dependent objects are reattached as well. It appears that you are asking about using an interceptor in the context of advanced Java programming, and specifically, you want to know about a strategy involving the Interceptor.isUnsaved() method. However, the provided information is not sufficient to give a specific and accurate answer. The Interceptor.isUnsaved() method seems to be a custom or context-specific method, and its behavior or purpose is not…

Read More Read More