Tell me the output of this code and explain
class Test
{
public static void main(String[] args)
{
boolean b = false; // lets assume it is line number 1
if(b = true) // lets assume it is line number 2
{
System.out.println("if block executes");
}
else
{
System.out.println("else block executes");
}
}
}
output of code is =>
if block executes
explanation of code:
## at line number 1 we are assignning false in a boolean variable b
## at line number 2 we are again assigning true inside variable b using
assignment operator (that is =)
## so new value of variable b is true, so result inside if statement
becomes true
## and we know in case of true condition the block of if will be
executed
class Test
{
public static void main(String[] args)
{
boolean b = false; // lets assume it is line number 1
if(b = true) // lets assume it is line number 2
{
System.out.println("if block executes");
}
else
{
System.out.println("else block executes");
}
}
}
output of code is =>
if block executes
explanation of code:
## at line number 1 we are assignning false in a boolean variable b
## at line number 2 we are again assigning true inside variable b using
assignment operator (that is =)
## so new value of variable b is true, so result inside if statement
becomes true
## and we know in case of true condition the block of if will be
executed
Very tricky.....but now you make this question very simple :)
ReplyDeleteThank you sir
good trick sir inside if condition....
ReplyDeletegood trick sir inside if condition....
ReplyDeleteThanku sir
ReplyDeleteHmm .. very tricky and important thing to be remembered..
ReplyDeleteThank you sir
Hmm .. very tricky and important thing to be noticed.
ReplyDeleteThank you sir
Awsm sir
ReplyDeleteNice tricky question sir..
ReplyDeleteAwsome sir
ReplyDeleteAwesome trick
ReplyDeletePlease enter more questions here like above.
ReplyDeleteThank u sir...