天天看點

Java的基本資料類型

1. Java 的基本資料類型都有哪些各占幾個位元組

Java的基本資料類型

2. short s1 = 1;s1 =s1 + 1;有錯嗎?short s1 = 1;s1 += 1有錯嗎;

前者不正确,後者正确。對于shorts1=1;s1=s1 + 1;由于1是int類型,是以s1+1運算結果也是int型,

需要強制轉換類型才能指派給short 型。而short s1 = 1;s1 += 1可以正确編譯,因為s1+= 1;相當于s1 =

(short)(s1 + 1):其中有隐含的強制類型轉換。

3. int和和Integer有什麼差別?

Java是一個近乎純潔的面向對象程式設計語言,但是為了程式設計的友善還是引入了基本資料類型,為了能夠将這些基本資料類型當成對象操作, Java為每一個基本資料類型都引入了對應的包裝類型 (wrapperclass) ,int 的包裝類就是Integer,從Java 5開始引入了自動裝箱/拆箱機制,使得者可以互相轉換。

Java為每個原始類型提供了包裝類型:

原始類型: boolean, char, byte, short, int, long, float, double

包裝類型:Boolean, Character, Byte, Short, Integer, Long, Float, Double

class AutoUnbaxingTest {
    public static void main(String[] args) {
  Integer a = new Integer(3);
  Integer b = 3;//将3自動裝箱成Integer類型
  int c=3;
  System.out.println(a == b);// false 兩個引用沒有引用同一對象
  System.out.printn(a == c);//true a自動拆箱成int類型再和c比較
    }
}      

4.下面 Integer類型的數值比較輸出的結果為?

public class Test03 {
    public static void main(String[] args) {
  Integerf1 = 100, f2= 100, f3=150, f4= 150;
  System.out. println(f1 == f2); 
  System.out. println(f3 == f4); 
    }
}      

如果不明就裡很容易認為兩個輸出要麼都是true 要麼都是false。 首先需要注意的是f1、f2、 f3、f4四個變量

都是Integer對象引用,是以下面的= =運算比較的不是值而是引用。裝箱的本質是什麼呢?當我們給一個Integer對象賦一個int值的時候,會調用Integer 類的靜态方法valueOf,如果看看valueOf 的源代碼就知道發生了什麼。

源碼: .

public static Integer valueOf(inti) {
    if(i >= IntegerCache.low && i <= IntegerCache.high)
  return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}      
IntegerCache是Integer的内部類,其代碼如下所示:
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
* 
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun. misc.VM class.
*/
private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];
    static {
  // high value may be configured by property
  int h= 127;
  String integerCacheHighPropValue =
    sun.misc.VM.getSavedProperty("ava.lang.Integer.IntegerCache.high");
  if (integerCacheHighPropValue != null) {
    try {
    int i = parselnt(integerCacheHighPropValue);
    i= Math.max(i, 127);
    / Maximum array size is Integer.MAX VALUE
    h = Math.min(i, Integer.MAX VALUE - (-low) -1);
    } catch( NumberFormatException nfe) {
    // If the property cannot be parsed into an int, ignore it.
    }
  }
  high=h;
 
  cache = new Integer(high-low) + 1];
  int j = low;
  for(int k = 0; k < cache.length; k++)
    cache[k] = new Integer(j++);
  // range [-128, 127] must be interned (JLS7 5.1.7)
  assert IntegerCache.high >= 127;
    }
    private IntegerCache() {}
}      

簡單的說,如果整型字面量的值在-128到127之間,那麼不會new新的Integer 對象,而是直接引用常量池中的Integer對象,是以上面的面試題中f1= =f2 的結果是true,而f3= =f4的結果是false.

5. String 類常用方法

Java的基本資料類型

6. String、 StringBuffer、 StringBuilder 的差別?

(1)、可變不可變

String:字元串常量,在修改時不會改變自身;若修改,等于重新生成新的字元串對象。

StringBuffer:在修改時會改變對象自身,每次操作都是對StringBuffer對象本身進行修改,不是生成新的對象;使用場景:對字元串經常改變情況下,主要方法: append (),insert ()等。

(2)、線程是否安全

String:對象定義後不可變,線程安全。

StringBuffer:是線程安全的(對調用方法加入同步鎖),執行效率較慢, 适用于多線程下操作字元串緩沖區大量資料。

StringBuilder:是線程不安全的,适用于單線程下操作字元串緩沖區大量資料。

(3)、共同點

StringBuilder與StringBuffer有公共父類AbstractStringBuilder(抽象類)。

StringBuilder、StringBuffer的方法都會調用AbstractStringBuilder中的公共方法,如super.append(…)

隻是StringBuffer會在方法上加synchronized關鍵字,進行同步。最後,如果程式不是多線程的,那麼使用StringBuilder效率高于StringBuffer.

7.資料類型之間的轉換

(1)、字元串如何轉基本資料類型?

調用基本資料類型對應的包裝類中的方法parseXX(String)或valueOf(String)即可傳回相應基本類型。

(2)、基本資料類型如何轉字元串?

一種方法是将基本資料類型與空字元串("")連接配接(+ )即可獲得其所對應的字元串;另一種方法是調用String類中的valueOf()方法傳回相應字元串。