zoukankan      html  css  js  c++  java
  • Java 中 String 、final 相关知识点

    1.java中 String str="abc";和String str=new String"abc"; 有什么区别

    如果是String str = "abc";编译器首先会在常量池中寻找有没有"abc"这个字符串,如果有则直接从常量池中取,不会new,如果常量池中没有,则会new一个,并将"abc"存放到常量池中。
    而String str = new String( "abc");则编译器直接new一个字符串,不会到常量池中查询。
    所以:
    String str1 = "abc";
    String str2 = "abc";//str1是"abc",常量池中已经有了"abc",所以str2直接从常量池中取
    String str3 = new String("abc");
    String str4 = new String("abc");
    System.out.println(str1 == str2);//true
    System.out.println(str2 == str3);//false
    System.out.println(str3 == str4);//false

    2.final

    final修饰的成员变量为基本数据类型是,在赋值之后无法改变。当final修饰的成员变量为引用数据类型时,在赋值后其指向地址无法改变,但是对象内容还是可以改变的

  • 相关阅读:
    牛客算法周周练18A
    洛谷P2580
    Codeforces 617E
    SPOJ 3267
    Codeforces Round #661 (Div. 3) 解题报告(ABCD)
    Codeforces 1399D
    Codeforces 1399C
    Codeforces 1399B
    Codeforces 1399A
    牛客算法周周练18 解题报告(ABCE)
  • 原文地址:https://www.cnblogs.com/listenerxx/p/14106291.html
Copyright © 2011-2022 走看看