Domo is Fugito. This time as well, it is a post that also serves as a review of myself.
"Assignment to variable" is to set some value to variable To do.
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
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".
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.
-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