I have facing few problems with my lab questions.
and i am using BlueJ software for my lab work.

  1. Are two objects equal if their state is the same? And i answered No, however they are still different objects with the same state. But how can i explain it?
  2. How can i able to view the class code? is the class code as sourse code?
  3. What is documentation in BlueJ meaning?

Recommended Answers

All 8 Replies

Never having used BlueJ I cannot answer #3. As for #1, if two objects of the same class have the same state, then at that time, they are identical, in that A == B. That said, you can change the state of one, and then they will no longer be identical. Identical does not mean they are the same, just as identical twins are not the same entity.

As for #2, when you compile a .java source file, you get a .class file (a binary representation of the source code). They are not the same, but there are tools to reconstruct the source code from the .class file.

But when i create the objects, i cannot put the same name of the objects. Therefore, can i say this is not identical since they are still two different objects?

What do you mean by "put the same name of the objects"? Show code please.

Therefore, can i say this is not identical since they are still two different objects?

no. equality has nothing to do with the name of the variable.

for instance:

String a = "hello";
String b = "hello";

Now, you would think that there are two different Strings. Actually, there are not (Agreed, String is a 'special case', but still).
There is only one String with value "hello" on the heap, a and b are not actual Strings, they are reference variables which point to the String in the heap.

Since there is only one String, they both have the same address, so, both for equality of value as for equality of address (actual object in memory) they are equal. This is not related to whether the instances have been given the same name (which is not possible anyway, so if your reasoning would be correct, it would be impossible to have two equal objects)

Next, it also depends on how you define 'equal'. Do you mean: the same object in memory, or: an object with an identical value, that will return true if the equals method is called on it?

if for the above Strings, you run:

if ( a == b ) System.out.println("1 is true");
if ( a.equals(b) ) System.out.println("2 is true");

both these lines will be printed.

however, add a new String and do the same checks:

String c = new String("hello");
if ( a == c ) System.out.println("3 is true");
if ( a.equals(c) ) System.out.println("4 is true");

Only the second line will be printed.

By using the new keyword, you force a new Object with identical value to be placed on the heap. So, is it the same object on the heap? no. Is it an object with equal value to the other ones? yes.

The == operator checks equality of reference (the address of the object in the memory) while the .equals method checks the value of the Object.

Let give an example,
I declared two object which are A and B, and both having the same attributes like name and amount.

So i assign the the same value into both object.
name = "iPhone6"
price = 600

So the question is ask, are this two objects same if their state(referring the value) are same.
So i am wondering is my reason correct? Since they are two different objects, but just having the same values.

They are two different Objects that happen to have the same values in them. They occupy different places in the JVM's heap, and have different hashes. The == operator will return false. You can change one of them, and the other will not be changed.
All Java objects have an equals method that is normally used to test for two objects having the same values (state). When you create a new class you have to define what "equals" means for your class, otherwize you just inherit the default equals method which is just the same as == (ie tests for being exactly the same object in memory)

They are different instances, they have their own memory location.An example in real life would be 2 people with the same name and age. Also you could tell that they are different in BlueJ as it creates 2 different graphical objects that you can call their methods on. For example if you implement a method changeName(String name) and you call it to one of your objects, it won't have affect over the other

Thanks guys. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.