precedence
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Can any one please explain the mechanics of this code, the answer for this question is False, why?
1: public class Q10
2: {
3: public static void main(String[] args)
4: {
5: int i = 10;
6: int j = 10;
7: boolean b = false;
8:
9: if( b = i == j)
10: System.out.println("True");
11: else
12: System.out.println("False");
13: }
14: }
1: public class Q10
2: {
3: public static void main(String[] args)
4: {
5: int i = 10;
6: int j = 10;
7: boolean b = false;
8:
9: if( b = i == j)
10: System.out.println("True");
11: else
12: System.out.println("False");
13: }
14: }
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I get "True"...dont know where the false is coming from.
Yo can break down the if like this:
if(b=(i==j))
Ok so you see that i==j which is 10==10
This is true no problem.
Next we have b=(i==j). We know that i==j is true, so we cna insert it in to the statement.
So we have b=true.
Note this is a assignment and not a test to see if thye are the same. so what we are doing is setting b to equal true. Hence it evaluates ot true and prints true.
Hope that helps.
Faisal
Yo can break down the if like this:
if(b=(i==j))
Ok so you see that i==j which is 10==10
This is true no problem.
Next we have b=(i==j). We know that i==j is true, so we cna insert it in to the statement.
So we have b=true.
Note this is a assignment and not a test to see if thye are the same. so what we are doing is setting b to equal true. Hence it evaluates ot true and prints true.
Hope that helps.
Faisal
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
But I run in my machine. The answer is true.
And I think this is the right answer, too.
regds,
Calanthe
And I think this is the right answer, too.
regds,
Calanthe
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
shankareb,
please read the JavaRanch Naming Policy and register again.
Thank you for your cooperation
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
please read the JavaRanch Naming Policy and register again.
Thank you for your cooperation
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
| Tomorrow is the first day of the new metric calendar. Comfort me tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |











