Tertiary Conditional: what does this evaluate to ("docRoot == null ? this.root : doc root")?

A

Andrew Hobbs

You should find it in one of your texts as the ternary operator (not
tertiary).

If not (I would be surprised if it is omitted from a basic java textbook)
look at

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************
 
D

Doug Schwartz

The ternary operator is shorthand for:

if(docRoot == null)
{
this.root;
}
else
{
doc root;
}

Your code doesn't look legit, but that's not your question.

doug
 
H

hiwa

I couldn't find anything in any of my text's.
Just the name.
someThing = docRoot == null ? this.root : doc root;
//if docRoot is null, then assign this.root to someThing
//if docRoot is not null, then assign doc.root to someThing
//'doc root' is a wrong Java syntax
 
J

Joona I Palaste

Doug Schwartz said:
The ternary operator is shorthand for:
if(docRoot == null)
{
this.root;
}
else
{
doc root;
}
Your code doesn't look legit, but that's not your question.

This gives the impression that this code:

(docRoot == null ? this.root : docRoot).someMethod();

is shorthand for:

if (docRoot == null) {
this.root;
}
else {
docRoot;
}.someMethod();

which is very much invalid Java syntax. It is really shorthand for:

if (docRoot == null) {
this.root.someMethod();
}
else {
docRoot.someMethod();
}
 
D

David Zimmerman

Joona said:
This gives the impression that this code:

(docRoot == null ? this.root : docRoot).someMethod();

is shorthand for:

if (docRoot == null) {
this.root;
}
else {
docRoot;
}.someMethod();

which is very much invalid Java syntax. It is really shorthand for:

if (docRoot == null) {
this.root.someMethod();
}
else {
docRoot.someMethod();
}

Keep in mind that the ?: ternary is an expression, not flow control.
It's used to choose from two values. You can't use it if the values are
void (ie a void method).
 
R

Rick Osborn

It's amazing. I only have exam guides left and none had it.
Only examples using it.

They also have "+=" but none explain in detail. I assume it's
shorthand for concatenation/re-assignment. But none says for sure.
Am I right?
 
T

Tony Morris

Rick Osborn said:
I couldn't find anything in any of my text's.
Just the name.

It's a String literal:
String s = ("docRoot == null ? this.root : doc root")


If you meant:
docRoot == null ? this.root : doc root

The ternary (not tertiary) operator is evaluated as follows:
(condition ? expression1 : expression2)

if(condition)
{
evaluate as expression1
}
else
{
evaluate as expression2
}

Therefore, your corrected statement is evaluated as follows:
if(docRoot == null)
{
evaluate as this.root
}
else
{
// I suspect a compile-time error will occur because of this
evaluate as doc root
}


--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
T

Thomas Weidenfeller

Rick said:
It's amazing. I only have exam guides left and none had it.
Only examples using it.

What a lame excuse. Let's face it. There is a boatload of beginner Java
information out there on the net. You could e.g. start with the
tutorials on Sun's web site. Or the language specification (on Sun's web
site, too). Or Bruce Eckel's "Thinking in Java", also for free on the net.

You could also start to ask your beginner's questions in
comp.lang.java.help - where they are on-topic.
They also have "+=" but none explain in detail. I assume it's
shorthand for concatenation/re-assignment. But none says for sure.
Am I right?

See above.

And what about reading the answers you already got? Hint: Andrew's
answer pointed you to a web site explaining that operator, too.

Get your lazy ass up, start reading and stop whining that your can't
find things in your text book.

/Thomas
 
T

Tony Morris

Get your lazy ass up, start reading and stop whining that your can't
find things in your text book.

Here here !
Blunt, completely accurate and I'd even speculate that it's the general
opinion of most.
I like it.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
J

Jon A. Cruz

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top