JAVA/JEE Interview Question

  1. What is enum in java?
  2. What is static class in Java?
  3. Design Patter and its implementations?
  4. Anonymous class in Java, and where to use it?
  5. Hibernate one-to-one Bidirectional mapping?
  6. Lazy and eager fetching in Hibernate with example?
  7. Cache in Hibernate?
Call by value and Call by reference in Java :
This is a very basic question asked in every interview and should known to each programmer also.

Everyone says "no" as Java does not support pointers. No one actually see the address of the object in Java. No one can dream of call by reference without pointers with their experience of C/C++.

Of course, they are very correct.

In Java the method parameters can be primitive data types or object references. Both are passed as value only but small tricky explanation is here for object references. I repeat, when primitive data types are passed as method parameters, they are passed by value (a copy of the value) but incase of object references, the reference is copied (of course, here also a copy only) and passed to the called method. That is, object reference is passed as a value. So, original reference and the parameter copy both will refer the same Java object. As both refer the same object, if the calling method changes the value of its reference (passed from calling method), the original object value itself changes. Note that object itself is not passed, but it’s references is passed.

Object reference is a handle to a Java object. Do not compare this reference term with the used in C/C++. In C/C++, the reference directly points to the memory address of a variable and can be used for pointer manipulations.

Finally to say, in Java, everyone is passed by value. But with objecct, the value of the reference is passed.