Can you explain the internal storage for string, in particular string literals, instances of string, and characters.
Also, if two string variables have the same data, is the memory reference the same?
String a = "test";
String b = "test";
will a and b both have the same memory address?
The strings are used to store the sequence of the character in any programming language. They are treated as an object. The string is immutable like an array function. Whenever there is a need to make a change he entirely new string is created.
Strings are stored in the string constant pool in a separate memory location. It is a block of the memory that holds the string variables in the heap area.
Example:
String a=” test”;
String b=” test” ;
Whenever the same name object is created it will not create a new object it will only refer to that.
A "string literal" is a series of characters stored in double quotation marks (") from a source character list. String literals are used to represent a series of characters that form a null-terminated string, taken together.
Where string literals are stored:
Whenever you create a string object with a literal string, that object is stored in the constant string pool, and when you create a string object with a new keyword, that object is stored in the memory of the heap. When you build string objects, for example, as below, they are stored in the String Constant Pool.
Character:
In programming languages, the type of data used to store characters is char. Characters are interpreted in programming languages using Unicode. Unicode is a foreign character range that describes all characters. The char extends from 0 to 65,535.
We will use java language to conclude our result
String a = "test";
String b = "test";
System.out.println(s1== s2);
This gives results true they will have the same memory reference because of a and b point to the same object. As they both have the same name discussed using the diagram.
Conclusion after solving:
Yeah, two or more references may both reference the same thing, e.g. from parameters and/or local variables and/or instance variables and/or static variables.
Get Answers For Free
Most questions answered within 1 hours.