Looking back on the basics of Java

Preface

I was reviewing Java, but I noticed that the basic part was subtle. Keep a record of your understanding

Pass by reference and pass by value

Pass by reference → Pass the location in memory Pass by value → Pass the value as it is

What about Java?

Only pass by value in Java Primitive type → Pass by value as it is Object type → Reference "value" passed

What is passing by reference value?

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;
}

About String

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.

Composition

Incorporating another class into one class

Why are you

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

Looking back on the basics of Java
Looking back on JSON handled by Java
[day: 5] I summarized the basics of Java
I touched on the new features of Java 15
Docker monitoring-explaining the basics of basics-
Basics of character operation (java)
Understand the basics of docker
The basics of Swift's TableView
Summary of Java language basics
I summarized the types and basics of Java exceptions
I tried to summarize the basics of kotlin and java
20190803_Java & k8s on Azure The story of going to the festival
About the basics of Android development
[Java version] The story of serialization
Progate Ruby on Rails5 Looking Back
The basics of SpringBoot + MyBatis + MySQL
Note on the path of request.getRequestDispatcher
The origin of Java lambda expressions
Java basics
Java basics
Java basics
The behavior of JS running on `Java SE 8 Nashorn` suddenly changed
My thoughts on the equals method (Java)
Display text on top of the image
Get the result of POST in Java
[Challenge CircleCI from 0] Learn the basics of CircleCI
Memorandum of new graduate SES [Java basics]
[Java] Get the day of the specific day of the week
Memo: [Java] Check the contents of the directory
Kick ShellScript on the server from Java
Samshin on the value of the hidden field
Install OpenJDK (Java) on the latest Ubuntu
Now, I've summarized the basics of RecyclerView
part of the syntax of ruby ​​on rails
[# 1 Java] Basics of Java-Major premise before studying-
Easily measure the size of Java Objects
Output of the book "Introduction to Java"
Memory measurement of Java application on Windows
Execute Java code stored on the clipboard.
The story of writing Java in Emacs
[Java] Check the number of occurrences of characters
[Java] [Spring] Test the behavior of the logger
Basics of java basics ② ~ if statement and switch statement ~
What wasn't fair use in the diversion of Java APIs on Android
What is JSP? ~ Let's know the basics of JSP !! ~
[Ruby] Summary of class definitions. Master the basics.
Fat Can, Docker and I look back on the quotes of First Gundam
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
JAVA: jar, aar, view the contents of the file
Back calculation of the transition of the internal seed of Random
The story of making ordinary Othello in Java
[Android] [Java] Manage the state of CheckBox of ListView
About the description order of Java system properties
[Ruby on Rails] Until the introduction of RSpec
About the idea of anonymous classes in Java
The order of Java method modifiers is fixed
[Java] Access the signed URL of s3 (signed version 2)
The story of learning Java in the first programming
java programming basics
Measure the size of a folder in Java