您的位置:首页 > 编程语言 > Java开发

java中基本数据类型与对象引用类型的默认初始化值学习

2014-05-19 23:37 651 查看
import org.junit.Test;

public class TestInit {
boolean boo;
char cha;
byte by;
short shor;
int in;
float flo;
long lon;
double dou;
String str;

@Test
public void aa() {
System.err.println("boolean:" + boo);
if (cha == '\u0000') {
System.err.println("char:" + (int) cha);
System.err.println(cha);
}
System.err.println("byte:" + by);
System.err.println("short:" + shor);
System.err.println("int:" + in);
System.err.println("long:" + lon);
System.err.println("float:" + flo);
System.err.println("double:" + dou);
System.err.println("String:" + str);
}
}


以上为测试程序,输出结果为:

boolean:false
char:0
byte:0
short:0
int:0
long:0
float:0.0
double:0.0
String:null


结论:

布尔值: boolean默认false

字符: char,

整数:byte,short,int,long为0

实数: float,double为double类型的0.0

对象引用为null

补充:

(1)、new方式创建对象保存到堆中,保存其地址的引用,对于基本数据类型来说,java采取和c、c++相同方式,创建的变量直接存储值,置于栈顶,更加高效。

(2)、Java中每种基本数据类型所占存储空间大小,不依赖于机器硬件架构变化而变化,即基本数据类型所占存储空间具有不变性。

(3)、所有数值类型都有正负号,没有无符号的数值类型

各个基本数据类型的大小及精度如下表:

基本类型大小最小值最大值包装类型
boolean------Boolean
char16bit,2个字节(byte)Unicode 0Unicode 2^16-1即,65535Character
byte8bit,1个字节-128127Byte
short16bit,2个字节-2^15即32768+2^15-1,即32767Short
int32bit,4个字节-2^31即2147483648,约-21亿+2^31-1,即2147483647,约约+21亿Integer
long64bit,8个字节-2^63即9223372036854775808,约-922亿亿+2^63即9223372036854775807,约+922亿亿Long
float32bit,4个字节1.4E-45,即1.4*(1/10^45)3.4028235E38,即3.4028235*10^38Float
double64bit,8个字节4.9E-324,即4.9*(1/10*324)1.7976931348623157E308即1.7976931348623157*10^308Double
表说明:1)、boolean类型所占存储空间大小没有明确指定,仅仅定义能够取字面值true,false

2)、各个数值类型的最大值,最小值,均可通过包装类型的MIN_VALUE与MAX_VALUE获取,如Double.MIN_VALUE。

3)、jdk1.5后提供自动装拆箱机制,基本数据类型和包装类型自动转换。

4)、表格中按照从上到下精度由低到高(boolean除外),从高精度到低精度需显示强制转换(boolean除外)。

附加:高精度数字

BigInteger:支持任意精度的额整数,可以表示任何大小整数值,而不会丢失信息。

BigDecimal:支持任何精度的定点数,如可以进行精确的货币运算。

高精度数字缺点:运算速度比较慢,以速度换取精度。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐