Java Data Types
Java Language Data Types
Variables can be classified into various forms according to the data types they can hold.
Primitive data types include integers, real numbers, characters, and boolean values. Reference data types include arrays, classes, and interfaces. A primitive variable stores the value of the primitive type it represents, while a reference variable stores a reference to the value, that is, a memory address. This difference appears when calling methods on other objects.
- Constant: data that always keeps a fixed value.
- Variable: data whose value changes depending on a specific situation.
- Data Type: the form of data specified to store changing data in constants or variables. Each data type has a different memory size depending on its kind.
Data Type Classification
Java data types consist of 8 primitive types and reference types.
They can be classified as follows.
- Java Data Type
- Primitive Type
- Boolean Type(boolean)
- Numeric Type
- Integral Type
- Integer Type(short, int, long)
- Floating Point Type(float, double)
- Character Type(char)
- Integral Type
- Reference Type
- Class Type
- String Type
- Wrapper Class
- Interface Type
- Array Type
- Enum Type
- Class Type
- Primitive Type
Primitive Type
Primitive types have the following characteristics.
- Primitive types must be declared before use.
- The length of the type does not change depending on the OS.
- Because they are not objects, they cannot have null.
The size and range of Java primitive types are as follows.
| Type | Description | Size(bit) | Range |
|---|---|---|---|
char |
16-bit Unicode character data | 16 | ‘\u0000’ ~ ‘\uFFFF’ |
boolean |
true/false value | 8 | true or false |
byte |
signed 8-bit integer | 8 | -128 ~ +127 |
short |
signed 16-bit integer | 16 | -32,768 ~ +32,767 |
int |
signed 32-bit integer | 32 | -2,147,483,638~+2,147,483,647 |
long |
signed 64-bit integer | 64 | -9223372036854775808~+9223372036854775807 |
float |
signed 32-bit floating point | 32 | -3.402932347e+38~+3.40292347e+38 |
double |
signed 64-bit floating point | 64 | -179769313486231570e+308~1.79769313486231570e+08 |
- Character type: char
- Boolean type: boolean
- Integer types: byte, short, int, long
- Floating-point types: float, double
BigInteger
When using the long type, the largest value is 9223372036854775807. If you need a number much larger than this, use BigInteger.
BigInteger cannot use normal operator syntax. Instead, it provides methods such as .add(), .subtract(), .multiply(), and .divide(), and the parameters also receive BigInteger.
Reference Type
Reference types are all data types except the 8 primitive types, and basically inherit from java.lang.Object.
A reference type variable does not store data directly; it stores only the memory address where the data is stored. In other words, a reference type variable stores data in another area and uses only its address.
Class Type
Unlike primitive types, class types refer to objects.
Let’s look at an example.
The object below is a class that simply has a str variable.
package com.devkuma.basic.datatype.ex1;
class MyObject {
private String str;
public MyObject(String str) {
this.str = str;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
The object below uses MyObject and displays the str value.
package com.devkuma.basic.datatype.ex1;
public class MyObjectMain {
public static void main(String[] args) {
MyObject a = new MyObject("a");
MyObject b = new MyObject("b");
// Display initial values
System.out.println(a.getStr()); // a
System.out.println(b.getStr()); // b
// Assign b to a
a = b;
System.out.println(a.getStr()); // b
// Assign value "c" to b's str
b.setStr("c");
System.out.println(a.getStr()); // c
}
}
Execution result:
a
b
b
c
When object b is first assigned to a and a is displayed, "b" is shown. Then, after assigning "c" to b’s str variable and displaying a, "c" is displayed.
This is because variables a and b do not hold the actual object, but the address of the object. Since a and b have the address of the same object, their values are the same even if one side changes.
String Type
Among class types, the String class is a little special. It belongs to reference types, but basic usage is like a primitive type. It is also an immutable object.
The String class has methods that appear to change values, but if data is changed through those methods, a new String object is created.
Usually primitive comparisons use the == operator, but comparisons between String objects should use the .equals() method.
Wrapper Class
Primitive types, as mentioned earlier, are not objects, so null cannot be assigned to them. With wrapper classes, null can be assigned.
Wrapper classes are objects that wrap primitive types in classes, so null can be declared.
| Primitive Type | Wrapper Class |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Char |
| boolean | Boolean |
Interface Type
Create an interface as follows. Here, T is a generic type and is optional.
interface MyInterface<T> {
void add(T value);
}
Creating an interface is like creating a new reference data type. Since an interface is also a data type, it can hold the address of an object that implements it. However, only the methods defined in the interface can be used.
Array Type
An array type can be created from primitive types or reference types.
int[] num1 = new int[2];
Integer[] num2 = new Integer[3];
You can specify an array by declaring [] for a data type. An array variable also holds the address of the array, so it has the same characteristics as a class type. Therefore, if it refers to the same object address, it points to the same array.
If nested brackets such as [][] are used as shown below, it can be used as a multidimensional array.
Object[][] obj = new Object[2][3];
Enum Type
An enum is a data type for handling multiple constants together.
Defining an Enum
[modifier] enum name {
member, ...
}
modifier: modifier(`public`, `strictfp` only)
name: enum name
member: enum constant
Use the enum keyword to declare an enum. Under the enum block, list names(enum constants) separated by commas. Because they are handled like constants, it is common to use underscore style(all uppercase, word boundaries separated by underscores).
For example, the following defines a WeekdayEnum enum that represents days of the week.
public enum WeekdayEnum {
Sunday,
Monday,
Tuesday,
Wednesday,
Tursday,
Friday,
Saturday
}
The defined enum can be accessed in the form typeName.constant, such as WeekdayEnum.Sunday.
Enum Methods
An enum is implicitly a kind of class that inherits from the Enum class in the java.lang package. The enum constant reference "WeekdayEnum.Sunday" is an Enum object.
The following methods can be used on Enum objects.
Methods of the Enum class
| Method | Summary |
|---|---|
String name() |
Returns the name of the enum constant |
int ordinal() |
Returns the order of the enum constant, starting from 0 |
toString() |
Returns the name of the enum constant |
E[] values() |
Returns all enum constants included in the enum |
For example, the following lists the names of the enum constants defined in the Weekday enum.
for (WeekdayEnum w : WeekdayEnum.values()) {
System.out.println(w.name());
}
Defining Constructors, Fields, and Methods
Because an enum is also a kind of class, constructors, fields, and methods can be defined under it.
public enum WeekdayEnum {
Sunday("sun"),
Monday("mon"),
Tuesday("tue"),
Wednesday("wed"),
Thursday("thu"),
Friday("fri"),
Saturday("sat");
// field
private String name;
// constructor
private WeekdayEnum(String name) {
this.name = name;
}
// method
@Override
public String toString() {
return this.name;
}
}
If you want an enum value to have a display value separate from its own identifier, declare a field(attribute) as in this example. Here, the abbreviated day name is stored in the name field.
Fields can be initialized by a constructor just like in a normal class. The syntax is basically the same as a class, but note that the access modifier is fixed to private, so omitting private in the sample code has the same meaning.
After defining the constructor, enum constants must be declared in the form enum constant(arguments, …). Put a semicolon “;” at the end of the enum definition.
Finally, define the method. Here, the toString method is defined so the value of the name field can be retrieved. The syntax is the same as a class.
The enum defined this way can be used as follows.
System.out.println(WeekdayEnum.Monday.toString()); // result: mon