About Question com.enthuware.ets.scjp.v6.2.412 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question com.enthuware.ets.scjp.v6.2.412 :

Post by ETS User »

An elaborate explanation is given for the ternary operator...can you please explain with examples.

If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.

If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.412 :

Post by admin »

1. Consider the expression:

Code: Select all

(  i == 10? null : "ten" )
Second operand is null and the third operand is a String. So the return type of this expression is String.

Code: Select all

String s = (i == 10? null : "ten");//valid
2. Consider the expression:

Code: Select all

(  i == 10? 10 : b )
, where b is declared as byte b = 20;
In this case, 10 is a constant. It is also small enough to representable by a byte. Therefore, since the third operand is a byte, the return type of the expression is byte.

Code: Select all

byte anotherb = (i == 10? null : b);//valid
But

Code: Select all

byte anotherb = (i == 500? null : b);//invalid
because 500 is too big to fit into a byte, whose range is -128 to 127.

Code: Select all

short anotherb = (i == 500? null : b);//valid
because 500 can fit into a short

HTH,
Paul

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.412 :

Post by admin »

BTW, byte anotherb = (i == 10? null : b);// will throw NullPointerException if i is equal to 10!

TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.412 :

Post by TwistedLizard »

According to the explanation:

If the second and third operands are of different reference types, then it must be possible to convert one of the types to the other type (call this latter type T) by assignment conversion (5.2); the type of the conditional expression is T. It is a compile-time error if neither type is assignment compatible with the other type.

I don't think that's true:

Code: Select all

class ConditionalTest {
  static class Animal{}
  static class Cat extends Animal{}
  static class Dog extends Animal{}
  public static void main(String[] args) {
    Animal animal = (Math.random() < 0.5) ? new Cat() : new Dog();
  }
}
yet a Cat instance can't be assigned to a Dog variable, nor the other way around.

Have I misunderstood the explanation?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.412 :

Post by admin »

This is quoted from JLS at the time the question was developed. Apparently, this was a mistake in JLS and they updated JLS later on. I see that this statement is mentioned here:
https://jcp.org/aboutJava/communityproc ... hanges.pdf

but not in official JLS anymore.

So you are right. This needs to be fixed.

thank you!
Paul.

TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.412 :

Post by TwistedLizard »

Thanks Paul.

Although this question was for scjp 6, I'm using javac 1.7.0_03
Even if I compile the code I gave with the -source 1.6 option:

>javac -source 1.6 ConditionalTest.java

I still don't get a compile error. So, for the ojcp java 6 exam, I'll assume that the behaviour of javac 1.7.xxxx is what's expected.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests