wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 'if'block ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " Watch " New topic
Author

'if'block ??

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
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
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
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);
patrick tang
Ranch Hand

Joined: Dec 16, 2001
Posts: 44
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
 
jQuery in Action, 2nd edition
 
subject: 'if'block ??
 
Similar Threads
can default value of char be used as an index of array?
Array element
doubt on char default value...
What is the Initial value of char literal?
Char default value - Enthuware question