This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

String array declaration doubt

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,

I had a small doubt, and hopefully, you'll could clear that for me:

Is this legal?

1. String x = new String[] {"aaa","bbb","ccc","ddd","eee"} [4];
//meaning String reference x would hold a value "eee" ?

And how about:

2. String x = new String[] {"aaa","bbb","ccc","ddd","eee"} [5];
//If legal, would the String reference x be null?

Assuming 1 and 2 are correct (legal), would the anonomous String array be eligible for gc at the end of the particular line?

Thanx...

Cheers,
Kaushal Shah
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kaushal,


String x = new String[] {"aaa","bbb","ccc","ddd","eee"} [4];



This statement is perfectly legal. Here you are creating an anonymous String array object with 5 elements and you are selecting the 5th element and assigning it to the string variable x. So after execution of this line the String variable x will be having the value eee.

After execution of this line the anonymous string array object will be eligible for garbage collection.


String x = new String[] {"aaa","bbb","ccc","ddd","eee"} [5];


Here the syntax is legal but you are trying to access the sixth element of an array which has only 5 elements. So this causes runtime error.

Hope you are clear.

Sanyev
[ July 26, 2004: Message edited by: Sanyev Babu ]
 
Kaushal Shah
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sanyev,

Sounds good! Thanx for the explanation!

Cheers,
Kosh!
 
reply
    Bookmark Topic Watch Topic
  • New Topic