Java basic data types and reference types

Basic data type

Basic data type Wrapper class
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

The wrapper class has convenient methods.

java


int x = Integer.valueOf("99");
int y = Integer.parseInt("88");

System.out.println(x);
System.out.println(y);
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);

Reference type

Reference type
String
Array
Class

Check the difference between the basic data type and the reference type.

For basic data types

java


public static void main(String[] args) {
	int x = 10;
	int y = x;
	y = 5;
	System.out.println(x); 
	System.out.println(y);
}
10
5

For reference type

java


public static void main(String[] args) {
	int[] a = {3, 5, 7};
	int[] b = a;
	b[1] = 8;
	System.out.println(a[1]);
	System.out.println(b[1]);
}

Both are 8. This is because both a and b have the same memory address.

8
8

What about wrapper classes?

java


public static void main(String[] args) {
	Integer x = 10;
	Integer y = x;
	y = 5;
	System.out.println(x);
	System.out.println(y);
}
10
5

What happens in the case of String type?

java


public static void main(String[] args) {
	String x = "hello";
	String y = x;
	y = "world";
	System.out.println(x);
	System.out.println(y);
}

Although it is a reference type, it is displayed like a basic type. The character string cannot be changed, so if you assign a different character string, new data will be secured in another area.

hello
world

About boxing and unboxing

Some Java classes only accept reference type arguments, so wrapper classes and basic data types Need to be converted to each other.

Auto boxing to put in reference type

java


Integer i = new Integer(32);
Integer j = 32;

auto unboxing

java


Int n = i; 
int m = j.intValue();

Recommended Posts

Basic data types and reference types (Java)
Java basic data types and reference types
About Java basic data types and reference type memory
Java basic data types
Basic data type and reference type
[Java] Basic types and instruction notes
About Java primitive types and reference types
[Java] Exception types and basic processing
[Java] Data type ①-Basic type
[Java] Variables and types
About Java data types (especially primitive types) and literals
Java programming (variables and data)
Java Learning 1 (learning various data types)
Java review ① (development steps, basic grammar, variables, data types)
Java variable declaration, initialization, and types
Java primitive types, reference types, Immutable, Mutable
Java reference mechanism (stack and heap)
Java array variables are reference types
Java pass by value and pass by reference
[Java] Difference between assignment of basic type variable and assignment of reference type variable
[Introduction to Java] Variables and types (variable declaration, initialization, data type)
Java starting from beginner, variables and types
[Note] Cooperation between Java and DB (basic)
[Introduction to Java] Variable declarations and types
Page number logic and reference code (java)
Java basic grammar
Java basic grammar
Java8 method reference
Java basic knowledge 1
[Java] Basic structure
[Java] [Basic] Glossary
Java basic grammar
Java and JavaScript
XXE and Java
Java basic grammar
java8 method reference
My Java reference
Java exercises [Basic]
[Java] Personal summary of classes and methods (basic)
[Processing x Java] Data type and object-oriented programming
Java review ③ (Basic usage of arrays / reference type)
Feel the basic type and reference type easily with ruby
Getters and setters (Java)
[Java] Types of comments and how to write them
[Java] Thread and Runnable
Java variable declaration, initialization, data type (cast and promotion)
java basic knowledge memo
Vectorize and image MNIST handwritten digit image data in Java
Java basic date manipulation
Going back to the beginning and getting started with Java ① Data types and access modifiers
Java basic naming conventions
Java learning memo (basic)
Java --Serialization and Deserialization
[Java] Arguments and parameters
I summarized the types and basics of Java exceptions
Feel the basic type and reference type easily with ruby 2
timedatectl and Java TimeZone
[Java] Branch and repeat
Equivalence comparison of Java wrapper classes and primitive types
[Java] Basic method notes
A memorandum about table data types and commands (Rails)