韩顺平Java02(各数据类型注意事项)

程序中 +号 的使用

  1. 当左右都是数值类型时,做加法运算

  2. 当左右有一个为字符串时,做拼接运算

  3. 运算顺序为从左到右

System.out.println(100+3);//103
System.out.println("100"+3);//1003
System.out.println(100+3+"hello");//103hello
System.out.println("hello"+100+3);//hello1003

 

 

浮点类型注意事项

  • Java浮点数默认是double类型

float num = 1.2;//会报错
float num = 1.2f;//正确写法 

类型转换时小单位 可以赋值给大单位,反之不可

  • 通常情况下应该使用double类型,因为它比float更精确


double num1 = 2.1234567851;
float  num2 = 2.1234567851F;
System.out.println(num1);//2.1234567851
System.out.println(num2);//2.1234567
  • 浮点数使用陷阱:8.1/3与2.7比较

看一段代码:


double num11 = 2.7;
double num21 = 8.1 / 3;
System.out.println(num11);//2.7
System.out.println(num21);//2.6999999999999997  

由此我们得到一个重要的使用点:

不要使用运算结果是小数的值直接进行相等判断,而是应该使用两数差的绝对值在一定精度范围内进行比较。

//错误的比较方法
if (num11 == num21) {
            System.out.println("相等");
                }
​
//相对妥当的比较方法
if (Math.abs(num11 - num21) < 0.001) {
            System.out.println("相等");
        } 
  • 如果是直接查询或者赋值获得的小数是可以直接判断相等的


double a = 2.7;
double b = 2.7;
if (a == b) {
            System.out.println("相等");
                }
  • java是强类型语言,变量必须先声明再调用

字符型(char)注意事项

  • char类型本质是一个整数,有对应的Unicode编码,可以参与运算

 char c = '聂';
        System.out.println((int)c);//32834
        System.out.println(c+123);//32957
字符编码表:

 

ASCII码对照表

布尔(boolean)型注意事项

  • Java中不可以使用0或者非0的整数来代替布尔值的true和false。(boolean只能是true或者false)
  • Boolean(对象)的默认值为null。
  • Boolean(原始)的默认值为false

自动类型转换(转换:convert)

 

注意细节:

  • 有多种类型的数据进行混合运算时,系统首先会将所有数据转换成容量最大的那种数据类型然后再进行计算。

int n1 = 10;
float f1 = n1 + 1.1;//编译会报错,因为这里的1.1为double类型
float f1 = n1 + 1.1f;//正确写法 
  • 把精度(容量)大的数据类型赋值给精度小的数据类型时会报错,反之则会进行自动类型转换。

  • (byte、short)和char类型之间不能相互自动转换

byte b = 1;
char c = b; //错误
  • byte、short和char类型之间相互计算(或者各自单独计算),在计算时首先将类型转换为int类型

byte b1 = 1;
byte b2 = 2;
short s = 1;
short s1 = b + s; //错误 b + s = > int 不能赋值给short 
byte b3 = b1 + b2 //错误 不兼容的类型: 从int转换到byte可能会有损失
  • boolean类型的值不参与类型的自动转换

boolean flag = true;
int a =flag; // 错误 不兼容的类型: boolean无法转换为int
  • 自动提升原则:表达式结果的类型自动提升为操作数中最大的类型(必须使用最大的类型来接收)


byte b = 12;
short s = 6;
int i = 45;
float f = 1.2f;
double d = 3.14;
//想接收以上所有数的和必须使用 double 类型的变量
float sum = b + s + f + d //错误 从double转换到float可能会有损失
 

强制类型转换

  • 自动类型转换的逆过程,是将大容量的数据类型强行转换为小容量的数据类型,可能会造成精度降低或者数据溢出,需加上强制类型转换符()。

int i = (int) 1.9;
System.out.println(i);//1
int j = 1000;
byte b = (byte) j;
System.out.println(b); //-24
  • 强制转换符号指针对最近的操作数有效,通常会加上括号来提升优先级

 

int x = (int)10*3.5 + 6*1.2 //错误 因为只转换了前面的10*3.5 整体还是double类型
int x = (int)(10*3.5 + 6*1.2) //正确写法 
  • char类型可以保存 int 类型的常量值,但是不可以保存 int 的变量值,需要强制转换

 

char c1 = 100;
int n = 100;
char c2 = n; //错误
char c2 = (char)n; //正确
System.out.println(c2); // d
  • 练习:判断能否通过编译

short s = 12;//
s - = 9; //x  int-> short
byte b = 10; //
b + = 11 ; //x byte -> int
short s1 = s + b; //x int -> short

基本数据类型和String的相互转换

  • 基本数据类型不能强制转换为String类型

  • 基本数据类型 -> String ,在后边拼接“ ”即可
float f = 1.2f;
String s1 = f1 + " ";
  • String转换为对应的基本数据类型

String s = "1234";
int num = Integer.parseInt(s); 
double d = Double.parseDouble(s);//以此类推 
  • 字符串转换为字符类型:含义为取得字符串的第一个字符


System.out.println(s.charAt(0)); //1
  • 注意String转换为基本数据类型时要确保转换的字符串有意义,否则会抛出异常

String s = "hello";
int num = Integer.parseInt(s);
System.out.println(num);
//Exception in thread "main" java.lang.NumberFormatException: For input string: "hello"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at basic.bilibili.chapter01.Homework01.main(Homework01.java:9)

 自动强制类型转换

对于 short s1 = 1; s1 = s1 + 1;编译出错,由于 1 是 int 类型,因此 s1+1 运算结果也是 int 型,需要强制转换类型才能赋值给 short 型。

而 short s1 = 1; s1 += 1;可以正确编译,因为 s1+= 1;相当于 s1 = ((short)(s1 + 1));其中有隐含的强制类型转换

posted @ 2021-12-04 00:34  紫英626  阅读(48)  评论(0编辑  收藏  举报

紫英