• 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

why is it so??

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i came across these 2 questions in a mock test.the first one is:
boolean flag = false;
if (flag = true) {
System.out.println("true");
} else {
System.out.println("false");
}
the answer to this is :true is printed .
the 2nd one is:
Which of the following are acceptable to the Java compiler:
A:if (2 == 3) System.out.println("Hi");
B:if (2 = 3) System.out.println("Hi");
C:if (true) System.out.println("Hi");
D:if (2 != 3) System.out.println("Hi");
E:if (aString.equals("hello")) System.out.println("Hi");
Select all correct answers.
The answer is A,C,D,E
if (flag=true)can be accepted by the compiler as in 1st question,why not if(2=3) is acceptable.can anyone tell me why is this so please.


 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"If (flag=true)can be accepted by the compiler as in 1st question,why not if(2=3) is acceptable.can anyone tell me why is this so please."
Because the only primitive value allowed inside the "()" is boolean.
The result of ( i=j ) in
int i=2;
int j=3;
if (i=j){}
is an int.
but the result of
flag=true
is boolean so it's OK.

[This message has been edited by Tony Alicea (edited February 09, 2000).]
reply
    Bookmark Topic Watch Topic
  • New Topic