Mã dưới đây cho thấy cách dùng dấu gạch dưới trong chữ số số trong Java:
Ví dụ
public class Demo{ public static void main (String[] args) throws java.lang.Exception{ int my_num_1 = 6_78_00_120; System.out.println("The number is : " + my_num_1); long my_num_2 = 2_00_11_001; System.out.println("The number is : " + my_num_2); float my_num_3 = 4.01_981F; System.out.println("The number is : " + my_num_3); double my_num_4 = 12.89_46_061; System.out.println("The number is : " + my_num_4); } }
Kết quả
The number is : 67800120 The number is : 20011001 The number is : 4.01981 The number is : 12.8946061
Lớp Demo chứa hàm main sử dụng các định dạng integer, long, float, double với dấu gạch dưới. Gán cho biến, in ra console.