• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

A Hash Table question

Hello folks
               Another simple question .I am using a hash Table to
store my objects.
public class MyObj {
           String addr;
           int   age;
    }

So now I use the get method in the HashTable API to get my objects
Hashtable hash = new Hashtable();
MyObj m1 = (MyObj)
i insert using a string  
hash.put("mantish",m1);
At some point I retrieve the object.
MyObj m2 = (MyObj)hash.get("mantish");
now I do m2.age += 10;

NOW my q is is whether the last statement changes the actual
object . If  ((MyObj)hash.get("mantish")).age will now be modified

My next q is what is the meaning of shallow copy.Does it mean it jus creates a new obj or does it still retaining a ref to the old obj.

     


0
mantish
Asked:
mantish
  • 6
  • 5
1 Solution
 
mantishAuthor Commented:
Edited text of question.
0
 
mantishAuthor Commented:
Edited text of question.
0
 
_lychee_Commented:
yes ur statement changes the object content (ie. m1.age also changed)

shallow copy means that a copy of the object is made but the objects that this object uses are not copied
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mantishAuthor Commented:
>objects that this object uses are not copied??

you mean are not changed.For now I'm rejecting the annwer Jus send  a reply and I'll award you points


0
 
_lychee_Commented:
i do not understand you...

are not changed?????
you are asking about shallow _copies_
it just means that the object itself is copied wholesale -- implying that any object references it holds are also copied ie. they point to the same objects as the original one.
0
 
mantishAuthor Commented:
Ok let me jus write some sample code to clear the pt.

public class MyClass implements Cloneable {

private String name;
private int age ;

public MyClass(){
  name ="default";
  age    =0;
}

public MyClass(name,int){
  this.name = name;
  this.age    = age;
}




}

MyClass m1 = new MyClass("mantish" ,1);
Myclass  m2 = (MyClass)m1.clone() ;
m2.age = 2 ;

System.out.println(" the age for the obj m1 is   "  + m1.age);

 what would this print . So u say that it should be 2 right ???





     
 




 
0
 
_lychee_Commented:
no... it should be 1...
cos m1.clone() and m1 are different objects

anyway to implement cloneable u should override clone to be public and call super.clone in it...
0
 
mantishAuthor Commented:
like this you mean???
public class MyClass implements Cloneable {

                      private String name;
                      private int age ;

                      public MyClass(){
                        name ="default";
                        age    =0;
                      }

                      public MyClass(name,int){
                        this.name = name;
                        this.age    = age;
                      }

                      public  Object clone(){
                           try{
                                   super.clone();
                                }      
                           catch(CloneNotSupportedException c){
                           }

                      }
}


                      MyClass m1 = new MyClass("mantish" ,1);
                      Myclass  m2 = (MyClass)m1.clone() ;
                      m2.age = 2 ;

                      System.out.println(" the age for the obj m1 is   "  + m1.age);
0
 
_lychee_Commented:
not really....
u should propagate the CloneNotSupportedException and u return the super.clone-return... like so:

public Object clone() throws CloneNotSupportedException {
    return super.clone();
}
0
 
mantishAuthor Commented:
Thanks for correcting it .But I'd like to know why it needs to be propagated

0
 
_lychee_Commented:
if  you don't propagate it what'll u return?
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now