DESKTOP-JM545O1_20200903-李东鹏

1. 问题

class CompareReference{
   public static void main(String [] args){
   float f=42.0f;
   float f1[]=new float[2];
   float f2[]=new float[2];
   float[] f3=f1;
   long x=42;
   f1[0]=42.0f;
   System.out.println(x==f1[0]);//true
   System.out.println(f1==f3);//true
  }
} 

基本数据类型之间的比较,会将低精度类型自动转换为高精度类型再比较

byte b1=1,b2=2,b3,b6;  
final byte b4=4,b5=6;  
b6=b4+b5;  
b3=(b1+b2);//编译出错  
System.out.println(b3+b6);

被final修饰的变量是常量,这里的b6=b4+b5可以看成是b6=10;在编译时就已经变为b6=10了 而b1和b2是byte类型,java中进行计算时候将他们提升为int类型,再进行计算,b1+b2计算后已经是int类型,赋值给b3,b3是byte类型,类型不匹配,编译不会通过,需要进行强制转换。

Java中的byte,short,char进行计算时都会提升为int类型。

public class Example{
    String str=new String("hello");
    char[]ch={'a','b'};
  
    public void change(String str,char ch[]){
        str="test ok";
        ch[0]='c';
    }
      public static void main(String args[]){
        Example ex=new Example();
        ex.change(ex.str,ex.ch);
        System.out.print(ex.str+" and ");
        System.out.print(ex.ch);
    }
}

基本数据类型和String类型是值传递,对象和数组等都是引用传递。值传递不改变原来的值,而引用传递直接改变。因此,本题中String类型str没有发生改变,而数组ch的第一位发生改变。结果为hello and cb

2. 吐槽

一周又结束了,时间过得好快呀 !!!