第五天04 JAVA基础语法(认识对象--对象指定与相等性)(学习笔记)

对象指定与相等性
主要介绍“=”和“equery”的区别性
当“=”用于基本类型时,是将值复制给变量,当“==”用于基本类型时,是比较两个变量储存的值是否相同,如下两个变量的值都是10,,所以会显示true。
例:
import java.math.BigDecimal;
public class TestThree {
public static void main(String[] args) {
//"=="
int a=10;
int b=10;
int c=a;
System.out.println(a==b);
System.out.println(a==c);
//"=="和"equals"
BigDecimal d=new BigDecimal("0.1");
BigDecimal e=new BigDecimal("0.1");
System.out.println(d==e); //显示flase 判断两个值是否绑定同一个对象
System.out.println(d.equals(e)); //显示true 比较两个值是否相等
}
}
文件目录





基本类型打包器
基本类型long、int、double、float、boolean等,如果要让基本类型像对象一样操作,可以使用Long、Integer、Double、Float、Boolean、Byte等类来打包(Wrap)基本类型。
例:
public class four {
/**
* 如果表达式中都是int,就只会在int空间中做运算,结果会是int整数,因此da1 / 3就会显示3
* 操作Integer的doubleValue()将打包值以double类型返回,这样就会在double类型空间中做相除,
* 结果就会显示3.3333333333333335
* 操作Integer的compareTo()方法,可以与另一个 Integer对象进行比较,如果打包值相同就返回0,小于compareTo()传入
* 的对象打包值就返回-1,否则就是1
* @param args
*/
public static void main(String[] args) {
int da1=10;
int da2=20;
Integer wr1=new Integer(da1); //打包基本类型
Integer wr2=new Integer(da2); //打包基本类型
System.out.println(da1 / 3); //基本类型运算
System.out.println(wr1.doubleValue() / 3); //操作打包器方法
System.out.println(wr1.compareTo(wr2));
}
}
文件目录

猜你喜欢

转载自blog.csdn.net/weixin_39559301/article/details/80635580
今日推荐