Equals Example





I ran following example from link

http://www.jchq.net/certkey/0502certkey.htm

What will be the result of attempting to compile and run the following code?

        Object o1 = new Object();
        Object o2 = new Object();
        o1=o2;
        if(o1.equals(o2))
                System.out.println("Equals");

        }
1) Compile time error
2) "Equals"
3) No output
4) Run time error


Answer 3)

2) "Equals"

Because the one instance of Object has been assigned to the other with the line

o1=o2;

They now point to the same memory address and the test with the equals method will return true

I was not clear on the answer.

>>>now point to the same memory address

do they point to same memory address or point to same object on the pool. please advise.


Any ideas, resources,sample code,links,  highly appreciated. thanks in advance.
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
> do they point to same memory address or point to same object on the pool. please advise.

they are both the same thing
they point to the same object, so they also reference the same memory address (though with Java we tend not talk about memory address)
subratabiswasCommented:
An object is a portion in memory reserved to hold various methods, properties as defined in the underlying class. When we define an object, that portion of memory is reserved from the total pool of memory in the computer and the variable is assigned the location (a pointer) to the beginning address of that memory block.

When another object is declared and assigned the value from the previous object, all that is done is to assign the beginning location to the new object.

For example, suppose there is a class class ExampleClass. An object is created from that class like the following:

ExampleClass exampleObjects = new ExampleClass()

We can not see the memory assignment, but assume that the object exampleObject occupies 2K of memory starting at memory location 3,000,000. So the variable (object) contains a value of 3,000,000, because that is the location where the object has been defined.

The difference between declaration and definition is to be understood here. Declaration like ExampleClass exampleObjects; only reserves a memory location; all the memory that is required to hold the properties, methods etc. from the underlying class is not yet claimed from the pool of system memory. When we define the object like exampleObjects = new ExampleClass(); the memory to hold the properties, methods etc. from the class is claimed from the system memory pool and the beginning address of that pool is assigned to the new object variable.

Suppose now that a second object is created from the same class and assigned the value from exampleObject

ExampleClass newObject = exampleObject;

What we are doing in the last statement is declaring a new variable (called newObject) and assigning it the same memory location from exampleObject (3,000,000)

With this in mind, when operations like "equals" are performed on these two objects, the comparison is basically done on the same memory location.

Usually there is a meaningful "equals" operation defined for each of the classes. For example, for Strings, the meaningful "equals" is to verify that the characters in both the string are same. Similarly, "equals" for a LocationObject class may compare the x-value and y-value to decide if they are equal.

In addition, it is also possible to compare the equality of two objects, e.g.

if (exampleObject == newObject)

In the above absolute comparison, it is verfied if both the objects are assigned the same memory location. For example:

String s1 = "New String";
String s2 = "New String";


the comparison s1.equals(s2) will return true but if (s1 == s2) will return false

Hope this clears the concept.
gudii9Author Commented:
>>>
With this in mind, when operations like "equals" are performed on these two objects, the comparison is basically done on the same memory location.

Usually there is a meaningful "equals" operation defined for each of the classes. For example, for Strings, the meaningful "equals" is to verify that the characters in both the string are same. Similarly, "equals" for a LocationObject class may compare the x-value and y-value to decide if they are equal.

In addition, it is also possible to compare the equality of two objects, e.g.

if (exampleObject == newObject)

In the above absolute comparison, it is verfied if both the objects are assigned the same memory location. For example:

String s1 = "New String";
String s2 = "New String";

the comparison s1.equals(s2) will return true but if (s1 == s2) will return false




i was not clear on above statements.  Espectially location object and absolute comparison.
>>>
With this in mind, when operations like "equals" are performed on these two objects, the comparison is basically done on the same memory location.

>>if (exampleObject == newObject)

In the above absolute comparison, it is verfied if both the objects are assigned the same memory location.
so in the case of object comparison as in this example either equals or absolute comparison(== )only memory comparison done?

please advise
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

subratabiswasCommented:
When a new java object is created, a portion of memory is used to hold the object (its properties, methods etc.)
When a variable is assigned that object, it is basically given the starting value of that memory location. For example (simplified), suppose an object is defined and the definition occupies 20 bytes in memory starting from memory location 4,000,000. If a variable is assigned that object, that variable will hold the value 4,000,000.

in the example String s1 = "New String"

imagine that the String object occupies 20 bytes to hold the characters 'N', 'e', 'w', ' ', 'S', 't', 'r', 'i', 'n', 'g' and any other additional bytes that a String object may require for definition. Then s1 above will be assigned the vale 4,000,000 - the address of the first byte of the 20 byte memory holding the object representing "New String".

Now if another variable is defined like s2 = "New String", another block of 20 bytes will be used to store the characters, but these 20 bytes will be used starting at different memory location, for example, 4,000,020.

With the two variables above, operation like if (s1 == s2) will check if both the variables are holding the same memory addresses. Since they are holding memory addresses 4,000,000 and 4,000,020 respectively, the comparison will return false.

But comparison like if (s1.equals(s2)) will check if each if the bytes in the first string is same as the character in corresponding position in the second string. i.e., if character[1] of s1 is same as character[1] of s2, character[2] of s1 is same as character[2] of s2 etc. upto the whole length of the two strings. In the example, since the characters in both the strings are identical for each of the positions, equals operation will return true

Suppose a third variable is defined like the following:

String s3 = s1

Here s3 is assigned the address of s1, which was 4,000,000. So if we compare the following

if (s1 == s3)

it will return true, because both s1 and s3 hold the same address (4,000,000) now.

In summary, == operation does an address comparison while equals operation compares the contents.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
when i execute like



public class stringex {
public static void main(String[] args) {

String s1 = "New String";
String s2 = "New String";
String s3 = s1;

System.out.println("equlas s1==s2 is-->"+s1==s2);
System.out.println("equlas s1.equals(s2) is"+s1.equals(s2));
System.out.println("equlas s1==s3 is---"+s1==s3);
System.out.println("equlas s1.equals(s3) is"+s1.equals(s3));
}
}



i got output as


false
equlas s1.equals(s2) istrue
false
equlas s1.equals(s3) istrue


i was not sure why

System.out.println("equlas s1==s3 is---"+s1==s3);
System.out.println("equlas s1.equals(s3) is"+s1.equals(s3));

is false and true.

I was not sure why system out not printing equlas s1==s3 is--
please advise
Mick BarryJava DeveloperCommented:
its the order of operations, you should be using:

System.out.println("equlas s1==s3 is---"+(s1==s3));

you're comparing "equlas s1==s3 is---"+s1   with    s3
gudii9Author Commented:

>>>if (s1 == s3)

it will return true, because both s1 and s3 hold the same address (4,000,000) now.


when i execute like



public class stringex {
public static void main(String[] args) {

String s1 = "New String";
String s2 = "New String";
String s3 = s1;

System.out.println("equlas s1==s2 is-->"+s1==s2);
System.out.println("equlas s1.equals(s2) is"+s1.equals(s2));
System.out.println("equlas s1==s3 is---"+s1==s3);
System.out.println("equlas s1.equals(s3) is"+s1.equals(s3));
}
}

i got
System.out.println("equlas s1==s3 is---"+s1==s3);
as false but based on above explanation i should get true right since both hold same memory address now.


>>its the order of operations, you should be using:

System.out.println("equlas s1==s3 is---"+(s1==s3));

you're comparing "equlas s1==s3 is---"+s1   with    s3


I have not understood above comment. can you please elaborate.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.