I was reviewing Java, but I noticed that the basic part was subtle. Keep a record of your understanding
Pass by reference → Pass the location in memory Pass by value → Pass the value as it is
Only pass by value in Java Primitive type → Pass by value as it is Object type → Reference "value" passed
Passing a pointer (information on where data is stored in memory) So You can manipulate the original value by passing a reference value to the method
public void sampleMethod(Object o){
o = 1;
}
However, if new is used, the reference value will be rewritten, so even if changes are made after that, the original value will not be affected. If you do not return with, a reference value that will not be used will be born
public void sampleMethod(Object o){
o = new Object();
o = 1;
}
Since String is an object type, it is passed by reference value But even if you replace the value, the original value does not change → Because it is a class that cannot be changed Every time you make a change to String, a new object is created behind the scenes (changed to a new pointer) Therefore, the original object cannot be changed. Therefore, this is a different movement from passing by value even if the result is the same.
Incorporating another class into one class
There is inheritance to use other classes, but using inheritance can cause unexpected behavior
Inheritance
public class InstrumentedHashSet<E> extends HashSet<E> {
private int addCount = 0;
public InstrumentedHashSet(){
}
@Override
public boolean add(E e){
addCount++;
return super.add(e);
}
@Override
//addAll internally loops the arguments and adds
//Therefore, in this class, the overridden add is used and the count advances by the amount of add and the amount of addAll.
//In other words, when inheriting and overriding a method, if the target method uses another overridden method, it will be affected.
public boolean addAll(Collection<? extends E> c){
addCount += c.size();
return super.addAll(c);
}
public int getAddCount(){
return addCount;
}
}
Composition
//Transfer class
//The composition itself can be done without making this separately.
public class ForwardingSet<E> implements Set<E>{
//Composition
private final Set<E> s;
//The Set used here is just the one passed as an argument
public ForwardingSet(Set<E> s){
this.s = s;
}
public void clear(){
s.clear();
}
public boolean add(E e){
return s.add(e);
}
public boolean addAll(Collection<? extends E> c){
return s.addAll(c);
}
//Override the abstract method of Set(abridgement)
}
//Wrapper class
public class RapperSet<E> extends ForwardingSet<E>{
private int addCount = 0;
//Pass the Set you want to base the superclass on
public RapperSet(Set<E> e){
super(e);
}
@Override
public boolean add(E e){
addCount++;
return super.add(e);
}
@Override
public boolean addAll(Collection<? extends E> c){
addCount += c.size();
//AddAll used here()Is used in the superclass addAll()
//That is, addAll of the class passed as an argument().. rapperSet add()Does not matter
return super.addAll(c);
}
public int getAddCount(){
return addCount;
}
}
Recommended Posts