Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes clone() with arrays?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "clone() with arrays??" Watch "clone() with arrays??" New topic
Author

clone() with arrays??

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435

Ans : It will print 'false true' when run
I did not understand the concept of clone()method.
What do we mean when a clone () is associated with an array?
Sonir
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Sonir:
here is part of the explanation of Clone method from the API .
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:
x.clone() != x
will be true, and that the expression:
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
x.clone().equals(x)

If you do an == comparison on two reference variables, then the comparison returns true ONLY if both variables point to the same object. Otherwise , this comparison returns false.
Since the second array is a clone of the first array, and these both are arrays of a primitive ( int ) naturally the elements within the array have the same values.
My guess is if this were an array of Strings, then even the OrigArray[i]==CloneArray[i] would return false , but OrigArray[i].equals(CloneArray[i]) would return true.
HTH
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Arrays are also objests, thats why == did not work.
victor
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
Hello Shivaji
Ok I understood the meaning of clone() method
But I am still not clear with the matter in the code specially the lines :
System.out.print((ia == ja) + " "); System.out.println(ia[0] == ja[0] && ia[1] == ja[1])
Why is the answer printed 'false true'
Sonir
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Let's try to breakdown the two statements
System.out.print((ia == ja) + " ");
The condition ia==ja evaluates to false . Therefore this line prints false becasue the two objects are not equal. Notice it is not println. So the next print will begin after the word false and a space.
System.out.println(ia[0] == ja[0] && ia[1] == ja[1])
This line uses && for two boolean conditions, both of which evaluate to true. The first elements in both arrays are equal to each other and so are the second elements.
So the condition here becomes ( true && true ) which evaluates to true again. So this statement prints true.
Hope this helps
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
ia means the array itself or the matrix
ia[0] refers to the first element of this array
ia[1] refers to the second element
ia and ja are two separate arrays. right ? one is the original and the second is its clone.
but their elements are same because they are clones. so ia[0] and ja[0] are equal
and also ia[1] and ja[1] are equal
hope you got it.

Originally posted by sonir shah:
Hello Shivaji
Ok I understood the meaning of clone() method
But I am still not clear with the matter in the code specially the lines :
System.out.print((ia == ja) + " "); System.out.println(ia[0] == ja[0] && ia[1] == ja[1])
Why is the answer printed 'false true'
Sonir
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
Here
ia==ja is false
because they are diffrent objects.
Here what do we mean to say.
objects i.e both are int or the value which it contains?
Sonir
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: clone() with arrays??