Java基本数据类型
数据类型 | 最小值 | 最大值 | 占用字节 | 占用bits | 默认值 | 可否作为Switch条件 |
byte | -128 | 127 | 1 | 8 | 0 | 可以 |
boolean | 1 | 8 | false | 不可以 | ||
short | -32768 | 32767 | 2 | 16 | 0 | 可以 |
char | ? | 2 | 16 | 可以 | ||
int | -2147483648 | 2147483647 | 4 | 32 | 0 | 可以 |
float | 1.4E-45 | 3.4028235E+38 | 4 | 32 | 0.0 | 不可以 |
double | 4.9E-324 | 1.7976931348623157E308 | 8 | 64 | 0.0 | 不可以 |
long | -9223372036854770000 | 9223372036854770000 | 8 | 64 | 0 | 不可以 |
①1字节=8bits ②String类型也可以用作Switch条件 ③String的默认值为null
测试代码:
package com.click369; public class NumberTest { static byte b; static short s; static int i; static double d; static float f; static char c; static long l; static boolean bool; static String str; public static void main(String[] args) { // Java中除了boolean类型之外的7种基本数据类型: System.out.println("Java 8种基本数据类型所占位数、最小值、最大值:"); System.out.println("byte size: " + Byte.SIZE); System.out.println("byte min: " + Byte.MIN_VALUE); System.out.println("byte max: " + Byte.MAX_VALUE); System.out.println("char size: " + Character.SIZE); System.out.println("char min: " + Character.MIN_VALUE); System.out.println("char max: " + Character.MAX_VALUE); System.out.println("short size: " + Short.SIZE); System.out.println("short min: " + Short.MIN_VALUE); System.out.println("short max: " + Short.MAX_VALUE); System.out.println("int size: " + Integer.SIZE); System.out.println("int min: " + Integer.MIN_VALUE); System.out.println("int max: " + Integer.MAX_VALUE); System.out.println("float size: " + Float.SIZE); System.out.println("float min: " + Float.MIN_VALUE); System.out.println("float max: " + Float.MAX_VALUE); System.out.println("double size: " + Double.SIZE); System.out.println("double min: " + Double.MIN_VALUE); System.out.println("double max: " + Double.MAX_VALUE); System.out.println("long size: " + Long.SIZE); System.out.println("long min: " + Long.MIN_VALUE); System.out.println("long max: " + Long.MAX_VALUE); // 输出默认值 System.out.println("str = " + str); System.out.println("b = " + b); System.out.println("s = " + s); System.out.println("i = " + i); System.out.println("d = " + d); System.out.println("f = " + f); System.out.println("c = " + c); System.out.println("l = " + l); System.out.println("bool = " + bool); } }