JAVA的那些坑

0. replaceAll方法

 public static void main (String[] args) { 
    String classFile = "com.jd.". replaceAll(".", "/") + "MyClass.class";
    System.out.println(classFile);
}

原以为是把”.”替换为”/”,即输出结果是 “com/jd/MyClass.class”
其实不然,replaceAll方法的第一个参数是一个正则表达式,而”.”在正则表达式中表示任何字符,所以会把前面字符串的所有字符都替换成”/”,即输出为 “///////MyClass.class”。如果想替换的只是”.”,那么久要写成”\.”

1. 类型比较
(1)基本型和基本型封装型进行“==”运算符的比较,基本型封装型将会自动拆箱变为基本型后再进行比较,因此Integer(0)会自动拆箱为int类型再进行比较,显然返回true;

     int a = 220;

     Integer b = 220;

    System.out.println(a==b);//true

(2)两个Integer类型进行“==”比较, 如果其值在-128至127 ,那么返回true,否则返回false, 这跟Integer.valueOf()的缓冲对象有关,这里不进行赘述。

    Integer c=3;
    Integer h=3;
    Integer e=321;
    Integer f=321;
    System.out.println(c==h);//true
    System.out.println(e==f);//false

(3)两个基本型的封装型进行equals()比较,首先equals()会比较类型,如果类型相同,则继续比较值,如果值也相同,返回true。

    Integer a=1;
    Integer b=2;
    Integer c=3;
    System.out.println(c.equals(a+b));//true

(4)基本型封装类型调用equals(),但是参数是基本类型,这时候,先会进行自动装箱,基本型转换为其封装类型,再进行3中的比较。

    int i=1;
    int j = 2;
    Integer c=3;
    System.out.println(c.equals(i+j));//true

2.各类型默认值
这里写图片描述

猜你喜欢

转载自blog.csdn.net/ccc_12345/article/details/81318070
今日推荐