[JAVA] Basic data type and reference type

What are variables of basic data type?

A variable whose value itself is contained in the variable

Memory usage area for basic data type variables

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.

What is a reference type variable?

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.

Referenced articles

https://nobuo-create.net/sanshougata/

Recommended Posts

Basic data type and reference type
Basic data types and reference types (Java)
Java basic data types and reference types
About Java basic data types and reference type memory
[Java] Data type ①-Basic type
Feel the basic type and reference type easily with ruby
Feel the basic type and reference type easily with ruby 2
Difference between primitive type and reference type
[Java] Difference between assignment of basic type variable and assignment of reference type variable
[Processing x Java] Data type and object-oriented programming
Java review ③ (Basic usage of arrays / reference type)
Java basic data types
Basic operators and operations
Java variable declaration, initialization, data type (cast and promotion)
Upcast and super type / subtype
Get Timestamp type data by truncating hours, minutes and seconds
Column type quick reference table
Pony and reference capability closure
Java programming (variables and data)
Rails reference type creation added
JavaScript overview and basic grammar
Ruby methods and classes (basic)
[Introduction to Java] Variables and types (variable declaration, initialization, data type)
[Java] Basic types and instruction notes
Java reference mechanism (stack and heap)
[PHP] Inheritance, protected and reference patterns
[Java] Data type / matrix product (AOJ ⑧ Matrix product)
Relationship between database and model (basic)
Java pass by value and pass by reference
Reading and writing Java basic files
What is a reference type variable?
Data linkage with Spark and Cassandra
TCP: Basic Principles and Application Architecture
About Java primitive types and reference types
[Java] Exception types and basic processing
Java and Swift comparison (2) Basic type / arithmetic expression / control syntax / function definition