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 |
---|
String |
Array |
Class |
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
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
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
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
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