java strings inmutable but the code doesn't shows that [duplicate] - java

This question already has answers here:
Immutability of Strings in Java
(26 answers)
String is immutable. What exactly is the meaning? [duplicate]
(19 answers)
Closed 4 years ago.
I was learning string concepts, so wrote a code,expected a different output but got something very unexpected.
class stringmute
{
public static void main(String[] args)
{
String s1="Hello "; //string one.
System.out.println("Str1:"+s1);
String s2= s1+"world"; //New String.
System.out.println("Str2="+s2);
s1=s1+"World!!"; //This should produce only Hello right?
System.out.println("Str1 modified:"+s1);
}
}
when I execute the above code i get the output as:
Str1:Hello
Str2=Hello world
Str1 modified:Hello World!!
if i've done something wrong please let me know.
Since strings are immutable, which implies we should get the output of the "Str1 Modified" as "HELLO" instead of "HELLO WORLD!!".

When you assign s1 as :
s1=s1+"World!!";
New String created in jvm string pool and assigned to s1.
So it's value became "Hello World!!"

Related

Java - reference equality in strings [duplicate]

This question already has answers here:
What is the Java string pool and how is "s" different from new String("s")? [duplicate]
(5 answers)
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I'm preparing for the OCA and I am puzzled by reference equality in strings.
So given Case 1:
String s = "Hello";
if("Hello" == s){
System.out.println("TRUE");//prints TRUE
}else {
System.out.println("FALSE");
}
But in Case 2:
String s = "";
s += "Hello";
if("Hello" == s){
System.out.println("TRUE");
}else{
System.out.println("FALSE"); //prints FALSE
}
I know that strings are immutable, but "==" checks for reference equality, and in the second case "s" is a reference that initially points to an empty string and then points to "Hello". So, why does the second case return false?
What am I missing?
Regarding:
What is the Java string pool and how is "s" different from new String("s")?
In my question, in both cases "s" is a string literal and found in the string pool. The above mentioned questions is about a string literal and a String object (which gets memory allocated).
Regarding:
How do I compare strings in Java?
My question asks "why", not "how".
#Mark Rotteveel
Yes I understand that ""+"Hello" is a different string than "", but we are discussing reference equality where "s" is a reference which first points to a string "", and then the same reference "s" points to another string "Hello". In both cases, before the if-clause, the reference "s" points to "Hello", but the results to the if-clause are different.
Thanks for having a look!

Comparing character of an array to a string [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
What's the best way to compare character of an array to a string.
import java.io.Console;
class msd{
public static void main(String[] args){
Console console= System.console();
String letter="Hello";
String[] Arrayname=letter.split("");
if(Arrayname[0] == "H")
console.printf("success");
else
console.printf("failure");
}
}
Output
failure
Expecting to be success.
You have to use the equals method to do so:
Arrayname[0].equals("H")
The == operator compares references not values.

Having issues with replaceAll() [duplicate]

This question already has answers here:
String replace method is not replacing characters
(5 answers)
Closed 7 years ago.
Maybe I am using replaceAll incorrectly, but I am unable to find why it acts this way. I want to simply remove a $ sign from a string and then output the string.
public class Example{
public static void main(String[] args){
String s = "$50";
s.replaceAll("\\D+", "");
System.out.println(s);
}
}
However, this still outputs the $ symbol with the string. Does anyone know why this is happening?
You need to assign the return value of replaceAll to a variable:
s = s.replaceAll("\\D+", "");
because a String object is immutable.

String & objects in Java [duplicate]

This question already has answers here:
What is the difference between "text" and new String("text")?
(13 answers)
Closed 8 years ago.
String a = "Hello";
String b = new String("Hello World");
Can someone please tell me how many objects are created and elaborate.
Thank you.
String greeting = "Hello world!";
In this case, "Hello world!" is a
string literal—a series of characters in your code that is enclosed in
double quotes. Whenever it encounters a string literal in your code,
the compiler creates a String object with its value—in this case,
Hello world!.
String a = "Hello"; // 1 object
String b = new String("Hello World");
// 1 object with new String(),
// 1 object with "Hello World"
in total you created 3 objecs.

How do I use substring method in ruby? [duplicate]

This question already has answers here:
How to get a substring of text?
(5 answers)
Closed 8 years ago.
I made a program in java like this:
class substr{
public static void main (String[]args){
int n=1;
String s1="";
String s2=" ";
while (n<=5){
System.out.println(s2.substring(n,5)+n+s1+n);
s1=s1+" ";
s2=s2+" ";
n++;
}
}
}
I can't make use of substring in ruby, so I wanted to know if there is some way. I read about regex, but you don't use it in a program like this.
You can use array notation to substring an string. In your case, something like s2[n,5].

Categories