Click vào từng dòng để xem ví dụ code · API methods chuyển đổi kiểu dữ liệu
String → Primitive
→ int
Integer.parseInt(str)throws NumberFormatException
int
int n = Integer.parseInt("42"); // n = 42int n = Integer.parseInt("-100"); // n = -100int hex = Integer.parseInt("FF", 16); // = 255 (cơ số 16)int bin = Integer.parseInt("1010", 2);// = 10 (cơ số 2)// ⚠ Throws NumberFormatException nếu chuỗi không hợp lệ
→ long
Long.parseLong(str)
long
long n = Long.parseLong("9876543210"); // 9876543210Llong n = Long.parseLong("-50"); // -50L
→ double
Double.parseDouble(str)
double
double d = Double.parseDouble("3.14"); // 3.14double d = Double.parseDouble("1e5"); // 100000.0float f = Float.parseFloat("3.14"); // 3.14f
→ boolean
Boolean.parseBoolean(str)
boolean
boolean b = Boolean.parseBoolean("true"); // trueboolean b = Boolean.parseBoolean("TRUE"); // true (case-insensitive)boolean b = Boolean.parseBoolean("yes"); // false ⚠ chỉ "true" mới đúngboolean b = Boolean.parseBoolean("1"); // false ⚠
→ char
str.charAt(index)
char
char c = "hello".charAt(0); // 'h'char c = "hello".charAt(4); // 'o'char last = s.charAt(s.length() - 1); // ký tự cuối// ⚠ Throws StringIndexOutOfBoundsException nếu index sai
→ char[]
str.toCharArray()
char[]
char[] chars = "hello".toCharArray();
// chars = ['h','e','l','l','o']for (char c : chars) {
System.out.print(c); // duyệt từng ký tự
}
Primitive / Object → String
→ String
String.valueOf(value)safe với null
String
String s = String.valueOf(42); // "42"
String s = String.valueOf(3.14); // "3.14"
String s = String.valueOf('A'); // "A"
String s = String.valueOf(true); // "true"
String s = String.valueOf(obj); // gọi obj.toString()
String s = String.valueOf(null); // "null" ✓ (không throw)
→ String
Integer.toString(n)cũng có Double, Long...
String
String s = Integer.toString(255); // "255"
String s = Integer.toString(255, 16); // "ff" (hex)
String s = Integer.toString(8, 2); // "1000" (binary)
String s = Double.toString(3.14); // "3.14"
String s = Long.toString(9876543210L); // "9876543210"
→ String
"" + valueshorthand, ít explicit hơn
String
String s = "" + 42; // "42" — gọi String.valueOf() internally
String s = "" + 'A'; // "A"
String s = "" + true; // "true"// ⚠ Kém explicit hơn String.valueOf() — tránh dùng trong code nghiêm túc
→ String
new String(charArray)
String
char[] chars = {'h', 'i'};
String s = new String(chars); // "hi"
String s = String.valueOf(chars); // "hi" — cách ngắn hơn
char ↔ int (ASCII / Unicode)
→ int
(int) chwidening cast
int
int n = (int) 'A'; // 65int n = (int) 'a'; // 97int n = (int) '0'; // 48// Không cần cast khi gán vào int:int n = 'A'; // 65 — auto wideningint diff = 'z' - 'a'; // 25 — char arithmetic
int d = Character.getNumericValue('7'); // 7int d = Character.getNumericValue('A'); // 10 (hex digit)// Cách ngắn hơn cho decimal digit:int d = '7' - '0'; // 7 ✓ phổ biến hơn
Integer obj = Integer.valueOf(42); // explicit boxing
Integer obj = 42; // autoboxing (Java 5+)// Dùng với collections:
List<Integer> list = new ArrayList<>();
list.add(42); // tự động autoboxed
unbox
obj.intValue()or autounboxing
int
Integer obj = 42;
int n = obj.intValue(); // explicitint n = obj; // autounboxing// Các kiểu khác:double d = obj.doubleValue();
long l = obj.longValue();
// ⚠ NullPointerException nếu obj == null
Format & Number Base
→ String
String.format(fmt, args)
String
String.format("%.2f", 3.14159) // "3.14" — 2 số thập phân
String.format("%05d", 42) // "00042" — pad 0 bên trái
String.format("%x", 255) // "ff" — hex lowercase
String.format("%X", 255) // "FF" — hex uppercase
String.format("%b", true) // "true" — boolean
String.format("%s", obj) // "..." — gọi toString()
String.format("%,d", 1000000) // "1,000,000" — với dấu phẩy
Integer.MAX_VALUE // 2,147,483,647 (2³¹ - 1)
Integer.MIN_VALUE // -2,147,483,648 (-2³¹)
Long.MAX_VALUE // 9,223,372,036,854,775,807
Double.MAX_VALUE // 1.7976931348623157E308
Double.MIN_VALUE // nhỏ nhất dương > 0, không phải âm!
Double.POSITIVE_INFINITY // Infinity
Double.NaN // Not a Number (0.0/0.0)
Khi nào dùng cái nào? parseInt khi biết chắc là số nguyên ·
String.valueOf khi không chắc kiểu (an toàn với null) ·
charAt để lấy 1 ký tự ·
toCharArray để duyệt từng ký tự ·
cast (int)/(char) cho ASCII math ·
String.format khi cần format đẹp