A variable whose value itself is contained in the variable
The basic data types are the following eight types.
type | Bit number | Description |
---|---|---|
boolean | 1bit | true or false |
byte | 8bit | Signed integer-128~127 |
char | 16bit | Unicode single character |
short | 16bit | Signed integer-32768~32767 |
int | 32bit | Signed integer-2147483648~2147483647 |
long | 64bit | Signed integer about-922 Kyo-about 922 Kyo |
float | 32bit | Floating point number |
double | 64bit | Floating point number |
Variables of the basic data type allocate the required memory area when defined. The value is assigned and held as it is in the secured memory area.
Suppose you execute the following code.
Basic data type
int x = 10;
int y = x;
y = 20;
System.out.println(x);
System.out.println(y);
The output is as follows.
output
10
20
In this way, the variable of the basic data type stores the value itself in the variable. Therefore, the content of y
changes from 10 to 20, and 20 is finally output.
A variable that holds the location (memory address) where the value is stored, rather than storing the value itself. Unlike the basic data type, if you do not know what value will be assigned, you do not know how much memory area should be reserved. In addition, the memory area once reserved for the variable in the memory cannot be changed significantly later. Therefore, these reference variables are assigned code that indicates the location of the values created in other parts of memory. The code that indicates this location is called the reference value.
Suppose you run the following code.
Reference type
int a[] = { 1,2,3 };
int b[] = a;
b[0] = 2;
System.out.println(a[0]);
The output is as follows.
output
2
This is a characteristic part of the behavior of reference variables.
Since the code stored in b []
indicates the location of ʻa [] , changing the contents of
b [] changes the contents of ʻa []
at the same time. I will end up.
https://nobuo-create.net/sanshougata/
Recommended Posts