Java Programming - Java.lang Class - Discussion


What will be the output of the program?

public class ObjComp 
    public static void main(String [] args ) 
        int result = 0;
        ObjComp oc = new ObjComp();
        Object o = oc;

        if (o == oc)  
            result = 1;
        if (o != oc)  
            result = result + 10;
        if (o.equals(oc) )  
            result = result + 100;
        if (oc.equals(o) )  
            result = result + 1000;

        System.out.println("result = " + result);

[A]. 1
[B]. 10
[C]. 101
[D]. 1101

Answer: Option D


Even though o and oc are reference variables of different types, they are both referring to the same object. This means that == will resolve to true and that the default equals() method will also resolve to true.

Badri said: (Jul 12, 2011)  
Please explain why 2nd one is not correct answer.

Mayur said: (Dec 26, 2011)  
Object is the base class of all Java classes.So O and oc can be compared.So result of first If statement is-result=1;again 3 if statement is also true so result is-result=101(1+100);finally,4th if is also true making result=1101(101+1000).

3rd if is obviously false as they refer to same object and thereby,(o!=oc) is false.

Akash said: (Jul 21, 2013)  
I think if(o.equals(oc)) always return false because o and oc are refer same object. Som that return false.

If my statement is NOT TRUE then please explain me in briefly.

Uma said: (Sep 6, 2013)  
oc is an object created for that class? Is it a reference?

Can we use "==" for comparing this object?

Please any one do explain?

Diva said: (Aug 31, 2014)  
oc is a reference variable, and so is 'o', both of them pointing to some object on heap. so in order to check if 2 references refer to same object, you always use == but to check if objects themselves are same, we go for equals() method. In the above case the 2 references refer to the same object because you have copied,ASSIGNED oc object to o. Therefore, they behave as 1 object.

Onie said: (Dec 11, 2015)  
Guys, you know in java == operator for primitive data type comparison contain.

Example: int a=10; int b=10; if(a==b);true.

If we declare reference type user type just like class student.

int a;
student st=new Student();
student st1=new student();

False because in case comparison address if we want constant comparison, than use equal() method as you know Object class super class direct all class so .in obj class equal() overridden.

Object o=st;

Avnish said: (Jun 7, 2016)  
See what happening in this:

ObjComp oc = new ObjComp(); //line1
Object o = oc; //line2

In line 1 we creating one object with reference variable oc and;
In line 2 we assigning that value to Object o so that both objects referring to same object and content for both the object is same.

if (o==oc)
result = 1;
if (o!=oc)
result = result + 10;
if (o.equals(oc))
result = result + 100;
if (oc.equals(o))
result = result + 1000;

As the program is like this,

First, if stmt is matched and value of int variable i.e result will be assigned to 1 so result=1
second if stmt is not matched so control will be moved to 3rd if stmt.

3rd stmt will return true so result = 101.

4th if stmt also return true so,

Result = 1101.

Tanuja said: (Jun 25, 2017)  
I think that (o) and (oc) have different reference and == checks reference, so how first condition is true. Please explain.

Vidy said: (Jul 18, 2019)  
== means that the references points to the same object and not equivalent objects (objects having same content) therefore since o and oc points to the same object the if condition is ' true' and since they will have same values/content (as both refers to the same object) comparison by equals is also true.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.