aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes = and == Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "= and ==" Watch "= and ==" New topic
Author

= and ==

Basu Patel
Ranch Hand

Joined: May 28, 2000
Posts: 60
//What is the result of executing the following fragment of code:

class _2{
public static void main(String []args){
boolean flag = false;
if (flag = true) {
System.out.println("true");
}
else {
System.out.println("false");
}
boolean flag1 = false;
if (flag1 == true) {
System.out.println("true");
}
else {
System.out.println("false");
}
}
}
what is the difference bet. = and == in the argument section of the if statement? what will the answer be.?


<BR>Contact Me<BR> <A HREF="mailto:basu_patel@usa.net" rel="nofollow">basu_patel@usa.net</A> <P>
swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
Hi,
In the first case (flag1=true) you are assinging "true" to flag1.So if condition will be always true and hence prints "TRUE".
In the second case ( flag1 == true) ,here you are comparing the value of flag1 ,which is assinged to false, so if condition will be flase and hence will print "False".
Correct if wrong
thanx


Swati Kale
SCJP
SCWCD
Siobhan Murphy
Ranch Hand

Joined: Oct 19, 2000
Posts: 72

reads as 'set a to b' or 'a becomes b', whereas

reads as 'Does a denote the same obect as b?'
Hope this helps
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: = and ==
 
Similar Threads
= Operator
equality test for boolean
can't get rid of 1st typed character
simple question
boolean