A variable is a “labeled box” that holds data. Java is a static type language, so you must decide in advance what type of value each variable will contain at the time of declaration. In this section, we look at eight Java primitives, type conversion, and the var keyword of JDK 10+.
Learning Objectives
- Know the sizes and default values of eight primitive types
- Understand when type conversion (implicit and explicit) occurs
- Distinguishes between locations where
varcan be used and locations where it cannot be used. - Know the difference between
Stringliteral andnew String()
Key concepts
1) 8 types of Primitive
| Type | size | default | range |
|---|---|---|---|
byte |
1 byte | 0 | -128 ~ 127 |
short |
2 byte | 0 | -32,768 ~ 32,767 |
int |
4 bytes | 0 | Approximately ±2.1 billion |
long |
8 bytes | 0L | Approximately ±9 × 10^18 |
float |
4 bytes | 0.0f | IEEE 754 single precision |
double |
8 bytes | 0.0d | IEEE 754 double precision |
boolean |
JVM dependency | false |
true / false |
char |
2 bytes | ' ' | UTF-16 code units |
int age = 21;
long population = 50_000_000L; //long literals have an L at the end
double pi = 3.14159;
boolean done = false;
char grade = 'A';
2) Type conversion
int i = 100;
double d = i; //int → double : implicit (value preservation)
int back = (int) d; //double → int : explicit (truncate decimal point)
Small type → large type is automatically converted, but the reverse requires explicit casting due to the possibility of data loss.
3) var keyword (JDK 10+)
var name = "exponent"; //Inference with String
var nums = new int[]{1, 2, 3};
- Can only be used in local variables (field/method signatures are not allowed)
- Since the type is inferred from the initial value, value must be present at the same time as declaration.
- Recommended to use only when it helps readability
4) String literal vs new String()
String a = "hello";
String b = "hello";
String c = new String("hello");
System.out.println(a == b); //true (string pool shared)
System.out.println(a == c); //false (another object)
System.out.println(a.equals(c)); //true (content comparison)
See as example
Example 1 — Primitives.java: 8 primitive outputs
public class Primitives {
public static void main(String[] args) {
byte b = 10;
short s = 200;
int i = 100_000;
long l = 9_999_999_999L;
float f = 3.14f;
double d = 3.141592653589793;
boolean t = true;
char c = 'A';
System.out.printf("byte=%d, short=%d, int=%d, long=%d%n", b, s, i, l);
System.out.printf("float=%.2f, double=%.6f%n", f, d);
System.out.printf("boolean=%b, char=%c(%d)%n", t, c, (int) c);
}
}
Execution result
byte=10, short=200, int=100000, long=9999999999
float=3.14, double=3.141593
boolean=true, char=A(65)
Note: Large numbers can be placed as _ (100_000).
Example 2 — Conversion.java: Casting trap
public class Conversion {
public static void main(String[] args) {
int i = 100;
double d = i;
System.out.println("int -> double: " + d);
double pi = 3.99;
int truncated = (int) pi;
System.out.println("double -> int (cut):" + truncated);
int big = 1_000;
byte overflow = (byte) big;
System.out.println("byte overflow:" + overflow);
}
}
Execution result
int -> double: 100.0
double -> int (cut): 3
byte overflow: -24
Memo: If it is outside the byte range (-128 to 127), bits are truncated and unpredictable values are produced.
Example 3 — VarKeyword.java : possible/impossible case using var
import java.util.ArrayList;
import java.util.List;
public class VarKeyword {
public static void main(String[] args) {
var name = "exponent";
var list = new ArrayList<String>();
list.add("Java");
list.add("Spring");
for (var s : list) {
System.out.println(s);
}
//var x; // Compilation error: initial value required
//var n = null; // Compilation error: type inference not possible
List<Integer> nums = List.of(1, 2, 3);
System.out.println("Name:" + name);
System.out.println("The types are:" + nums.getClass().getSimpleName());
}
}
Execution result
Java
Spring
Name: Jisoo
The type is: ListN
Memo: Field/parameter/return-type var cannot be used. This is limited to local variables.
Example 4 — StringBasics.java: String literal vs new String()
public class StringBasics {
public static void main(String[] args) {
String a = "hello";
String b = "hello";
String c = new String("hello");
System.out.println("a == b : " + (a == b));
System.out.println("a == c : " + (a == c));
System.out.println("a.equals(c) : " + a.equals(c));
System.out.println("length : " + a.length());
System.out.println("upper : " + a.toUpperCase());
}
}
Execution result
a == b : true
a == c : false
a.equals(c) : true
length : 5
upper : HELLO
Note: Always use .equals()** to compare string **contents in Java. == is a reference (address) comparison.
Common Mistakes
- Omitting
Lin thelongliteral → interpreted asintand overflow - Omit
finfloatliteral → Default isdouble, casting required - I forgot that
int / intis integer division and got confused by1 / 2 == 0. - Not knowing that
charis compatible with small integers (0~65535) - Use
==for string comparison
Cleanup
- Debugging is easy if you memorize Java's 8 primitives and their default values.
- The principle of type conversion is small → large automatically, large → small explicit casting.
varis only a readability tool, not an all-purpose keyword.- The string content is
.equals(), the reference is==
Try it yourself
cd 01_Basics/02_Variables-and_Types/src
javac Primitives.java
java Primitives