• java之基础数据类型学习————(一)


    JAVA数据类型:                   

    总结来说,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 }
    View Code

    标注

    ①:看一下Byte.TYPE属性的toString

    1 System.out.println(Byte.TYPE);//byte
    2 System.out.println(byte.class);//byte
    3 System.out.println(Byte.class);//class java.lang.Byte

      可以看出,Byte.TYPE属性表示基本类型 byte 的Class实例,这里注意是基本类型。

    ②:
      在静态内部私有类中定义了一个缓存数组,这个缓存数组在什么时候初始化?是在ByteCache类加载的时候。
      ByteCahe类什么时候加载呢?肯定是在调用ValueOf()方法中调用的时候。
      如果Byte加载的时候就对这个数组进行初始化,那么有可能会造成资源的浪费,因为有可能程序根本用不到
      除了提高效率外,这种方式还可以实现数据的共享。


     short类

    short类与byte类的很多方法是相同的,这里只拿出来一些不同的方法来看一下:

     1 package java.lang;
     2 
     3 public final class Short extends Number implements Comparable<Short> {
     4 
     5     /**
     6      * short型数据最大值32767 即:0111 1111 1111 1111
     7      * 最小值-32768 即1000 0000 0000 0000
     8      * short型占两个字节,即16bit
     9      */
    10     public static final short   MIN_VALUE = -32768;
    11     public static final short   MAX_VALUE = 32767;
    12     public static final int SIZE = 16;
    13 
    14     /**
    15      * ①
    16      * 与byte类一样,short也有一个自己的缓存私有内部类
    17      */
    18     private static class ShortCache {
    19         private ShortCache(){}
    20 
    21         static final Short cache[] = new Short[-(-128) + 127 + 1];
    22 
    23         static {
    24             for(int i = 0; i < cache.length; i++)
    25                 cache[i] = new Short((short)(i - 128));
    26         }
    27     }
    28 
    29     public static Short valueOf(short s) {
    30         final int offset = 128;
    31         int sAsInt = s;
    32         if (sAsInt >= -128 && sAsInt <= 127) { // must cache
    33             return ShortCache.cache[sAsInt + offset];
    34         }
    35         return new Short(s);
    36     }
    37 
    38     /**
    39      * short的equals方法比较的是类型必须是Short类型,值必须相等
    40      */
    41     public boolean equals(Object obj) {
    42         if (obj instanceof Short) {
    43             return value == ((Short)obj).shortValue();
    44         }
    45         return false;
    46     }
    47 
    48     /**
    49      * 将二进制码顺序颠倒,我的理解是将大端序转换为小端序
    50      */
    51     public static short reverseBytes(short i) {
    52         return (short) (((i & 0xFF00) >> 8) | (i << 8));
    53     }
    54 }
    View Code

    ①:byte类的大小允许其有256个值,所以ByteCache的缓存数组的长度是256,short类比byte的值多,为什么还是256个缓存值呢?

    因为short类型的值有65534个,如果要建立这样一个缓存数组,首先非常没有效率(在初始化的以后),次之,非常的浪费空间。

    而位于-128到127之间的这些值是使用频率非常高的,所以为了不过度浪费空间,并且通过共享数据而加快效率,包括Byte、Short、Integer、Long都是使用这样的方式,并且数组的值都是-128到127,而Character类也使用了这种共享方式,不同的是它的值是0~127


    总结:

    1、类型转换:注意byte、short(还包括char)之间的转换,都是先转换成int型,再转换成目标类型

    2、数据共享:通过自己的私有内部类来缓存使用频率高的数据,实现数据共享(对于在范围内的数据,所有值相通的Byte类,其引用指向的类是同一个)

    3、TYPE属性:表示基本类型(比如byte)的Class实例,这里注意是基本类型

    在转换类型时,首先是转换成int型,再转换成byte、shortr型,所以,它们的很多方法是调用的Integer类的方法,下一次着重看一下这个类,以及分析一下Comparable接口。

  • 相关阅读:
    Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable
    调试bug 技巧
    调试bug 技巧
    调试bug 技巧
    调试技巧之 找准调试点
    调试技巧之 找准调试点
    调试技巧之 找准调试点
    adnroid 打包问题 :compileReleaseJavaWithJavac
    线程等待
    LinkedList源码解析(jdk1.8)
  • 原文地址:https://www.cnblogs.com/gdayq/p/5818902.html
Copyright © 2020-2023  润新知