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.

     


mantishAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.