总结来说,java的基本数据类型分为 四类八种
• 第一类:整数类型:byte、short、int、long
• 第二类:浮点型:float、double
• 第三类:字符类型:char
• 第四类:布尔类型:boolean
而基本数据类型与引用数据类型的区别是:
基本数据类型可以直接在栈中分配内存,而引用数据类型是在堆中分配内存的,栈中存储的只是一个数据的引用(指针)。
八种基本数据类型的字节大小:
| boolean(布尔类型) | 1/8字节 |
| byte (字节类型) | 1字节 |
| short (短整型) | 2字节 |
| int (整型) | 4字节 |
| long (长整型) | 8字节 |
| float (单精度浮点类型) | 4字节 |
| double (双精度浮点类型) | 8字节 |
| char (字符类型) | 2字节 |
注意:java中所有的数据类型都有固定的存储范围和所占内存空间大小,而不受操作系统的影响,从而保证java程序的可移植性。
整形默认是 int型,浮点型默认是 double型。
表示 long型 要在数据后面加上 l or L,表示 float型要在数据后面加上 f or F。
基本数据类型的转换:
• 自动转换
byte -> short/char -> int -> long
float -> double
int -> float
long -> double
• 强制转换
由大转小,使用强制转换,但是注意精度的丢失!
另外,对于byte、short与char之间的转换,是先转化成int类型,然后再转换成目标类型。比如,byte转short:byte -> int ->short
Number类
jdk对Number类的介绍:
The abstract class Number is the superclass of classes BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short. Subclasses of Number must provide methods to convert the represented numeric value to byte, double, float, int, long, and short.
BigDecimal,BigInteger,Byte,Double,Float,Integer,Long与Short类都继承了Number类,这几个类都实现了Number的抽象方法。
看一下number的抽象方法:
1 public abstract int intValue(); 2 public abstract long longValue(); 3 public abstract float floatValue(); 4 public abstract double doubleValue(); 5 public byte byteValue() { 6 return (byte)intValue(); 7 } 8 public short shortValue() { 9 return (short)intValue(); 10 }
这几个抽象类主要实现了各个类型之间的转换,可以看到,在提供某类型转换成 byte 类型或者 short 类型时,经历了两个阶段,首先调用 intValue() 方法先转换成 int型,然后对得到的int型数据做一个强制转换,这就是转换成 byte、short的过程。
Byte类
1 package java.lang; 2 3 public final class Test extends Number implements Comparable<Byte> { 4 5 /** 6 * Byte类型能表示的最小的值,即:0x1000 0000 7 * Byte类型能表示的最大值,即0x0111 1111 8 *(最高位为符号位,java使用补码表示二进制数,正数补码为其本身,负数为反码+1) 9 */ 10 public static final byte MIN_VALUE = -128; 11 public static final byte MAX_VALUE = 127; 12 13 /** 14 * ① 15 * 这里表示int类的实例 16 */ 17 public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte"); 18 19 /** 20 * Byte的静态方法,作用是将byte类型转换为String类型 21 * 实现过程是调用Integer类的头String方法,参数10表示是十进制 22 */ 23 public static String toString(byte b) { 24 return Integer.toString((int)b, 10); 25 } 26 27 /** 28 * Byte的静态内部类,作用是创建一个Byte缓存数组 29 * 将值为-128到127的256个Byte实例放到该缓存数组中 30 */ 31 private static class ByteCache { 32 private ByteCache(){} 33 34 static final Byte cache[] = new Byte[-(-128) + 127 + 1]; 35 36 static { 37 for(int i = 0; i < cache.length; i++) 38 cache[i] = new Byte((byte)(i - 128)); 39 } 40 } 41 42 /** 43 * ② 44 * Byte的静态方法,作用是将基本数据类型byte型转为Byte类的一个对象 45 * 过程是去缓存类的缓存数组中去取对应的值 46 */ 47 public static Byte valueOf(byte b) { 48 final int offset = 128; 49 return ByteCache.cache[(int)b + offset]; 50 } 51 52 /** 53 * Byte的静态方法,作用是将String按照给定的基数转化为相应的byte数据 54 * 过程是调用Integer的parseInt方法实现将String转换为int型, 55 * 然后进行一次强制转换。 56 */ 57 public static byte parseByte(String s, int radix) 58 throws NumberFormatException { 59 int i = Integer.parseInt(s, radix); 60 if (i < MIN_VALUE || i > MAX_VALUE) 61 throw new NumberFormatException( 62 "Value out of range. Value:\"" + s + "\" Radix:" + radix); 63 return (byte)i; 64 } 65 66 public static byte parseByte(String s) throws NumberFormatException { 67 return parseByte(s, 10); 68 } 69 70 public static Byte valueOf(String s, int radix) 71 throws NumberFormatException { 72 return valueOf(parseByte(s, radix)); 73 } 74 75 public static Byte valueOf(String s) throws NumberFormatException { 76 return valueOf(s, 10); 77 } 78 79 /** 80 * Byte的静态方法,作用是对String数据进行解码 81 * 比如:-1 0x77 等不同进制的数据 82 */ 83 public static Byte decode(String nm) throws NumberFormatException { 84 int i = Integer.decode(nm); 85 if (i < MIN_VALUE || i > MAX_VALUE) 86 throw new NumberFormatException( 87 "Value " + i + " out of range from input " + nm); 88 return valueOf((byte)i); 89 } 90 91 /** 92 * 这个就是Byte类的值,这个属性由final修饰,表示value属性是不可变的 93 */ 94 private final byte value; 95 96 public Byte(byte value) { 97 this.value = value; 98 } 99 100 public Byte(String s) throws NumberFormatException { 101 this.value = parseByte(s, 10); 102 } 103 104 public byte byteValue() { 105 return value; 106 } 107 108 public short shortValue() { 109 return (short)value; 110 } 111 112 public int intValue() { 113 return (int)value; 114 } 115 116 public long longValue() { 117 return (long)value; 118 } 119 120 public float floatValue() { 121 return (float)value; 122 } 123 124 public double doubleValue() { 125 return (double)value; 126 } 127 128 public String toString() { 129 return Integer.toString((int)value); 130 } 131 132 public int hashCode() { 133 return (int)value; 134 } 135 136 public boolean equals(Object obj) { 137 if (obj instanceof Byte) { 138 return value == ((Byte)obj).byteValue(); 139 } 140 return false; 141 } 142 143 /** 144 * 实现了Comparable接口,这个另外开一个篇幅来说 145 */ 146 public int compareTo(Byte anotherByte) { 147 return compare(this.value, anotherByte.value); 148 } 149 150 public static int compare(byte x, byte y) { 151 return x - y; 152 } 153 154 //表示一个byte由8位表示,也就是一个字节 155 public static final int SIZE = 8; 156 }