Java String variable setting - reference or value?

后端 未结 9 806
感动是毒
感动是毒 2020-11-30 01:08

The following Java code segment is from an AP Computer Science practice exam.

String s1 = \"ab\";
String s2 = s1;
s1 = s1 + \"c\";
System.out.println(s1 + \"         


        
9条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 02:10

    String is a class, so a String variable is a reference. But it's a language intrinsic, in the sense that Java has special handling and syntax for it, which is why you can do things like your example.

    See e.g. http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html.

提交回复
热议问题