java夯实基础-基本数据类型

学习任何一门语言,基础永远都是最重要的。本文测试java中八种基本数据类型(int,short,long,float,double,char,boolean,byte),包括其占用空间大小,能够存储的最大值和最小值、java中自动装箱和拆箱、注意事项。

测试代码如下:

package com.wllfengshu.test;

import org.junit.Test;

/**
 *1、测试基本数据类型
 *2、测试基本数据类型占用空间大小
 *3、测试基本数据类型存储的最大值和最小值 
 */
public class Variate {
	//int
	@Test
	public void testInt(){
		System.out.println("占用空间"+Integer.SIZE/8+"Byte");
		System.out.println("最大值"+Integer.MAX_VALUE);
		System.out.println("最大值"+Integer.MIN_VALUE);
		//总结:int占用4字节,最大值约21忆,最小值约-21忆
		int a=1;
		Integer b=2;
		Integer c=3;
		b=a;//自动装箱
		System.out.println(b);
		a=c;//自动拆箱
		System.out.println(a);
		//总结:自动拆箱和自动装箱
	}
	
	//long
	@Test
	public void testLong(){
		System.out.println("占用空间"+Long.SIZE/8+"Byte");
		System.out.println("最大值"+Long.MAX_VALUE);
		System.out.println("最大值"+Long.MIN_VALUE);
		//总结:long占用8字节
		long a=1;
		Long b=2L;//整数默认为int类型,int类型数据不能自动装箱为Long
		Long c=3L;//在整形数据后加L,表示该数为long类型
//		Long d=4;//错误
		b=a;//自动装箱
		System.out.println(b);
		a=c;//自动拆箱
		System.out.println(a);
		//总结:整形数据默认为int,自动拆箱和自动装箱
	}
	
	//short
	@Test
	public void testShort(){
		System.out.println("占用空间"+Short.SIZE/8+"Byte");
		System.out.println("最大值"+Short.MAX_VALUE);
		System.out.println("最大值"+Short.MIN_VALUE);
		//总结:short占用2字节,最大值约3万,最小值约-3万
		short a=1;
		Short b=2;//整数默认为int类型,把int数据赋值给short类型可能会出现数据丢失
		Short c=3;
		b=a;//自动装箱
		System.out.println(b);
		a=c;//自动拆箱
		System.out.println(a);
		
//		int d=4;
//		short e=d;//该代码必须通过强转
		//对以上需要强转代码的总结:编译器问题,具体解释请看:http://ask.csdn.net/questions/657817#answer_471158
	}
	
	
	//char
	@Test
	public void testChar(){
		System.out.println("占用空间"+Character.SIZE/8+"Byte");
		System.out.println("最大值"+Character.MAX_VALUE);
		System.out.println("最大值"+Character.MIN_VALUE);
		//总结:char占用4字节,最大值\uffff,最小值\u0000    \ u 表示unicode编码
		char a='1';
		Character b='2';
		Character c='3';
		b=a;//自动装箱
		System.out.println(b);
		a=c;//自动拆箱
		System.out.println(a);
		//总结:自动拆箱和自动装箱
		
		String uniCodeTemp = "\\u"+(int)'你';//获取字符的unicode编码值
		System.out.println(uniCodeTemp);
	}
	
	//float
	@Test
	public void testFloat(){
		System.out.println("占用空间"+Float.SIZE/8+"Byte");
		System.out.println("最大值"+(long)Float.MAX_VALUE);
		System.out.println("最大值"+Float.MIN_VALUE);
		//总结:float占用4字节
		float a=1;
		Float b=(float) 2;//int can't case to Float
		Float c=3f;
		b=a;//自动装箱
		System.out.println(b);
		a=c;//自动拆箱
		System.out.println(a);
		//总结:自动拆箱和自动装箱
		
		float d=(float) 1.0;//this is double,because 1.0 default is double 
		Float e=(float) 2.0;
	}
	
	//double
	@Test
	public void testDouble(){
		System.out.println("占用空间"+Double.SIZE/8+"Byte");
		System.out.println("最大值"+Double.MAX_VALUE);
		System.out.println("最大值"+Double.MIN_VALUE);
		//总结:double占用8字节
		double a=1;//
//		Double b=2;//报错
		Double c=(double) 3;
		c=a;//自动装箱
		//总结:自动拆箱和自动装箱
	}
	
	//boolean
	@Test
	public void testBoolean(){
		/**
		 * 根据http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html官方文档的描述:
		boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.
		布尔类型:布尔数据类型只有两个可能的值:真和假。使用此数据类型为跟踪真/假条件的简单标记。这种数据类型就表示这一点信息,但是它的“大小”并不是精确定义的。
		所以,boolean类型没有给出精确的定义,《Java虚拟机规范》给出了4个字节,和boolean数组1个字节的定义,具体还要看虚拟机实现是否按照规范来,所以1个字节、4个字节都是有可能的。
		 */
		boolean a=true;
		Boolean b=true;
		b=a;//自动装箱
	}
	
	//byte
	@Test
	public void testByte(){
		System.out.println("占用空间"+Byte.SIZE/8+"Byte");
		System.out.println("最大值"+Byte.MAX_VALUE);
		System.out.println("最大值"+Byte.MIN_VALUE);
		//总结:Byte占用1字节
		byte a=1;//数值为超过127不报错,编译器会做检查
		Byte b=2;
		b=1;
		int c=a;
//		a=c;//编译器并不知道整形变量c是否超过byte最大值,它只有在运行期间才能检测,所以报错
	}
}

注意:上表中大小单位为:位

【最后】有不足或者错误之处,欢迎大家指正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值