Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java The Thing About Strings

What were the creator's motivation?

In most programming languages string is a primitive data type. I do not understand why the creator's made Strings and object. Thank you. :)

Not sure what you mean. Why the creators make String and Object? How else would you do it?

String:

String myName = "Aayush";

Int: int myAge = 11;

Why is string capitalized and int isn't? Why couldn't they have made it so that strings are primitive data types too?

2 Answers

Brendon Butler
Brendon Butler
4,254 Points

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. - Official Javadocs

A String is a string of characters. So in a way chars would be the primitive of Strings. That is really stretching it though.

Primitive data types were created for performance reasons. And they're just that -- primitive.

Primitive types are the most basic data types available within the Java language... Such types serve only one purpose — containing pure, simple values of a kind. - Wikibooks

Thank you that helps me understand better :)

Because the pritimive data types dont have methods, and since strings are so common throughout programming they decided to do it like that. I think google is a better ressource to answer this question. That being said:

There are two different types of strings, string litterals and string obeject. A string litteral is what your wrote in your example, whats interesting about string litterals is that Java is smart enough to know that if two string litterals decleared in a program share the same content, they are stored in the same memory location and can therefor be accesed via the same pointer. (They are both stored in whats called the common pool, as apposed to heap)

String objects instead is delcared like any other object using the 'new' keyword. Now the difference between the .equals method and the == operator is that .equals looks at the content of the string where as == operator actually looks at wether the two variables share the same memory pointer. Example:

String myStringLiteral1 = "Hello";
String myStringLiteral2 = "Hello";
String myStringObject1 = new String("Hello");
String myStringObject2 = new String("Hello");

myStringLiteral1.equals(myStringLiteral2); // True, they have the same content.
myStringLiteral2.equals(myStringObject1); // True, they have the same content.
myStringLiteral2 == myStringLiteral1; // True, they share storage in common pool.
myStringLiteral1 == myStringObject1; // False, they dont have the same pointer.
myStringObject1 == myStringObject2; // False, they dont have the same pointer.

This also explains why string in java are immutable, seeing as two different string literals with same content share same common pool, modifying one will mess with the value of the other one. Which is why methods like .toUpperCase() dont actually modify the string but instead returns a copy of the string.

This is far from the full explanation, but i hope it helps to see why java creators decided to do it this way, it surely also has disadvantages perhaps relating to performance, but its not something you should worry about.

Hope this helped!

Thank you :)