If Strings in java are immutable, why I can do this? [duplicate] - java

This question already has answers here:
Immutability of Strings in Java
(26 answers)
Closed 3 years ago.
I realize that the hashCode of the variable name, is different after the "update", but objectively what makes a String object in fact immutable ?
public static void main(String[] args) {
String str = "AB";
System.out.println(str ); // AB
str = str .replace(str .charAt(0) ,'W');
System.out.println(str );//WB
}
EDIT 1 : The hashCode is based on the value of the variable and have no relation with memory adress.
EDIT 2 : I now understand that Strings are references and not Objects in it self.
I read back all the answers for this same question and found out good answers in topics like [this] (Immutability of Strings in Java). Thank you whos tried to help me and my excuses for any silly misunderstood.
I also recommend this articles here to who wants better understand how Strings works in Java :
https://www.pushkarrajpujari.com/article/strings-in-java/
and how references works :
https://javaranch.com/campfire/StoryPassBy.jsp
EDIT 3: I cannot DELETE this topic anymore, according with Stackoverflow "You cannot delete this question as others have invested time and effort into answering it." which I agree.

If you look at the documentation of replace(), it mentions:
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
Therefore, the replaced String is an entirely new String.

Related

alternative for strings combing strings that isnt "+" [duplicate]

This question already has answers here:
String variable interpolation Java [duplicate]
(5 answers)
Closed 1 year ago.
Is there any way to join a string and another variable together, that is also a string, without using the "+" operator. Specifically one similar to this one that is done in C#.
string str = $"Hello {userName}. Today is {date}.";
If there is any way to achieve a similar or same outcome in Java please let me know.
See https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
String.format("Hello %s. Today is %s.", userName, date);

how to view the the address of object of String class? [duplicate]

This question already has answers here:
How can I get the memory location of a object in java?
(4 answers)
Closed 2 years ago.
described above. for example.
String s = "abc";
System.out.println(s); // this method will output the string, not the address
so how to view the address, thanks in advance.
Java has no concept of "address", so it is impossible to get the address of any object, including strings.
if u want an answer as u mentioned in the comment, Actually there is a way to do it that way.
String s = new String("abc");
System.out.println(Integer.toHexString(s.hashCode()));
this will return
1ae66
This answer was historically accepted as correct and will only work for classes that didn't override the hashCode() method

Array == String [duplicate]

This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 7 years ago.
I am doing a "Rock,Paper,Scissors" game with Java and I have a question, is there any way to create and Array with answers and check if any of these values equal to another array? For example:
Here is the answerArray (also I want to have short cuts for answers):
String[] answersArray = {"Rock","R","Paper","P","Scissors","S"};
And here is a randomArray for computer:
String[] npcAnswer = {"Rock","Paper","Scissors"};
int idx = new Random().nextInt(npcAnswer.length);
String npcRandomAnswer = (npcAnswer[idx]);
So, I want to check through the scanner if my answer (answersArray) equal to npcRandomAnswer.
Sorry if I have grammar mistakes, I did my best to explain my point.
Thank you.
You can only compare apples with apples. Or actually you can compare apples with oranges, but then you don't really need to compare them, you know the answer will always be false.
It looks like you don't really know yet what will your algorithm be.
I suggest you to concentrate on the algorithm, write pseudo code, and when you have it, try to make java out of it. If you'll have problem with the java, we can easily help you if you post your algorithm (in a new question)

How many object created when creating object of String [duplicate]

This question already has answers here:
Questions about Java's String pool [duplicate]
(7 answers)
Closed 8 years ago.
Actually I am bit confused that how many object created in below "code processing".
String s=new String("A");
s=s+"B";
Actually someone said that here 4 objects will be created but in whole processing but how not understand.
Please anyone can give me detail description also included memory part such string pool etc.
The first String created is literal "A", that is, if not interned
prior.
The second String is the instance generated by the new keyword.
The third one is literal "B", again, if not interned prior.
The last one is the concatenation of s and "B".
You have two String literal Objects, namely "A" and "B". Then you explicitly instantiate a new instance of "A" with new String("A");. Finally, the fourth instance is created when you perform the String concatenation s+"B"

Updating a string [duplicate]

This question already has answers here:
compare and update a string
(11 answers)
Closed 9 years ago.
I already opened a similar question, unfortunetly I didnt explain my problem correctly, sorry about that.
This time I hope everyone understand what I mean.
My intention is to Update a String A with a String B.
For example:
String A:
A
B
C//Good morning, sir
D//A comment
E
String B:
A
B//Yes
C
D
DD
E
The result should be:
A
B//Yes
C//Good morning, sir
D//A comment
DD
E
I do not know where the differences are, I just want to keep everything in String A and just add the new things from String B (to the correct positions).
Has anyone an idea if java can do this?
Well, how do you know where to insert the parts of your string? You could use an array and update write all words of your strings on an index by using a simple loop.

Categories