More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. This looks like the case for optimistic locking wherein hibernate suspects that the information in table was updated by some other transaction after the entity was loaded by current transaction.
One way is to have synchronized entity state and don't detach the entity. Other could be to merge the entity with the table record rather than just directly persisting the entity.
Help us improve. Please let us know the company, where you were asked this question :
Ans. runtime polymorphism or method overriding doesn't require method name and signature to be different whereas compile time polymorphism or method overloading requires method name to be same but the signature to be different.
Help us improve. Please let us know the company, where you were asked this question :
Ans. dynamic method dispatch is a process in which a call to an
overridden method is resolved at runtime rather than at compile-time. This is used to achieve runtime polymorphism in java.
Help us improve. Please let us know the company, where you were asked this question :
Ans. It will print "true" with integers as well as strings. The reason is "Integer constant pool" and "String pool"
String pool maintains pool of string literals. When a string literal is used for the first time, a new string object is created and is added to the pool. Upon it's subsequent usage , the reference for the same object is returned. Similarly java uses integer constant pool.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Object is an entity in Java , i.e which has a state ( instance variables ) and methods attached to it ( static or non static , through class definition ). References are the identifiers that are used to point to objects.
For example -
Employee emp = new Employee();
emp = new Employee();
In this code, emp is the reference that gets assigned to the new object created by the new operator. In the second line , we have assigned the same reference to another object. So with these 2 lines of code, we have 2 objects in memory with reference emp now pointing to second object.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Now the reference points to a new object in memory. If that was the only reference for the previous object , it will be marked for garbage collection.
Foe example -
Object obj = new Object();
obj = new Object();
object created in first line will be eligible for garbage collection after line 2 as it looses all it's handlers.
Help us improve. Please let us know the company, where you were asked this question :
Ans. By encapsulating it within another class and declaring it private. In such a case, it will only be accessible through parent class or parent class object.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Java Programs are collection of objects that communicates with each other to get a task accomplished. To add to those objects, there are common spaces ( static i.e common for objects belonging to a class ) that are used too.
We can visualize objects as departments of an organization in real world. Just like Task gets initiated in one department and then files are moved across different departments to get work done. In a similar fashion, a task is initiated in one object ( having main method ) and then information ( through POJOs / DTOs ) is moved across objects to accomplish a task.
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  objects  Java Programs are collection of objects  real life example of object communication
Very frequently asked. Usually among very first few questions.
Ans. Encapsulation is a feature of OOP's that binds the data and it's associated methods together as a single unit and facilitate protection and data hiding by providing minimal interface to outside. For example - member variables are declared private and are accessed through public methods. Moreover we have private methods that can only be used internally and hence providing minimal interface to outside class through use of public methods.
Ans. We can store the references in a collection by adding to those objects in the collection. We can create a class "ObjectRegistry" with a collection or multiple collections with a search algorithm to look for the already collected objects.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Immutable objects relieves us from the problems of inconsistencies and security and helps with better read performance but at the same time are write performance and storage overheads. In case any modification is required , a new object is created and thus creating multiple copies of it.
This is the reason we use StringBuffer / StringBuilder when we have to append some text multiple times and then create a String out of it.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes , but only the read performance in case we don't need to modify it much. In case we need to modify it a lot , it creates write performance overheads as we would need to create many news objects instead of just changing one.
Help us improve. Please let us know the company, where you were asked this question :