[Java] Difference between assignment of basic type variable and assignment of reference type variable

Domo is Fugito. This time as well, it is a post that also serves as a review of myself.

"Assignment to variable"

"Assignment to variable" is to set some value to variable To do.

Assignment to "basic type variable"

A basic type variable is a "variable that holds the value itself". For example, if you assign 3 to the int type variable number, the number will be The value to be retained is 3.


 int number = 3; // Substitute integer 3 for number

Assignment to "reference type variable"

A reference variable is "allocated in computer memory. Reference to a certain area (object) (object location) A variable that holds (positional information) that indicates.


 String word = "Qiita"; // Assign the string "Qiita" to the variable word?

In the above example, the character string "Qiita" is assigned to the String type variable word. Looks like it is. In other words, the value held by the variable word Looks like the string "Qiita". However, in reality, the value held by the variable word is "String type of A reference to the project. " That is, "computer memory Location information of the area for storing the character string "Qiita" secured above " The variable word has.

The difference in the nature of the value that a variable has is that "the variable is transferred to another variable. It appears visibly when "substituting".

"Assign that variable to another variable"

int i = 21;
 int k = i; // Assign the value of variable i to int type variable k
 int i = 30; // Reassign 30 to variable i

 //StringBuilder型変数sb1(参照型変数)を宣言
StringBuilder sb1 = new StringBuilder();
 sb1.append ("Qiita"); // Append the string "Qiita" to the object referenced by sb1
 StringBuilder sb2 = sb1; // Substitute the value of sb1 for sb2
 sb1.append ("★"); // Append the string "★" to the object referenced by sb1

 System.out.println (i); // Output "30"
 System.out.println (k); // Output "21"
 System.out.println (sb1); // Output "Qiita ★"
 System.out.println (sb2); // Output "Qiita ★"

A basic type variable is a "variable that holds the value itself". for that reason, In the above example, when i is assigned to the int type variable k, at that point The value (21) held by i is assigned and held. Therefore, even if 30 is reassigned to i, the value of k does not change and remains at 21. Will be. On the other hand, a reference type variable is a "variable that holds a reference to an object". It is. Therefore, when sb1 is substituted for sb2, "the o that sb1 refers to "Reference to object" will be assigned as a value. wife And "position information of the object held by sb1", also sb2 It will also be retained. Therefore, the object referenced by sb1 When the character string "★" is added to the output and then output, the same object There is also a change in sb2 that refers to.

Summary

-Basic type variable holds "value itself" as a value -Reference type variable holds "reference to object" as a value ・ The difference in the properties of the two types of variables is that "the variable (value) in another variable" Becomes apparent when "is substituted"

... Then this time around here. It was Puyi.

(P.S.) If you have any suggestions for the content of the article Please do not hesitate to let me know m (_ _) m

Recommended Posts

[Java] Difference between assignment of basic type variable and assignment of reference type variable
Difference between primitive type and reference type
[Java] Difference between "final variable" and "immutable object"
Java review ③ (Basic usage of arrays / reference type)
Basic data type and reference type
[Java] Difference between == and equals
About Java basic data types and reference type memory
[Docker-compose] Difference between env_file and environment. Priority of environment variable application
[Java] Difference between Hashmap and HashTable
Basic data types and reference types (Java)
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
[Java] Difference between array and ArrayList
Java basic data types and reference types
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between Stack Overflow Error and Out Of Memory Error
[Java] Difference between length, length () and size ()
Difference between final and Immutable in Java
Difference between Ruby instance variable and local variable
[Note] Cooperation between Java and DB (basic)
[Basic knowledge of Java] About type conversion
Difference between addPanel and presentModally of FloatingPanel
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
[Java] Understand the difference between List and Set
[Java] Personal summary of classes and methods (basic)
Difference between next () and nextLine () in Java Scanner
[Ruby] "Reference to object" and "Contents of variable"
Difference between member and collection of rails routes.rb
progate java L1 basic summary console.log, difference between variables and constants, if statement
[Java] Data type ①-Basic type
Difference between vh and%
Difference between i ++ and ++ i
Feel the basic type and reference type easily with ruby
Java variable declaration, initialization, data type (cast and promotion)
[Java] Difference between static final and final in member variables
[JAVA] What is the difference between interface and abstract? ?? ??
Feel the basic type and reference type easily with ruby 2
What is the difference between Java EE and Jakarta EE?
[Java beginner] Difference between length and length () ~ I don't know ~
Difference between product and variant
Difference between redirect_to and render
Difference between Java and JavaScript (how to find the average)
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
[Ruby] About the difference between 2 dots and 3 dots of range object.
Difference between class and instance
Difference between bundle and bundle install
[Java] Check the difference between orElse and orElseGet with IntStream
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
java Generics T and? Difference
Advantages and disadvantages of Java
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to