Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding String as an Object

 
Michael Farinha
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the difference between primitive and reference datatypes but I'm somewhat baffled by the "String" datatype.

According to the java docs String is an object:
java.lang.String
but why is it that you can declare a string like a primitive?



I've discovered that you can treat a string like an object:


But how is it that you can treat it like a primitive with out declaring "new String()?"

also since you can say:


does that mean a string of characters is defined as an object?

Sorry if this is a bit too simplistic but its something that has boggled me since I leared of the differences between primitives and references.

Thanks!

-Mike
 
Dani Atrei
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because they are used so commonly, strings can be created like primitives, yet they really are objects, thus you can write things like myStringVar.length(). It just makes it easier for the programmer. You have similar things in Ruby for arrays etc...
 
Michael Farinha
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I'm still not understanding.
Is the String class implementing an OO feature to enable it to act like a primitive or is the compiler doing some trick here that enables this special case?

Can I create a class that behaves like String?

Thanks!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not true to say that a String is like a primitive in any way, shape or form. Saying

String aString;

gives you an uninitialized variable that points to nothing, just as

Object anObject;

does. Until you assign an object to the variable, it can't be used for anything.

Now, there is one single thing that's special about the String class: there's a special notation for creating them. If you say

String aString = "Some text";

that's a special shorthand for something which you could imagine like this:

String aString = Java.getStringFromPoolOrCreateANewOne("Some text");

There's no real function by that name, of course; instead Java takes a special action when it sees a quoted String literal. But those special actions are as if this made-up Java.getStringFromPoolOrCreateANewOne() function did something like:



So you see that Strings are always created with "new", just like any other object. It's just that there's a shorthand which hides this from you. It doesn't make Strings anything like primitives, though.
 
Dani Atrei
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I just realised my answer wasnt very clear... Thanks Ernest for clearing things up
 
Michael Farinha
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think I understand.
Strings are basically a special case, nothing really to understand with how java implements it other than "its the way it is."

Also, I have a basic understanding of the string pool so I think your example makes more sense to me than if I didn't know about the string pool.

I just have a hard time dealing with 'special cases.' I like to know why its a special case and how the special case was implemented.

But I think I grasp whats going on.

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65228
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Farinha:

Strings are basically a special case


Not really. The String object acts like any other.

What is the special case is the ability to use the string literal notation in Java code. That's a compiler issue, not a run-time issue.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic