JavaSE 复习:基本数据类型

1.Java中基本数据类型有那些

数值型: 

整数类型: byte(8位) short(16) int(默认,32位) long(64位)

小数型: float(32位) , double(64位)

字符型:

char

布尔型:

 有两个值(true, false)

代码演示:

/**
 * BasicType.class
 * 
 * 演示基本数据类型的定义和初始化
 * 
 * @see System.out.println
 * 
 * @see Java.lang.Byte
 * @see Java.lang.Short
 * @see Java.lang.Integer
 * @see Java.lang.Long
 * @see Java.lang.Char
 * @see Java.lang.Character
 * @see Java.lang.Float
 * @see Java.lang.Double
 * @see Java.lang.Boolean
 */
public class BasicType {
	/** 定义byte类型的变量,并初始化  */
	public byte bByte = Byte.MAX_VALUE;
	/** 定义short类型的变量,并初始化 */
	public short sShort = Short.MAX_VALUE;
	/** 定义int类型的变量,并初始化 */
	public int iInt = Integer.MAX_VALUE;
	/** 定义long类型的变量,并初始化 */
	public long lLong = Long.MAX_VALUE;
	/** 定义char类型的变量,并初始化 */
	public char cChar = 'a';
	/** 定义单精度小数型,并初始化 */
	public float fFloat = Float.MAX_VALUE;
	/** 定义双精度小数型,并初始化 */
	public double dDouble = Double.MAX_VALUE;
	/** 定义布尔型变量, 默认为true */
	public boolean bBoolean = true;
	
	/**
	 * 主函数
	 * 
	 * 分别将基本类型的值进行输出
	 * @see System.out.println
	 */
	public static void main(String[] args) {
		BasicType basicType = new BasicType();
		System.out.println("Byte=" + basicType.bByte);
		System.out.println("Short=" + basicType.sShort);
		System.out.println("Integer=" + basicType.iInt);
		System.out.println("Long=" + basicType.lLong);
		System.out.println("Character=" + basicType.cChar + " 它对应的Unicode编码是:" + (int)basicType.cChar);
		System.out.println("Float=" + basicType.fFloat);
		System.out.println("double=" + basicType.dDouble);
		System.out.println("Boolean=" + basicType.bBoolean);
	}
}

Integer 操作

/**
 * 本类演示对Integer对象的一些常用操作
 * @see Java.lang.Integer
 */
public class IntegerOperation {

	public static void main(String[] args) {
		Integer a = new Integer(12);
		System.out.println("Int占用" + Integer.SIZE + "个字节!");
		System.out.println("Int类型变量的表示范围是:" + Integer.MIN_VALUE + " ~ " + Integer.MAX_VALUE);
		// 将字符串转换成整数,会抛出NumberFormatException异常
		try {
			a = Integer.decode("35");
			System.out.println("Integer.decode(\"35\")=" + a);
			// 将字符串转换成一个int类型的变量
			// int a = Integer.parseInt("21");
			System.out.println("Integer.parseInt(\"11\")=" + Integer.parseInt("11"));
			// Integer.valueOf("21a");就会出现异常其他一样
			a = Integer.valueOf("21");
			System.out.println("Integer.valueOf(\"sss\")=" + a);
		} catch (NumberFormatException e) {
			System.out.println("该字符串不能够转换成int型数据");
			return;
		}
		// 将整数型转换成一个字符串
		System.out.println("a.toString = " + a.toString());
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值