Java - assinging a replaced string , or printing it [duplicate] - java

This question already has answers here:
Using variables to store values or directly get values from the objects?
(3 answers)
Closed 7 years ago.
Is there a difference between this:
String str1 = "abcabc";
String str2 = str1.replaceAll("a", "");
System.out.print(str2);
And
String str1 = "abcabc";
System.out.print(str1.replaceAll("a", ""));
In terms of memory used, or in other words, will the print method create memory in the heap for the new string?

str1.replaceAll("a", "")
returns a new String instance. So "Yes", it does create it on the heap, & "Yes" they are the same. (memory speaking)

Related

concatenation and displaying a string [duplicate]

This question already has answers here:
Does concatenating strings in Java always lead to new strings being created in memory?
(3 answers)
Converting String to "Character" array in Java
(14 answers)
String to char array Java
(1 answer)
Closed 3 months ago.
a string created with String class can not be modified.
but when we use the += operator does it mean that the original string change?
exp:
String ch="hello"; ch+= "world";
another question:
why we don't use these instructions to display the string ?
for (int i=0;i<ch.length();i++) {System.out.println(ch[i]); }
i tried this
for (int i=0;i<ch.length();i++) {System.out.println(ch.charAt(i)); }
why it is not similar to
for (int i=0;i<ch.length();i++) {System.out.println(ch[i]); }

String new Object and comparision [duplicate]

This question already has answers here:
Are two Java objects with same hashcodes not necessarily equal?
(10 answers)
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
As of i know any changes on String a new Object will create,and for some run time activity if there is a content change then a new object will create in Heap are, but i am confusing on below cases, please give idea...
String s8="abcd";
String s9=s8.toUpperCase();
String s11=s8.toUpperCase();
System.out.println("S9 "+s9.hashCode() +" s10 "+s11.hashCode());//S9 -- 2001986 s10 -- 2001986
System.out.println(s9==s11);//false
In the above scenario the address is printing same but the == operator shaowing false.
please tell why address is same and comparision is false.
String s8="abcd"; : Memory will be allocated from constant pool.
String s9=s8.toUpperCase(); New object will be created on heap
String s11=s8.toUpperCase(); Another New object will be created on heap
If you look at the implementation of toUpperCase
public String toUpperCase(Locale locale) {
..
return new String(result, 0, len + resultOffset);
Hence it creates a new object on heap each time. therefore s9 != s11
Note: If two objects are equal then their hashcodes are equal but vice
versa is not true
UPDATE:
String s11=s9.toUpperCase();
s11==s9 // returns true
Because there are not chars which can be modified and therefore s11 and s9 both points to the same object. I strongly recommend to you to read the implementation
== operator is used for reference comperison. Basically when you create s9 and s11 then only 1 object is created in heap. That's why those 2 hashcode is same and 2 different references are pointing same object. That's why s9==s11 has retured false.

How string concat method works [duplicate]

This question already has answers here:
String is immutable. What exactly is the meaning? [duplicate]
(19 answers)
Closed 6 years ago.
I know String in Java is immutable. Consider:
String s = "Java";
s = s.concat(" is simple");
System.out.println(s); //prints Java is simple
Here the value of String s is changed? Can someone explain me, how String concat actually works?
s.concat(" is simple") returns a new string and s = s.concat(" is simple") assigns this new string to variable s.

Best approach of Creating String Object [duplicate]

This question already has answers here:
Java Strings: "String s = new String("silly");"
(23 answers)
What is the difference between "text" and new String("text")?
(13 answers)
Closed 8 years ago.
In java we can create String in the following 2 ways -
String str1 = new String("first string"); //1
String str2 = "second string"; //2
Is there any difference in performance with these 2 approaches? And in the second case is there any new String object created?
Thanks in advance.
The first approach forces the creation of a new String object. The second allows java to use the constant from the string pool, and should generally be preferred.

When is `StringBuilder` is preferred instead appending `String` to `String`? [duplicate]

This question already has answers here:
StringBuilder vs String concatenation in toString() in Java
(20 answers)
When to use StringBuilder in Java [duplicate]
(9 answers)
Closed 9 years ago.
Below are two ways how to append String:
String firstString = "text_0";
String secondString = "text_1";
String resultString = firstString + secondString;
StringBuilder sb = new StringBuilder();
sb.append(firstString).append(secondString);
String resultString = sb.toString();
My question is - when is more effective to use StringBuilder? Let's say there are 10 strings, and I need to create one of them.
Because StringBuilder can "append" a string instead of concatenating two strings each time creating a new object. Even if you use += operator with Strings a new object is created. This advantage will only become relevant once you try to concatenate a great number of strings. If is also consiedered a bit more readable.
Two main Advantages:
Mutable
Not Synchronized.

Categories