• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

clone() with arrays??

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arrays are also objests, thats why == did not work.
victor
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic