Java array variables are reference types

Java array variables are reference types.

I think it's a natural basic thing, but since I was originally doing PHP, For my own memorandum, I summarized it a little.

For example, in the case of the following code, 100 is output.

int[] a = { 1, 2, 3 };
int[] b = a;
b[0] = 100;
// a[0]Outputs 100.

This is because Java array variables are reference types, so an array was created for int [] a Because the memory address is stored.

For example, if the memory address of the array created by int [] a is ABC123 (I don't think there is actually such a memory address), The array a is recorded at this memory address (location) called ABC123.

And since ABC123, which is the memory address of the array variable int [] a, is contained in int [] b, the point is that it is the same array. Therefore, b [0] = 100 is the same as a [0] = 100.

If you write the same code below in PHP, 1 will be output instead of 100.

$a = [1,2,3];
$b = $a;
$b[0] = 100;
echo $a[0];
//1 is output

This is not a reference type in PHP, it is passed by value, so This is because the array of \ $ a itself is copied and passed to \ $ b, not the memory address of \ $ a.

In PHP, in order to make it a reference type, it is necessary to add \ & as shown below.

$a = [1,2,3];
$b = &$a;
// ↑$Before a&Put on
$b[0] = 100;
echo $a[0];
//100 is output

By doing this, it becomes a reference type, and \ $ b contains the memory address of the array created by \ $ a. Since it is stored, the operation is the same as in the case of Java above.

Recommended Posts

Java array variables are reference types
[Java] Variables and types
[Java] array
Why are class variables needed? [Java]
Java array
Java array
java (array)
Java Primer Series (Variables and Types)
Java array
[Java] Array
Java primitive types, reference types, Immutable, Mutable
Java array
About Java primitive types and reference types
Java basic data types and reference types
java array
[Java] Array
Java local variables are thread safe
Java starting from beginner, variables and types
[Java] output, variables
java array variable
[Java] Array notes
Java8 method reference
[WIP] Java variables
java8 method reference
JAVA reference materials
My Java reference
When there are environment variables in Java tests
What I learned in Java (Part 2) What are variables?
printf / arguments / variables / types
Java review ① (development steps, basic grammar, variables, data types)
About Java Array List
Whether Java arguments are passed by value or by reference
About Java basic data types and reference type memory
[Java] Main data types
[Java Silver] What are class variables instance variables and local variables?
Java basic data types
If variables are no longer highlighted in Eclipse's Java editor
Generate Stream from an array of primitive types in Java
[Java] Integer wrapper class reference
[Java] List type / Array type conversion
Java service reference halfway
(Note) Java classes / variables / methods
Java equals are pretty unpleasant
Java programming (variables and data)
About Java class loader types
I investigated Java primitive types
[Environment variables] What are rails environment variables?
What are practically final variables?
About Java class variables class methods
Java Development Basics ~ Exercise (Array) ~
How to use Java variables
[Java] Convert ArrayList to array
[Java Silver] Array generation method
How to initialize Java array
[Beginner] Java basic "array" description
Java Learning 1 (learning various data types)
What are Java metrics? _Memo_20200818
[Introduction to Java] Variables and types (variable declaration, initialization, data type)
Basics of Java development ~ How to write programs (variables and types) ~