本章节的整体结构说明
基本类型
基本类型变量,不需要用new来创建,而是直接赋值即可。而且变量直接存储“值”,并置于堆栈之中。
Java程序实例:
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("char:\t" + Character.SIZE/8 + " byte");
System.out.println("byte:\t" + Byte.SIZE/8 + " byte");
System.out.println("short:\t" + Short.SIZE/8 + " byte");
System.out.println("int:\t" + Integer.SIZE/8 + " byte");
System.out.println("long:\t" + Long.SIZE/8 + " byte");
System.out.println("float:\t" + Float.SIZE/8 + " byte");
System.out.println("double:\t" + Double.SIZE/8 + " byte");
}
运行结果如下:
char: 2 byte
byte: 1 byte
short: 2 byte
int: 4 byte
long: 8 byte
float: 4 byte
double: 8 byte
表格说明如下:
基本类型 | 大小 | 最小值 | 最大值 | 包装器类型 |
---|---|---|---|---|
void | - | - | - | Void |
boolean | - | - | - | Boolean |
char | 16-bit(2-byte) | 0 | 2^16-1 | Character |
byte | 8-bit(1-byte) | -128 | 127 | Byte |
short | 16-bit(2-byte) | -2^15 | 2^15-1 | Short |
int | 32-bit(4-byte) | -2^31 | 2^31-1 | Integer |
long | 64-bit(8-byte) | -2^63 | 2^63-1 | Long |
float | 32-bit(4-byte) | IEEE754 | IEEE754 | Float |
double | 64-bit(8-byte) | IEEE754 | IEEE754 | Double |
基本类型的默认值
Java程序实例:
public class default {
static boolean bool;
static char c;
static byte b;
static short s;
static int i;
static long l;
static float f;
static double d;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("boolean: " + bool);
System.out.println("char:\t" + c);
System.out.println("byte:\t" + b);
System.out.println("short:\t" + s);
System.out.println("int:\t" + i);
System.out.println("long:\t" + l);
System.out.println("float:\t" + f);
System.out.println("double:\t" + d);
}
}
运行结果如下:
boolean: false
char:
byte: 0
short: 0
int: 0
long: 0
float: 0.0
double: 0.0
表格说明如下:
基本类型 | 默认值 |
---|---|
boolean | false |
char | null |
byte | (byte)0 |
short | (short)0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
嵌入式文档说明
标签 | 说明 |
---|---|
@see | 引用其他类 |
@version | 版本信息 |
@author | 作者信息 |
@since | 允许的最早版本 |
@param | 标识方法参数 |
@return | 描述方法返回值 |
@throws | 异常对象说明 |
@deprecated | 建议不要使用这些旧特性 |