Parsing the delivery of string objects in Java

Source: Internet
Author: User

Words not much to say, directly on the code.

public class demo {//static string str =  "hello world!"; /static string str = new string ("hello world!"); /static char[] ch = new char[]{' A ', ' B ', ' C '};p ublic static void test ( String str,char[] ch,int a)  {system.out.println ("str="  + str); System.out.println ("Str.hashcode ():"  + str.hashcode ());str =  "-------"; System.out.println ("str="  + str); System.out.println ("Str.hashcode ():"  + str.hashcode ());ch[0] =  ' G '; a = 11;} Public static void main (String[] args)  {String str =  "Hello world !"; String str1 = new string ("hello world!"); char[] ch = new char[]{' A ', ' B ', ' C '};int a = 1; System.out.println ("Str.hashcode ():"  + str.hashcode ()); System.out.println ("Str1.hashcode ():"  + STR1.HASHCOde ()); test (str,ch,a); System.out.println ("a="  + a); System.out.println ("str="  + str); System.out.println ("Str.hashcode ():"  + str.hashcode ());str =  "ooooooo";// Change the value of str System.out.println ("Str.hashcode ():"  + str.hashcode ()); System.out.println ("Str1.hashcode ():"  + str1.hashcode ());for  (char e : ch)   {System.out.print (e);} /* ① the values of the above STR and a variables are not changed, but the memory address (hashcode) printed by str changes,  * ②str the same memory address (hashcode) that is printed.  *  *  speculated that the values of: *  different string variables are the same as long as they are identical. A string object is created in a constant pool (open space), and the caller accesses a constant pool of space  *  if the above assumptions are true, then doubts come. (It is advisable to remember the previous one.) when the  * string type variable passes a parameter between methods, is it a pass-through reference (memory address) or value pass?.  *  *  here first, the C + + string variable reference pass is passed memory address, that is, access to the actual memory space of the variable, *  change the value of the variable is to modify the value in that memory space.  *  objects in Java are passed by reference, but if the string is used as a constant, it is obvious that simple reference passing is inappropriate.  *  There are two considerations: 1. Constants cannot change values, 2. If it can be changed, this is a shared data, involving data security issues.  *  So when you modify a value, the good one string object space is first opened in the constant pool, and the reference (memory address) is assigned to the string variable, which points to the space.  *  If the value is passed, when a string has a size of 1G, think about what happens when this string is transmitted? Is it comfortable to walk around with a big stone? Hey, this is the joke  *  the same data, 1G large space, will not open one again.  */}}

Console Print Results:

Str.hashcode ():-217287203

Str1.hashcode ():-217287203

Str=hello world!

Str.hashcode ():-217287203

str=-------

Str.hashcode ():-1680751795

A=1

Str=hello world!

Str.hashcode ():-217287203

Str.hashcode ():-1282542897

Str1.hashcode ():-217287203

GBC


Parsing the delivery of string objects in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.