• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding String as an Object

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic