VariableTest1.java.bak 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Java定义的数据类型
  3. 一、变量安装数据类型来分:
  4. 基本数据类型
  5. 整型:byte short int long
  6. 浮点型: float double
  7. 字符型:char
  8. 布尔型:Boolean
  9. 引用数据类型:
  10. 类(class)
  11. 接口(interface)
  12. 数组(array)
  13. */
  14. class VariableTest1{
  15. public static void main(String[] args){
  16. //1、整型:byte(1字节=8bit) short(2字节) int(4字节) long(8字节)
  17. //(1)byte范围:-128~127
  18. byte b1 = 12;
  19. byte b2 = -128;
  20. //b2=128;//编译不通过
  21. System.out.println(b1);
  22. System.out.println(b2);
  23. //(2)声明long型变量,必须以“l”或“L”结尾
  24. //(3)通常,定义整型变量时,使用int型
  25. short s1 = 128;
  26. int i1 = 1234;
  27. long l1 = 132356464;
  28. //2.浮点型:float(4字节)、double(8字节)
  29. //(1)浮点型,表示带小数点的数值
  30. //(2)float表示数值的范围比long还大
  31. double d1 = 123.3;
  32. System.out.println(d1+1);
  33. //(3)定义float类型变量时,变量要以“f”或“F”结尾
  34. float f1 = 12.3f;
  35. System.out.println(f1);
  36. //(4)通常,定义浮点型变量时,使用double类型
  37. //3、字符型
  38. //(1)定义char型变量,通常使用一对'',内部只能写一个字符
  39. char c1 = 'a';
  40. //c1 = 'AB';
  41. System.out.println(c1);
  42. char c2 = '1';
  43. char c3 = '好';
  44. char c4 = 'め';
  45. System.out.println(c2);
  46. System.out.println(c3);
  47. System.out.println(c4);
  48. //(2)表示方式:1、声明一个字符 2、转义字符 3、直接使用Unicode值来表示字符型常量
  49. char c5 = '\n';//换行符
  50. c5 = '\t';//制表符
  51. System.out.print("hello"+c5);
  52. System.out.println("world");
  53. char c6 = '\u0123';
  54. System.out.println(c6);
  55. }
  56. }