Why do we declare strings in Java using the class String (not string)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why do we declare strings in Java using the class String (not string)?

For other data types, you can declare variables using the data types int, double, char, boolean, etc. However, we use the class String instead of the lowercase data type string. I heard this has something to do with autoboxing but I’m not sure I completely understand it.

1st Oct 2018, 4:50 AM
Roger Wang
Roger Wang - avatar
5 Answers
+ 2
String class has many useful stuff like charAt() and substring that a native string type could not have. All the other data type separate int from Integer class, char to Character class... I think there is no string because string is basecally a char array (not sure about that)
1st Oct 2018, 5:14 AM
Daniele Bonomi
Daniele Bonomi - avatar
+ 1
Daniele Bonomi Then why don’t the useful functions and methods of other data types make them intialized as instances of that class?
1st Oct 2018, 5:36 AM
Roger Wang
Roger Wang - avatar
+ 1
Roger Wang because of efficiency! Primitive type variables are stored in the Stack and Reference type variables hold a reference to the Heap where the instance/object is stored. Therefore if you used everything as a reference you would have to access memory twice for a single increment, for example!
1st Oct 2018, 6:55 AM
Chriptus13
Chriptus13 - avatar
+ 1
Chriptus13 How are strings different than other variable types that make them more efficient this way?
1st Oct 2018, 2:58 PM
Roger Wang
Roger Wang - avatar
0
ha
1st Oct 2018, 8:53 PM
Asutosh Mohanty (lulu)
Asutosh Mohanty (lulu) - avatar