+ 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.
5 odpowiedzi
+ 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)
+ 1
Daniele Bonomi Then why don’t the useful functions and methods of other data types make them intialized as instances of that class?
+ 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!
+ 1
Chriptus13 How are strings different than other variable types that make them more efficient this way?
0
ha