java变量,数据类型,转换
迪丽瓦拉
2025-05-31 13:09:06
0
  1. 变量是程序的基本组成单位
  2. int占用4个字节,double占用8个字节
  3. 变量在同一个作用域内不能重名
  4.  +号的使用
    1. 当左右两边都是数值型时,则做加法运算
    2. 当左右两边有一方为字符串,则做拼接运算
      public class add{public static void main(String[] args) {System.out.println(10 + 10);System.out.println("100" + 93);System.out.println(100 + 3 + "hello");System.out.println("hello" + 100 + 3);}
      }
      /*
      20
      10093
      103hello
      hello1003
      **/
  5. java数据类型
    1. 基本数据类型(8大数据类型)
      1. 数值型
        1. 整数类型:byte[1],short[2],int[4],long[8]
        2. 浮点(小数)类型:float[4],double[8]
      2. 字符型:char[2],存放单个字符'a'
      3. 布尔型:boolean[1],存放true,false
    2. 引用数据类型
      1. 类(class)
      2. 接口(interface)
      3. 数组([ ])
  6. byte存放的范围-128~127。
  7. java各整数类型有固定的范围和字段长度,不受具体OS的影响,以保证java程序的可移植性。
  8. java的整型常量默认为int型,声明long型常量需后加‘I’或‘L’
  9. bit:计算机中最小的存储单位
  10. 浮点数在机器中存放的形式的简单说明,浮点数=符号位+指数位+尾数位
    1. 浮点型常量默认为double型,声明float型常量,需后加‘f’或‘F’
    2. float num1 = 1.1f;
    3. 科学计数法  5.12e2(5.12*10的2次方)5.12E-2(5.12/10的2次方)
  11. 当我们对运算结果是小数的进行相等判断时要小心。
  12. char类型是可以运算的,相当于一个整数,因为它有对应的Unicode码
  13. 字符型存储到计算机中,需要将字符对应的码值(整数)找出来,比如‘a’
    1. 存储:‘a’==>码值97==>二进制(1100001)==>存储
    2. 读取:二进制(1100001)==>97==>'a'==>显示
  14. 字符编码表
    1. ASCII编码表:一个字节表示,有128个字符,实际上一个字节可以表示256个字符,只用了128个(缺点:不能表示所有字符)。
    2. Unicode:固定大小的编码,使用两个字节来表示字符,字母和汉字统一都是占用两个字节,这样浪费空间
    3. UTF-8:大小可变的编码,字母用一个字节,汉字用三个字节。(可以认为是Unicode的一种改进)
    4. gbk:字母占用1个字节,汉字2个字节
  15. public class CharDetail{public static void main(String[] args) {char c1 = 'a';System.out.println(c1);//aSystem.out.println((int)c1);//97char c2 = 97;System.out.println(c2);//aSystem.out.println('a' + 10);//107char c5 = 'b' + 1;System.out.println((int)c5); //99System.out.println(c5);  //c}
    }

  16. boolen类型:只能赋false和true,不可以0或非0的整数代替false和true,这点和C语言不同。
  17. 基本数据类型转换
    1. 当java程序在进行赋值或者运算时,精度小的类型自动转换为精度大的数据类型
    2. 当我们把精度大的数据类型赋值给精度小的数据类型时就会报错。
    3. 当把具体数赋给byte时,(1)先判断该数是否在byte范围内,如果是就可以。
    4. (byte,short)和char之间不会相互自动转换
    5. byte,short,char三者可以计算,在计算时首先转换为int类型
    6. 自动提升原则:表达式结果的类型自动提升为操作数中最大的类型。
    7. char==》int==》long==》float==》double
    8. byte==》short==》int==》long==》float==》double
    9. public class Convert{public static void main(String[] args) {int num = 'a';double d1 = 80;System.out.println(num);//97System.out.println(d1);//80.0//float a1 = 1.1f;float d2 = 2 + 1.1f;System.out.println(d2);//3.1}
      }

  18. 强制类型转换:将容量大的数据类型转换为容量小的数据类型,强制转换符()。
  19. 基本类型转String类型:将基本类型的值+""即可
  20. String类型转基本数据类型:通过基本类型的包装类调用parseXX方法即可。
  21. public class Excecise{public static void main(String[] args) {int n1 = 100;float n2 = 1.1f;double n3 = 3.4;boolean n4 = true;String s1 = n1 + "";String s2 = n2 + "";String s3 = n3 + "";String s4 = n4 + "";System.out.println(s1 + " " + s2 + " " + s3 + " " + s4);//100 1.1 3.4 true//String->对应的基本数据类型String s5 = "123";int num1 = Integer.parseInt(s5);float num2 = Float.parseFloat(s5);System.out.println(num1);//123System.out.println(num2);//123.0boolean num3 = Boolean.parseBoolean("true");System.out.println(num3);//true//把字符串转成字符charSystem.out.println(s5.charAt(0));//得到s5字符串的第一个字符,1}
    }

  22. public class Homework{public static void main(String[] args) {char c1 = '\n';char c2 = '\t';char c3 = '\r';char c4 = '\\';char c5 = '1';char c6 = '2';char c7 = '3';System.out.println(c1 + " " + c2 + " " + c3 + " " + c4 + " " + c5 + " " + c6 + " " + c7);System.out.println("===========================");String book1 = "红楼梦";String book2 = "西游记";System.out.println(book1 + book2);//红楼梦西游记char c8 = '男';char c9 = '女';System.out.println(c8);//男System.out.println(c9);//女System.out.println(c8+c9);//52906float price1 = 1.1f;float price2 = 2.2f;System.out.println(price2+price1);//3.3000002System.out.println("===========================");	String name = "贾树行";int age = 29;double force = 80.5;char gender = '男';String like = "学习";System.out.println("姓名\t年龄\t成绩\t性别\t爱好\n" + name + '\t' + age + '\t' + force + '\t' + gender + '\t' +like);/*
    姓名    年龄    成绩    性别    爱好
    贾树行  29      80.5    男      学习*/}
    }

相关内容