File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q on Operators in Dan Chisholm's site Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q on Operators in Dan Chisholm Watch "Q on Operators in Dan Chisholm New topic
Author

Q on Operators in Dan Chisholm's site

sriRam gaddaM
Greenhorn

Joined: Dec 17, 2004
Posts: 7
ppl,

this is a question on operators:

class EBH202 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}}


What will be the output of the program:
the answer is given as true,false, false.

but before applying ||,&& the assignment expressions
in () shud be executed. what do ya say? then the output
should be true, true, true. But, i executed the program
it displays true, false, false.


can someone give me the logical explanation for this?


Sriram.
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Hi Sriram,

your name 'IGNORAMUS' does not confirm with the name policy here at JavaRanch.

Your boolean a, b and c are all false by default. Since you use the short circuit logical operator || only the left side is evaluated. So a = true, b and c remain false. Whenever the left side is true the right will not be evaluated. That is the speciality of the short circuit logical operator ||.

Regards,
Darya
[ February 18, 2005: Message edited by: Darya Akbari ]

SCJP, SCJD, SCWCD, SCBCD
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Follow the link give below, the same question has been dicsussed before
here
sriRam gaddaM
Greenhorn

Joined: Dec 17, 2004
Posts: 7
Hi,


i have got a small explanation for it. can u guys comment on it.
&&, ||, ?: are always evaluated from left to right irrespective of parentheses. but if you take the case of (a=2) * (b=3) * (c=4)
a,b,c will have 2,3,4. ain't it?

But, i got one more doutb here. && has more precedence than || so, it shud
be evaluated first before ||. But, it is not happening here?


P.S: Darya, Didn't get when u said my Uname doesn't conform with
the naming policy.
can u throw some light on that? (haven't read the policy in
detail... ))


Sriram.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
&& and || are Short Circuit operators.
Did u go thru the link i mentioned in my previous reply. Its clearly mentioned abt the usage of these Short Circuit operators.
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Hi Sriram,

there is somewhere a link on JavaRanch about name policy let me look for it...

Here it is: name policy
sriRam gaddaM
Greenhorn

Joined: Dec 17, 2004
Posts: 7
hi guys,


i think my name is fictious but doesn't fall into any of the categories mentioned in the policy nor is offending. moreover, i specify my name whenever i make a post. wud request the forum mods to let me use it.

Sriram.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by IGNORAMUS:
hi guys,


i think my name is fictious but doesn't fall into any of the categories mentioned in the policy nor is offending. moreover, i specify my name whenever i make a post. wud request the forum mods to let me use it.

Sriram.


Guess again. We require display names to be two words: your first name, a space, then your last name. Fictitious names are not allowed. You can edit your display name here. Don't take too long - accounts with invalid display names get deleted.

thanks,
Dave.
Moderator.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
wud request the forum mods to let me use it.


I don't know who wud is, but nope you cannot use that displayed name.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Q on Operators in Dan Chisholm's site
 
Similar Threads
Explain please
operator precedence
Assignment Q
Operators Precedance: Output of the Program
Output of this snippet.