Difference between primitive type and reference type

Note

Primitive type (basic data type) and reference type

There are two main types of Java. ** Primitive type ** and ** Reference type **. I tried to summarize each.

What is a primitive type?

The following eight are primitive types.

Mold Classification Bit number
boolean Boolean value 1
byte integer 8
short integer 16
char letter 16
int integer 32
float Decimal 32
long integer 64
double Decimal 64

When declaring a variable of a primitive type, the value can be stored in the variable at the same time as the declaration. The value 1 is stored in the variable a below.

int a = 1;

What is a reference type?

Reference types are types other than the above eight. For example, Strings and arrays are reference types.

Hoge hoge = new Hoge();
String str = "fuga";

In the reference type, the variable does not store the value as it is, but stores the memory location where the value is stored.

Difference between basic type and reference type seen by reassignment

There is a difference in the behavior of reassignment between the basic type and the reference type.

The sample code below is the code that reassigns the basic type. You can see this intuitively.

kihon.java


int a = 1;
int b = a; //The value of a: 1 is stored in b
a = 2;

Systemout.println(a); //2 is output
Systemout.println(b); //1 is output

On the other hand, in the reference type, it is as follows. In the same way as the basic type, b [0] seems to output 1. However, with reference types, variables store only the location of the memory where the data is stored.

When the array a is declared, a stores the memory location information in which the array information {1, 2, 3} is stored. When the array b is declared, b also stores the memory location of the array {1, 2, 3} that a has.

At this time, both a and b are variables that contain the same memory location information.

ʻA [0] = 0; replaces the 0th value of the array {1, 2, 3} in the memory location stored in a with 0. The array stored in memory is now {0, 2, 3}`.

Therefore, even if you look at the 0th array from the memory location stored in b, the output value will be 0.

sansyo.java


int[] a = {1, 2, 3};
int[] b = a;
a[0] = 0;

Systemout.println(a[0]); //0 is output
Systemout.println(b[0]); //0 is output

Recommended Posts

Difference between primitive type and reference type
[Java] Difference between assignment of basic type variable and assignment of reference type variable
Difference between vh and%
Difference between i ++ and ++ i
Upcast, Dowcast and Constructor
Basic data type and reference type
Difference between primitive type and reference type
Difference between product and variant
Basic data type and reference type
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
[Java] Difference between equals and == in a character string that is a reference type
[Ruby] Difference between get and post
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
[Rails] Difference between find and find_by
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
About Java primitive types and reference types
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update
Difference between Ruby instance variable and local variable
Difference between pop () and peek () in stack
[For beginners] Difference between Java and Kotlin
Difference between isEmpty and isBlank of StringUtils
Difference between getText () and getAttribute () in Selenium
About the difference between irb and pry
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
Difference between addPanel and presentModally of FloatingPanel
[Ruby] Difference between print, puts and p
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
[Rails] Difference between redirect_to and render [Beginner]
[iOS] Understand the difference between frame and bounds