字串String物件構造方法的建立和直接賦值的區別
阿新 • • 發佈:2019-01-09
/* * 通過構造方法建立的字串物件和直接賦值方式建立的字串物件有什麼區別呢? * 區別是:通過構造方法建立的字串物件是在堆記憶體。通過賦值建立的字串物件是在方法區的常量池 * * == * 基本資料型別:比較的是基本資料型別的值是否相同 * 1)四種整數型別(byte、short、int、long) * 2)兩種浮點數型別(float、double) * 3)一種字元型別(char) * 4)一種布林型別(boolean):true 真 和 false 假。 * 引用資料型別:比較的是引用資料型別的地址值是否相同 (String是引用型別)*/ public class StringDemo2 { public static void main(String[] args) { String s1=new String("hello");//通過構造方法建立字串物件 String s2="hello";//直接賦值建立的字串物件 System.out.println("s1:"+s1); System.out.println("s2:"+s2); //比較的是2個物件的地址 System.out.println("s1==s2:"+(s1==s2));//false String s3="hello"; System.out.println("s1==s3:"+(s1==s3));//false System.out.println("s2==s3:"+(s2==s3));//true } }