Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String literal in array

 
Mark Justison
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question regarding strings in an array. I have this code:


I'm confused by the fact that someStuff[0] == someStuff[2]. I was under the impression that since String[] someStuff = new String[3], it created 3 new String objects and I was simply assigning a value to them. I'm not sure how they can be the same object.
The second array behaves and I figured it would. even after I assign moo[0] = "Angus", it's a different object and therefore should fail an object comparison test to the "Angus" at index 2.

The reason this came up at all is a mistake in a program I wrote that compares strings in an array with arguments passed in from the command line. I mistakenly used '==' instead of '.equals()' and was curious if String arguments passed to the command line were placed in the string literal pool or placed on the heap.
Is that what is happening with someStuff? Even though there are 3 new String objects, does someStuff[0] and someStuff[2] end up pointing to the same "Cow" on the string literal pool? Does this mean that nothing is pointing to those two String objects in someStuff that I thought I was assigning "Cow" to?
I'd greatly appreciate an explanation!

-Mark
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java's string pool

What makes you think there are "three new objects"? (Hint: "I was under the impression that since String[] someStuff = new String[3], it created 3 new String objects" <-- check that)
 
Mark Justison
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that I'm explicitly creating new String objects in 'moo' and not in 'someStuff' does clue me in that perhaps I'm not creating new String objects in someStuff. I think where I misunderstood is that 'String[] someStuff = new String[3];' is creating a String Array object and not three String objects, correct?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you should definitely read this excellent article about String literals. It's a must read for any OCA and OCP candidate.

I think where I misunderstood is that 'String[] someStuff = new String[3];' is creating a String Array object and not three String objects, correct?

The statement String[] someStuff = new String[3]; creates an array of 3 String reference variables. Each of these array elements refers to null. So you are correct! No String objects are created. In fact, the statement creates only 1 object: the array itself. Every array IS-A Object, meaning both print-statements in this code snippet will print true

Hope it helps!
Kind regards,
Roel
 
Mark Justison
Ranch Hand
Posts: 31
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, of course. It creates reference variables and not objects. Definitely one of those concepts I remember learning and forgot that I forgot it.
In that case the reason that someStuff[0] == someStuff[2] is true is that the two String ref variables are pointing to the same "Cow" in the string literal pool...I think.
At any rate I'll look over that article because it's pretty obvious this concept has yet to sink in.

Thanks!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Justison wrote:In that case the reason that someStuff[0] == someStuff[2] is true is that the two String ref variables are pointing to the same "Cow" in the string literal pool...I think.

Exactly!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic