• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

surprise....boolean check

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in my below code....1>>> compiles w/o err 2>>>doesnt compile,can anybody pls help....
{
boolean b1=true;
boolean b2=true;
1>>>>>if(b1==b2)
System.out.println("hi");
2>>>>>if(true=true)
System.out.println("hi");
}
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Be careful with the the symbols you use. This example is tough to read.
The first one compiles because you use the operator ==.
This operator is used for comparison.
The second example you use the operator =.
This operator is used for assignment.
When you use an if statement you need to use a boolean, or a statement that returns a boolean.
IN the first example b1==b2 returns a boolean so it works.
The second example is just an assignment and no boolean is returned hence it won't compile.
 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sean thanx for that clarification,i got it, but lets take this one below,...//......2) doesnt compile
i assigned true to b3, //......2) as u see below,
is it not that...true=true returns true?
{
boolean b3;
if(b3=true) //..........1)
System.out.println("hi");
if(true=true) //..........2)
System.out.println("hi");
}
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
true= true doesn't return anything, it's an assignment.
if I say:
int a= 5;
Does anything get returned? of course not.
But if I said:
if(true==true)
This returns true so the if statement gets executed.
 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got it,thanQ !!
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sean Casey:
true= true doesn't return anything, it's an assignment.


I don't think that above assignment will compile because you are trying to use a reserved word (true) as a variable. In the if statements you can do assignments to a boolean in the condition i.e you can say if (b3 = true) but you cannot assign any other type i.e if you say if (b4 = 5) then it won't compile
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never said it would compile. I was only trying to explain the difference between = and ==.

[This message has been edited by Sean Casey (edited July 08, 2001).]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Sean,
i have got a similar problem. here inside if, flag=true is not supposed to return a boolean right? but when i run it i get the output true because true is assigned to flag at that time. can u please make this point clear to me. thanks.
kavita desai.
boolean flag = false;
if (flag = true) {
System.out.println("true");
}
else{
System.out.println("false");
}
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kavita,
In java, anything on the right hand side of an assignment statement is returned. If an assignment is required the return value is copied into the variable memory location. If no assignment is specified then the value is just left alone.
See the following class to see other examples of this.

Regards,
Manfred.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what Manfred is saying is that every expression in Java has a return value even assignment statements and the value of the expresion is normally the value of the left hand side.
check out the JLS section 15.26. and look at this example:

it prints 5.
So in answer to the original question:
if(b1==b2) will compile no problem
if(true=true) wont compile because your using the keyword true on the left hand side. And as Manfred explained and showed in his code, Kavita's question is answered because the assignment statement does have a value.
Hope that helps

Dave
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To elaborate on Manfred's post with an example

Lines 1, 2, and 3 will compile; line 4 will not
Lines 1 and 3 are valid comparison operations and will return a boolean.
Lines 2 and 4 are an assignment operations that will return the value of the assignment -- in Line 2, this happens to be a boolean, and so is valid in this context, but in Line 4 the value is an int, which is not valid as an argument to the if construct.
 
kavita desai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manfred, Dave and Scott, Thanks a lot. this was my first experience at javaranch saloon and found it very helpful thanx again.
kavita desai
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic