This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clone() with arrays??

 
sonir shah
Ranch Hand
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 203
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Arrays are also objests, thats why == did not work.
victor
 
sonir shah
Ranch Hand
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 203
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic