首页 > 笔记 > 正文

Long类型比较大小,long型和Long型区别

2022-03-28 笔记 1759 ℃ 0 评论
public class test {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		/**
		* long 是基本类型
		* Long是对象类型,进行比较时:若验证相等则取地址,数值为(-128~127)则相等,
		* 因为这段数值取的是相同的地址,其余的则不相等,验证相等可用longValue(),可用equals();
		*/
		long a1 = 127;
		long b1 = 127;
		long a2 = 123456;
		long b2 = 123456;
		Long c1 = 127L;
		Long d1 = 127L;
		Long c2 = 123456L;
		Long d2 = 123456L;
		
		
		System.out.println(a1==b1);//true
		System.out.println(a2==b2);//true
		System.out.println(c1==d1);//数值为(-128~127)则相等true,
		System.out.println(c2==d2);//false 数值不在(-128~127)
		System.out.println(c2.equals(d2));//true数值不在(-128~127) 可用equals();
		System.out.println(a1==c1);//true
		System.out.println(a2==c2);//true
	}
}

转自:https://www.cnblogs.com/linjiaxin/p/7571759.html


用.longValue()解决

猜你喜欢

日历
«    2024年3月    »
123
45678910
11121314151617
18192021222324
25262728293031
标签列表
最近发表
友情链接