• 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

Strings

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The output of this program prints null in First case and empty strings in second case can somebody tell me inspite of assigning a[i++]=" " in the for loop of first case why does it still print null.
I understand that since in first case the String was constructed and not only declared hence it would be given default value of null. But after reassigning in for loop it does't change to empty string.
Thanks
Deeps
[ Jess added UBB [CODE] tags to preserve whitespace, improve readability ]
[ June 19, 2002: Message edited by: Jessica Sant ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that when you create a new array, all elements of that array are initialized to null. The problem here is to understand the sequence of the different operations. The for-loop will iterate 5 times (from 0 to 4). The update expression is only executed once the body has been executed, this means that when System.out.prinln is called a[i] has not been initialized yet. After that call, a[i++]=" " is executed and the i-th element of a is initialized. Basically, the element is printed before its initialization to " ", hence the null.
In the second case, the array is created and initialized at the same time, so there is no question there.
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am not sure but i guess u dint get my problem clearly. If ur explaination is true i agree a[0] will be null. But what abt a[1],a[2],a[3] and a[4]. Should they not be blank stings?
The o/p i am getting is
First:null
First:null
First:null
First:null
First:null
Second:
Second:
Second:
Second:
Second:
How???
[ Jess added UBB [CODE] tags ]
[ June 19, 2002: Message edited by: Jessica Sant ]
 
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepali Pate:

I am not sure but i guess u dint get my problem clearly. If ur explaination is true i agree a[0] will be null. But what abt a[1],a[2],a[3] and a[4]. Should they not be blank stings?
The o/p i am getting is
First:null
First:null
First:null
First:null
First:null
Second:
Second:
Second:
Second:
Second:
How???



I think, Val was correct.
here,
a[i++]=""
and then you are printing a[i].
Suppose,in the first iteration
you are assigning a[0] = "" and i will be incremented and but, you are prining a[1]. which is already null due to the
String a [] = new String [5];
then in next iteration, a1[] = "" and but, you are printing a[2].
HTH,
Am I correct Val??
murthy
[ June 19, 2002: Message edited by: Jessica Sant ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can interpret a FOR loop like this:

Notice the increment is done after the loop body is executed -- hence the reason why NULL is printed on the first run.
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Val, Murthy and Jessica
I got it !!
reply
    Bookmark Topic Watch Topic
  • New Topic