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

JavaSE 复习:基本数据类型

2014-03-24 23:01 344 查看
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());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java