Author: Editorial Team
-
Java program to get sum of all digits of a number in Java
Approach Method 1 – Iterative Method 2 – Single line Method 3 – Recursive Output Java 8 Program: Sum of Digits
-
Can a constructor be synchronized in Java?
Constructors cannot be synchronized. Using the synchronized keyword with a constructor is a syntax error – Illegal modifier for the constructor in type your class name; only public, protected & private are permitted’. Synchronizing constructors doesn’t make sense, because only the thread that creates an object should have access to it while it is being…
-
Write a Java program to demonstrate deadlock
Deadlock is a situation when two or more threads are indefinitely blocked waiting for each other. In below example, there are two threads, Thread1 and Thread2. Two resources are represented by two strings, resource1 and resource2. Thread1 first gets a lock on resource1 and then resource2. Thread2 first gets a lock on resource2 and then…
-
Remove white spaces from String in Java
Remove white spaces from string using build-in methods Approach: We’ll use replaceAll() method of String class and replace all white spaces with “”. Output Note: We could have used “\s+” instead of “\s”. Both produce the same result with almost same performance. But, when the number of consecutive spaces increase, “\s+” is faster than “\s”.…
-
Difference between Runnable and Callable
Both Runnable and Callable interface are used to encapsulate task supposed to be executed by another thread. Runnable and Callable both run on a different thread than the calling thread. There are certain things which Runnable can not do like throwing checked exception and returning result after computation. Java team could have changed the signature…
-
Externalization in Java
Externalizable interface extends java.io.Serializable interface. Externalizable interface is used to provide the implementing class complete control over the format and contents of the stream for an object and its supertypes. Externalization in Java is used for customized serialization. If you want to control the process of reading and writing the objects during serialization and de-serialization…
-
Serialization with respect to inheritance
Case 1: Parent class implements Serializable When parent class is serializable then child class is also serializable. That is if parent class implements Serializable interface then this behavior is inherited to child. Hence, it is not required by child class to explicitly implement Serializable interface. Case 2: Parent class does not implement Serializable but Child…
-
Custom serialization in Java
Consider a case where an object has username and password as fields. We don’t want password field to be serialized like username field. So we use transient with password field. As we already know, transient fields are not persisted. During serialization, use of transient may lead to data loss. Sometimes, it is needed to serialize…
-
Object graph in Serialization
It is possible that an object references(uses) other objects as well. When we serialize an object, all objects which are referenced by the serialized object are also serialized. This group of objects is Object Graph. In object graph every object should be serializable. If any of the object is not serializable then we’ll get NotSerializableException.…
-
Serialization of multiple objects in same file
Yes, we can serialize multiple objects in a single file. While deserialization the order in which objects are deserialized should be same in which the objects were serialized. In the following example, we are serializing object of Student, Employee and Car class. While deserialization, the order is same as serialization. Output Now try changing the…
