????????????????3#???????????

?????????????????get??????÷????????????????????????????á????????????У?

/*** Example class.The value should never * be negative.*/ public class Example{
private Integer i = new Integer (0);
public Example (){
}
/*** Set x. x must be nonnegative* or an exception will be thrown*/
public synchronized void setValues (int x) throws IllegalArgumentException{ if (x < 0)
throw new IllegalArgumentException();
i = new Integer (x);
}
public synchronized Integer getValue(){
// We can’t clone Integers so we makea copy this way. return new Integer (i.intValue());
}
}

??????δ?????????????????????1#?????????????????????Integer??????String??????????????????????????????????????Integer???????????????????????????

????????getValue()????д???

public synchronized Integer getValue(){ // ’i’ is immutable?? so it is safe to return it instead of a copy. return i; }

????Java?????C++???????????????????JDK ????????????????????

????Boolean
????Byte
????Character
????Class
????Double
????Float
????Integer
????Long
????Short
????String

?????????Exception??????

???????????????4#??????????????????

????Java???????????飬???????????????????д???μ???????????????μ?????????????????飬???????Object??clone????????п???????

public class Example{ private int[] copy;
/*** Save a copy of ’data’. ’data’ cannot be null.*/ public void saveCopy (int[] data){
copy = new int[data.length];
for (int i = 0; i < copy.length;
++i) copy[i] = data[i];
}
}

??????δ?????????????????????????saveCopy()????????????????

void saveCopy (int[] data){ try{ copy = (int[])data.clone(); }catch (CloneNotSupportedException e){ // Can’t get here. } }

?????????????????飬??д???μ??????????????????????

static int[] cloneArray (int[] data){
try{
return(int[])data.clone();
}
catch(CloneNotSupportedException e){
// Can’t get here.
}
}

??????????????????saveCopy??????????????

void saveCopy (int[] data){
copy = cloneArray ( data);
}