About Question com.enthuware.ets.scjp.v6.2.412 :
Moderator: admin
About Question com.enthuware.ets.scjp.v6.2.412 :
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.
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.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.412 :
1. Consider the expression:
Second operand is null and the third operand is a String. So the return type of this expression is String.
2. Consider the expression: , 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.
But
because 500 is too big to fit into a byte, whose range is -128 to 127.
because 500 can fit into a short
HTH,
Paul
Code: Select all
( i == 10? null : "ten" )
Code: Select all
String s = (i == 10? null : "ten");//valid
Code: Select all
( i == 10? 10 : b )
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
Code: Select all
byte anotherb = (i == 500? null : b);//invalid
Code: Select all
short anotherb = (i == 500? null : b);//valid
HTH,
Paul
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.412 :
BTW, byte anotherb = (i == 10? null : b);// will throw NullPointerException if i is equal to 10!
-
- Posts: 57
- Joined: Sat Mar 01, 2014 1:48 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.412 :
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:
yet a Cat instance can't be assigned to a Dog variable, nor the other way around.
Have I misunderstood 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();
}
}
Have I misunderstood the explanation?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.412 :
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.
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.
-
- Posts: 57
- Joined: Sat Mar 01, 2014 1:48 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.412 :
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.
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.
Who is online
Users browsing this forum: Google [Bot] and 4 guests