answersLogoWhite

0

You can have two String variables (note that String variables are object references) refer to the same String object like so:

String str1 = "Hello";

String str2 = str1;

Now the str1 and str2 are references for the same String object containing the word "Hello".

If you actually want a new String object with a copy of the contents of the original String, you use the String constructor that takes a String argument, like so:

String str3 = new String(str1);

Now str1 and str3 refer to SEPARATE String objects that happen to contain the same sequence of characters (the word "Hello").

Since Strings objects in Java are immutable, they can be shared without worrying about the contents used by one variable being upset by the use through another variable as might happen with char[] arrays in C or C++ so the first method is probably sufficient for most cases.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Write a program to implement the various operations on string such as length of string concatenation reverse of a string copy of a string to another in VB?

what is string


How do you write c program to accept a string from the console and count number of vowels constants digits tabs and blank spaces in a string?

Read the characters one at a time, and write an "if" for each of the cases. In each case, if the condition is fulfilled, increment the corresponding counter variable.


Write a program in java to display a string message using Servlets?

i dont no string for servlate


Write the program in Linux to find the reverse of any string?

i am sam


Write a program to find the length of a string in shellscript?

This is an assignment not a question.


How do you write a c program to find a word in a string?

what is if(!(str[i]==32))


Write a program to reverse the given string?

Use for loop declare string array str[] and string variable l= string length of string array j=l for i=0 to i=l/2 then temp=str[i] str[i]=str[j-1] str[j-1]=temp j=j-1 now print str array it will be reversed


Write a java program to count the space from the given line?

.... String line = "This is example program with spaces"; String[] tokens = line.split(" "); System.out.println(tokens.length-1); .......


How do you write a c program to find the smallest word in a string?

what is if(!(str[i]==32))


Write a program convert a string from uppercase to lower case and vice-versa without using string tolower and string toupper commands in TCL?

[ string toupper $str ] or [ string tolower $str ]


Do I need to write a program to find a substring in a given string in c plus plus?

No.


How do we write c program without using swap to interchange values by accessing another variable?

int a,b; a=a+b; b=a-b; a=a-b; that's it simple