• Post Reply Bookmark Topic Watch Topic
  • New Topic

String - the proto-auto-boxer!  RSS feed

 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading the discussion about autoboxing in this thread, I got to thinking that what the Javagods have come up with as autoboxing can be considered to have kind-of existed even earlier, with the String class.

Here is the "alternate theory":

is essentially a wrapper class for a primitive type called string.

Comments.


This perspective might help people trying to grapple with the intricacies of autoboxing (such as the one discussed in that thread) in light of known existing behavior - that of String.

Comments.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is essentially a wrapper class for a primitive type called string


Wow, I don't think I'd go there. Talking about an imaginary primitive doesn't seem to be the way to clear up confusion in the world.

String is just a class like any other - except for some JVM magic about its internal storage that you can ignore almost all the time outside academic discussions.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no string primitive.

You might confuse it with String literals, which are objects, too.

Try

System.out.println("I'm an Object!".length());
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
There is no string primitive.

You might confuse it with String literals, which are objects, too.

Try

System.out.println("I'm an Object!".length());



Hey, I didn't mean there was one! But the autoboxing feature seems to give a certain transparency to the use of primitives and their wrappers, it's as though something like that already existing with strings.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want to clear up confusion about autoboxing by creating confusion somewhere else - by telling people that java.lang.String is a wrapper for a non-existent primitive type string? I don't think that's the best way to explain Java to people.

Just explain autoboxing as it actually is - you can rewrite Java code which relies on autoboxing to code that doesn't use autoboxing. That will make it clear what happens.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The similarity is that now we can get into just as much trouble using == to compare primitives as we always have when using it to compare strings.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!