aspose file tools*
The moose likes Beginning Java and the fly likes Understanding String as an Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Understanding String as an Object" Watch "Understanding String as an Object" New topic
Author

Understanding String as an Object

Michael Farinha
Greenhorn

Joined: Jun 16, 2006
Posts: 26
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

Joined: Feb 17, 2004
Posts: 73
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...


Si altas son las torres, el valor es alto - Alberti
Michael Farinha
Greenhorn

Joined: Jun 16, 2006
Posts: 26
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Dani Atrei
Ranch Hand

Joined: Feb 17, 2004
Posts: 73
Sorry, I just realised my answer wasnt very clear... Thanks Ernest for clearing things up
Michael Farinha
Greenhorn

Joined: Jun 16, 2006
Posts: 26
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

Joined: Jan 10, 2002
Posts: 61654
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Understanding String as an Object