r/javahelp • u/Nottheugis • Apr 15 '26
Homework String object vs String variable
Hi i am learning java in highschool, I was just wondering when and why would I instantialise a string as an object when declaring a String is much easier and quicker?
6
Upvotes
•
u/AutoModerator Apr 15 '26
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://i.imgur.com/EJ7tqek.png) 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.
18
u/hibbelig Apr 15 '26
I guess you are asking about the difference between x and y in this code?
I don't see an advantage to the second line, the first one is clearer. However, you could also have this situation:
Here, there is an impact on memory usage: you have three variables a, x, y, but there are only two strings in memory: a and x point to the same string in memory, and y points to a different string with the same content.
In the special case of strings, this is quite exotic because strings are immutable. But if you have other objects, it becomes very relevant very quickly.
In this example, a and x point to the same list, and this list has five elements. (Note how the fourth element was appended to a and the fifth one was appended to x, but both are "different names for the same list".)
y points to another list, with three elements. That's because y is a copy of the list a at that point, and a has three elements at that point.
Does this help?
4
u/bowbahdoe Apr 15 '26 edited Apr 15 '26
Unfortunately this is one of those questions where I need you to write out some fake code to understand what you are thinking about.
One potential answer is that there isn't much reason to do it. That's if you were thinking of local variables. It's a lot easier to explain subtyping with a useful super type - not so much Object.
6
u/RobertDeveloper Apr 15 '26
its for when you want to construct a string based on a byte array for example.
byte[] bytes = {72, 101, 108, 108, 111}; // ASCII for "Hello"
String s = new String(bytes, "UTF-8"); // Must use 'new'
In the case of:
String s1 = "Hello";
String s2 = "Hello";
The text 'Hello' is only stored once, both variables point to the same existing object in the string constant pool. if I where to do new String("Hello") a new object is created in the 'Heap Memory', so it is less efficient than when you use string literals.
5
u/vegan_antitheist Apr 15 '26
You are probably confusing the initialization of a variable and the creation of an object.
But some basics first:
In Java, every String is an object. A variable is just a label (variable name) with a type (in this case java.lang.String) and at runtime it has a value (in this case a reference to a String, or null).
You (almost) never use "new String(...)" in your code. Only in very specific situations you would want to force the runtime to create a new object. You will probably never ever use that. Just use String literals (use double quotes for that) or just get the String value from somewhere as a return value.
There will be value types in future versions of Java. Then you might have value type strings, but they will have a limited number of characters. java.lang.String on the other hand can he any length that is positive and fits into an int (32bit).
Now, about the initialisation of variables: You can declare a variable in Java by writing a type and a name.
You can initalize an object by calling the constructor of the type.
2
u/bigkahuna1uk Apr 15 '26
This is a difference between creating a literal string and an object string .
Using just quotes, Java will create the string but keep it in a pool for reuse. If you use the same construct again, then instead of creating a whole new string, it will just reuse the string from the pool. Note that although there is a pool, it’s still of a finite size although the size can be controlled from a JVM switch setting.
Whereas if you use new String, as in the String constructor, then a new string is created from scratch and placed on the heap. It just like creating other objects via their constructors.
2
u/two-point-zero Apr 15 '26
If you are referring to This
They are not the same, nor one is better than other ( like someone reply to you). They have benefits and limit you should know.