This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

'if'block ??

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will the following program print?

I tried running the program, it printed "false false"
I cannot understand the if/else block i.e
if( b )
{
x = ( ch == ia[ch]);
}
else x = ( ba[ch] = b );
Can any one explain me??
Sonir
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The boolean b is automatically intialized to false. The char ch is automatically initialized to \u0000 which is 0 (Remember you can use Unicode anywhere in your code) so the if statement reads like this if (false) // skips the if section and goes to the else block. The assignment else x = ( ba[ch] = b ); will break down like this
else x = ( ba[ch] = b ); //original
else x = (ba[0] = false); // first op
else x = (false); 2nd op
So the sys print will print
System.out.println(false" "+false);
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
member variables will be given default value if they're not initiated. so
b=false
ia=0
ch='\u0000'
ba=false
so in if block, as b is false, it'll go to else statement. b(false) is first assigned to ba, then x. so both x and ba[ch] are false
 
reply
    Bookmark Topic Watch Topic
  • New Topic