Question

Can you explain the internal storage for string, in particular string literals, instances of string, and...

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?

Homework Answers

Answer #1

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:

  1. Java tests whether or not a string exists.
  2. If we create a second-string object using a new one and have different content, then it creates an object and assigns a different reference
  3. If we do not create an object using a new one and have the same content, it assigns the same reference as the first string.

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1) Explain the difference between: Array of characters like char c[] = {'A', 'B', 'C'}; AND...
1) Explain the difference between: Array of characters like char c[] = {'A', 'B', 'C'}; AND c-string like string = "ABC"; 2) We can have two pointers that point to one memory location such as: int *intPtr1, *intPtr2; intPtr1 = &x; intPtr2 = &x; a) What method we have to make sure only one pointer points to a variable? b) Give an example. 3) a) Can a pointer to a constant receive an address of a constant item and/or non-constant...
Research and Discuss variables and data types. 1. What are the various data types you can...
Research and Discuss variables and data types. 1. What are the various data types you can use in C#, Explain memory sizes of these data types? 2. Discuss string variables. What does a string variable provide? Provide details. 3. Provide examples how you would create a values like "Sarah", 2, 3.50, and store into memory. For example, DataType VariableName = "Sarah".
Create a function in MIPS using MARS to determine whether a user input string is a...
Create a function in MIPS using MARS to determine whether a user input string is a palindrome or not. Assume that the function is not part of the same program that is calling it. This means it would not have access to your .data segment in the function, so you need to send and receive information from the function itself. The program should be as simple as possible while still using necessary procedures. Follow the instructions below carefully. Instructions: ●...
Objectives: ⦁   Declare and initialize null-terminated string ⦁   Apply indirect address ⦁   Write loop ⦁   Apply...
Objectives: ⦁   Declare and initialize null-terminated string ⦁   Apply indirect address ⦁   Write loop ⦁   Apply Irvine.inc library functions to display a string Problem Description: Write a program with a loop and indirect address that copies a string from source to target. Revising the character order in the process. Use the following variables: source BYTE “This is the string that will be reversed”, 0 target BYTE SIZEOF source DUP(‘#’) You may refer to the Programming Exercise #7 on Page 138...
Can you please explain what information will be needed for socket based communication? Can you please...
Can you please explain what information will be needed for socket based communication? Can you please describe the ONE of the following? a. interrupt procedure; b. how system call parameters are transferred; c. how system call API are provided to users Can you please explain how the boot process overcome ONE of the following obstacles? a. Where to find the OS installed on the storage b. How control flow is transferred from firmware to software c. Missing drivers d. Real...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
You are part of a quality control team for a factory that manufactures high-quality string for...
You are part of a quality control team for a factory that manufactures high-quality string for special purposes. To make sure the strings are all made to the same high standard, you have decided to make a test facility that measures the harmonic frequencies (the frequencies that create standing waves) of the strings as a function of their tension. You decide that the best way do this is to set up a pattern of standing waves at known tensions. Questions:...
C++ Part B: (String) Program Description: Write a word search program that searches an input data...
C++ Part B: (String) Program Description: Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of grammatical characters in the input data file. Your program must do this by providing the following functions : void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount) ; (15%)...
no need to explain. True or False 1. A constructor is a special Class member method....
no need to explain. True or False 1. A constructor is a special Class member method. It is automatically called when an object of the class is created. It can also be called more than once after the object is created. 2. In Java, the new operator is used to create an instance/object of a class in the heap. 3. A reference variable is a memory location contains an address of an object stored in the stack. 4. Encapsulation and...
In the following C code, Which variable if NOT of primitive data type? A. a B....
In the following C code, Which variable if NOT of primitive data type? A. a B. b C. c D. d int a = 10; double b = 20.0; float c = false; char d[5] = "Hello"; // here we define a string In programming language C, the implementation of a string data type is limited dynamic length, which means the length of a string variable is fixed once it has been defined. A. True B. False In C# programming...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT