Java中的小知識點總結(一)

Java中的小知識點總結(一)

1.佔位符

  • String.format():該方法中有效佔位符爲 %s%d%c 等。
  • MessageFormat.format():該方法中有效佔位符爲 {?}? 爲非負整數。
import java.text.MessageFormat;

public class Main {
    public static void main(String[] args) {
        //type1
        System.out.println(String.format("數字:%d,字符串:%s", 1, "abc"));
        //type2
        System.out.println(MessageFormat.format("數字:{0},字符串:{1}", 1, "abc"));
    }
}

2.字符串的比較

  • 該比較是在Java7之後版本運行的。(Java 7 之後常量池裏)

2.1 創建字符串 = 和 new 的區別

  • 通過 s1=“abc”; 創建字符串時,字符串被存在常量池中,s1指向常量池中字符串的地址。
  • 通過s2=new String(“abc”); 創建字符串時,首先字符串被存在常量池中,然後在堆中保存字符串的副本,最後s2指向中字符串地址。
        String s1 = "abc";
        String s2 = new String("abc");

        System.out.println(s1==s2);//false

2.2 字符串在常量池中和堆中的區別

  • 常量池中的字符串對象是唯一的。
  • 堆中的字符串對象是不唯一的。
		String s1 = "abc";
        String s2 = "abc";
        String s3 = new String("abc");
        String s4 = new String("abc");

        System.out.println(s1 == s2);//true
        System.out.println(s3 == s4);//false

2.3 intern()方法

  • 字符串存在於常量池中,intern()方法會返回一個對象引用,該對象引用指向常量池中的字符串。
		String s1 = "abc";
        String s2 = new String("abc");

        System.out.println(s1 == s2.intern());//true
  • 字符串不存在於常量池中,intern()方法首先會在常量池中保存該字符串對象的引用,然後返回一個對象引用,該對象引用指向常量池中的引用
        String s1 = new String("ab")+new String("cd");//1
        System.out.println(s1 == s1.intern());//true

        String s2 = new String("ab")+new String("cd");//2
        System.out.println(s2 == s2.intern());//false

        System.out.println(s1 == s2.intern());//true

        /**
         * 執行完1時,常量池中存在"ab"和"cd";堆中存在"abcd";s1指向堆中"abcd"的地址。
         * s1.intern()執行,因爲常量池中並不存在"abcd",所以會將堆中"abcd"的引用保存在常量池中。
         * 此時常量池中存在的是:"ab"和"cd"和一個象徵"abcd"引用,這個引用指向的是堆中保存的"abcd"。
         * 所以: s1 和 s1.intern() 實際指向同一個對象。
         */
        /**
         * 執行2後,常量池中存在的是:"ab"和"cd"和一個象徵"abcd"引用(s1的);
         * 堆中存在兩個"abcd"對象(一個s1的,一個s2的)。
         * s2.intern()執行,因爲常量池中存在一個象徵"abcd"引用,
         * 會返回一個對象引用,該對象引用指向那個象徵"abcd"引用;
         * 實際上,s2.intern()返回的對象引用指向的是堆中的"abcd"(s1的)。
         * 所以: s2 和 s2.intern() 實際指向非同一個對象;
         * s1 和 s2.intern() 實際指向同一個對象。
         */

2.4 使用中間變量也會返回指向堆中對象的引用

  • **" "**形式的字符串相加,在編譯階段會被直接合並。eg.“ab”+“cd”—>“abcd”。
  • 通過中間變量,字符串相加,在編譯階段不會被合併處理,會返回堆中對象的引用。(類似new)
        String s1 = "ab";
        String s2 = "ab"+"cd";
        String s3 = s1 +"cd";
        String s4 = "abcd";

        System.out.println(s2==s3);//false
        System.out.println(s2==s4);//true

3.System.identityHashCode()

  • 該方法是System類中的靜態方法,根據對象內存地址來計算哈希值。
  • 可以用來判斷兩個引用對象是否指向同一實例。(在2中可以判斷字符串是否是同一對象)
        String s1 = new String("ab")+new String("cd");
        String s2 = new String("ab")+new String("cd");

        System.out.println(System.identityHashCode(s1));//1349393271
        System.out.println(System.identityHashCode(s1.intern()));//1349393271
        System.out.println(System.identityHashCode(s2));//1338668845
        System.out.println(System.identityHashCode(s2.intern()));//1349393271

4.賦值中的技巧

4.1 可讀性增強

//java7之後支持
//可以使用下劃線分割,獲得更好的可讀性
int x=1_000_000;

//可以省略小數點前的數字
float y=.5f;

4.2 類型提升

//+、-、*、/、~會導致類型提升
byte x = 2;
byte y = +x;		//錯誤
byte y = (byte) +x;	//正確

4.3 StringBuffer和StringBuilder

  • String對象會存在常量池中,如果對字符串多次操作(比如:拼接),最好使用StringBuffer或StringBuilder,使其操作完成後再轉爲String對象。
  • StringBuffer同步方法
  • StringBuilder異步方法

5.嵌套類

5.1 靜態嵌套類

  • 靜態嵌套類可以有靜態成員。

  • 靜態嵌套類只能訪問外部類的靜態成員。

  • 舉例:

    public class Outer {
    	private static int value = 10;
    
    	protected static class Nested {
    		public int getValue() {
    			return value;
    		}
    	}
    }
    
  • 訪問方式:

  public static void main(String[] args) {
  		Outer.Nested nested=new Outer.Nested();
  		System.out.println(nested.getValue());
  }

5.2 內部類

  • 內部類不可以有靜態成員。
  • 內部類可以訪問外部類的靜態和非靜態成員,包括私有成員。

5.2.1 成員內部類

  • 舉例:

    public class Outer {
        private int value = 10;
    	
    	private static int staticValue=11;
    
    	protected  class Nested {
    		public int getValue() {
    			// 可以訪問外部類的非靜態、靜態、私有成員
    			return value+staticValue;
    		}
    	}
    }
    
  • 訪問方式:

    public static void main(String[] args) {
    		Outer outer=new Outer();
    		Outer.Nested nested=outer.new Nested();
    		System.out.println(nested.getValue());
    }
    

5.2.2 局部內部類

  • 局部內部類作用域位於代碼塊之中。例如:if語句塊、while語句塊等。

  • 舉例:

    public interface Logger {
        void log(String message);
    }
    
    public class Outer {
        String time = LocalDateTime.now().format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
        
        public Logger getLogger() {
    		class LoggerImpl implements Logger {
    			@Override
    			public void log(String message) {
    				System.out.println(time + " : " + message);
    			}
    		}
    		return new LoggerImpl();
    	}
    }
    
  • 訪問方式:

    public static void main(String[] args) {
        Outer outer=new Outer();
        Logger logger=outer.getLogger();
        logger.log("Hello!");
    }
    

5.2.3匿名內部類

  • 匿名內部類沒有名稱,用於實現接口。

  • 接口:

    public interface Logger {
        void log(String message);
    }
    
  • 舉例:

    public static void main(String[] args) {
        Logger logger=new Logger() {
            @Override
            public void log(String message) {
                System.out.println("Hello "+message);
            }
        };
        logger.log("world!");
    }
    

6.參考資料

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章