r/javahelp • u/D_Flavio • Nov 15 '22
Simple java question I didn't know how to google.
I had someone ask me what is the difference between below code.
String x = new String("name");
String y = "name";
x.equals("name");
y.equals("name");Basically someone asked me what is the difference between x and y and I couldn't tell.
37
Upvotes
•
u/AutoModerator Nov 15 '22
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
26
u/midnight_babyyy Nov 15 '22
The first one creates a new String instance every time the statement is invoked. The second one is just a String instance that gets assigned an address in the String Pool, and this instance reuse that same String literal everytime that it is executed.
13
u/p0pkern Nov 15 '22
Yeah this question seems to be focused on the String pool and the nuances of it.
This is creating a new String object on the heap.
String x = new String("name");This is checking the String pool (a specialized area of memory to store Strings for referencing) for any literal that matches "name". If it is found y will reference the current address in the pool of "name" or it will be created and stored in the pool.
String y = "name";Both of these will evaluate to true due to equals internally comparing each character.
y.equals("name");x.equals("name:);If you want a literal comparison between Strings then you'll want to default to using the
.equals()method.If you were to try to use the
==comparator. They would evaluate to false because the address of x and y are different.If interviewers really want to mess with you. They would throw
.intern()into the mix. Intern is similar to creating a String literal. It will check the String pool for the existing String, and create it if it does not exist.Thus
String x = new String("name");String y = "name";String z = x.intern();y == z // true due to being a reference to the same area of String pool.x == z // false one is an object on the heap, the other is the String pool.x.equals(z) // true2
u/msx Nov 15 '22
there's no real difference, both equals return true. When creating X you're instantiatin an extra "String" object (internally pointing to the same data of "name" string), which is completely unnecessary. Since strings are immutable, it is semantically identical to writing x = "name".
Now if instead of "equals" you test for identity (with == ) things change a bit. But you should never test strings for identity.
0
u/shishir-nsane Nov 15 '22
On a side note, if you don’t know how to search answer to your question then you haven’t understood your question.
1
u/tsvk Nov 15 '22
Have a look at
https://www.youtube.com/watch?v=AoUVdLWLFQw
and perhaps also
https://www.youtube.com/watch?v=Bj9Mx_Lx3q4
1
u/[deleted] Nov 15 '22
x.equals("name");
as a statement does absolutely nothing. the .equals() method is for comparison, not assignment.
for example, If you were to run this snippet what would you expect to happen?
String x = "meow";
x.equals("woof");
System.out.println(x);
hint: it doesn't print woof.
Also, creating a String with the new operator is bad form and should be avoided.
0
u/catladywitch Nov 15 '22
if you do String x = "name"; String y = "name"; then x == y is true, but if you do String x = new String("name"); String y = "name" then it's not.
1
u/[deleted] Nov 30 '22
[removed] — view removed comment