java笔记

本文详细介绍了Java中float与double的区别,包括单精度与双精度浮点数的使用及转换;同时深入探讨了int与Integer的不同,包括基本类型与包装类之间的自动装箱、拆箱过程以及==运算符在不同情况下的比较行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、float单精度跟double双精度的区别

数后加上字母f,如 2.3f、1.0f 等此类是单精度浮点数(float)
数直接写出的数
,如 2.3、1.0 等此类是 double 型的。

float f=3.4

3.4是双精度数,将双精度型(double)赋值给浮点型(float)属于下转型(down-casting,也称为窄化)

会造成精度损失因此需要强制类型转换float f =(float)3.4; 或者写成float f =3.4F;。



 二、int跟Integer的区别

public class Test {
	
	public static void main(String[] args) {
		Integer a = new Integer(3);
		Integer b = 3; 						// 将3自动装箱成Integer类型
		int c = 3;
		System.out.println(a == b);			 // false 两个引用没有引用同一对象
		System.out.println(a == c);			//ture
		Integer x = 100;
		Integer y = 100;
		System.out.println(x ==y);			//true
		
		Integer m = new Integer(100); 
		Integer n = new Integer(100); 
		System.out.println(m == n);			//false
						// ?   Integer a = new Integer(3);   跟   Integer a = 3  的区别?
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值