《Java核心技术卷1》拾遗

  之前对Java的基础知识有过学习,现在开始学习《Java核心技术卷1》,将一些新学的知识点,做简要记录,以备后续回顾:

 

1、double

(1)所有的“非数值”都认为是不相同的

if(x==Double.NaN)  //永远是true

(2) 可以通过Double.isNaN 来判断是不是“非数值”

public static void main(String[] args) {
     int a=9;
         float c=3.4f;
         double d=5.6D;
        System.out.println(Double.isNaN(d)); //false
        System.out.println(Double.isNaN(c)); //false
        System.out.println(Double.isNaN(a)); //fasle
    }

(3)  常量 

 1 public class Main {
 2 
 3     public static void main(String[] args) {
 4         System.out.println(1.1/0);//结果是:Infinity    就是常量Double.POSITIVE_INFINITY
 5         System.out.println(-1.1/0);//结果是:-Infinity     就是常量Double.NEGATIVE_INFINITY
 6         System.out.println(0/3.2);//结果是:0.0
 7         System.out.println(Math.pow(-0.2,0.5));//结果是:NaN    就是常量Double.NaN
 8         System.out.println(1/0);//结果是:Exception in thread "main" java.lang.ArithmeticException: / by zero
 9     }
10 }

 

2、Math

(1) Math使用时候,可以不带“Math” 前缀,添加导入静态包

import static java.lang.Math.*; 
public class Main {
    public static void main(String[] args) {  
        System.out.println(pow(-0.2,0.5));
    }
}

(2) Math的计算结果并不完全可预测,首先是保证了速度。如果需要一个所有平台相同的结果的数据,推荐使用 StrictMath

 

3、数值类型转化

 

 

 4、枚举型

public class Main {
    public static void main(String[] args) {
        enum Size  {XL,L,M,M,S};
        Size s=Size.M;
    }
}

 

 5、字符串

(1) 字符串之间的比较不能用==,但是字符串常量可以,应为虚拟机将相同的字符串常量共享

(2) 空串是一个Java对象,有长度和内容。String也可以被赋值null,表示没有任何对象与该String变量关联

(3) 字符串打印,可以用C语言的printf()

System.out.printf("你好啊!%s,我今年%d岁","小猫",10);//你好啊!小猫,我今年10岁

 

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/pikaqiucode/p/10697298.html