Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Sat Sep 07, 2013 6:57 am
by TejWidJava
Ques:The following code snippet will not compile:

Code: Select all

 int i = 10; System.out.println( i<20 ? out1() : out2() );  Assume that out1 and out2 have method signature: public void out1(); and public void out2();
Intially i thought the reason for compilation failure is return type of methods void and there is nothing to print by the SOP.But explanation startles me.
i do not understand the explanation.What actually the examiner trying to say with this question?

Plz help me out of this

Thanks in advance
Tej

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Sat Sep 07, 2013 7:11 am
by admin
Even before the problem with SOP, the usage of ? operator is wrong because it is not permitted for the second and the third operand expression to be an invocation of a void method.


HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Tue Mar 31, 2015 8:23 am
by victoranica
Hello,

I did answer correctly do this question but I do not fully understand the explanation. For instance :
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 tried to modify the two methods as follows :

public int out1() {
return 3;
}

public String out() {
return "A String";
}

The compiler does not complain.

Regards,
Victor

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Tue Mar 31, 2015 8:54 am
by admin
That is because you are not assigning the value of the expression to any other variable. System.out.println requires just an Object and return value of both the sides can be passed to the println method. So it works. The following will not work -

Integer k = i<20 ? out1() : out2();

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Sat Aug 19, 2017 1:46 pm
by shambhavi
It is a compile-time error if neither type is assignment compatible with the other type.

if the return types of the methods out1 and out2 were, String and Integer respectively (with the appropriate return values defined), then as per the bold statement , it should show a compiler error right ! but it doesn't ! why ? :shock:

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Sat Aug 19, 2017 8:48 pm
by admin
This is because the type of the conditional operator when operands 2 and 2 are objects is Object. Specification defines it clearly in Section 15.25, Table 15.25-E. The compiler, therefore, has no choice but to accept the assignment.

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Sun Aug 20, 2017 4:52 am
by shambhavi
hmmm... ok. its a quite big table to remember. :o

I mean many combinations in the table are not something that one would easily predict :
Byte and Short - short

But Long and Short is bnp(Short,Long) - what does this exactly mean ?

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Sun Aug 20, 2017 12:07 pm
by shambhavi
any body with an answer on my previous post ? :(

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Sun Aug 20, 2017 2:50 pm
by admin
The meaning of bnp is explained in the same section. do control f.

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Mon Aug 21, 2017 2:01 am
by shambhavi
i know bnp is binary numeric promotion . :)

what i meant was, for the case of Byte and Short , the type of the conditional expression is stated explicitly as - short

But for Long and Short ,when they say binary numeric promotion of(Short,Long), could it be any of long, float or a higher type ?

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Mon Aug 21, 2017 2:14 am
by admin
Well, you can try out a one line code to see if that compiles!

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Fri Aug 10, 2018 11:49 am
by flex567
From the explanation I don't understand this
If one of the operands is of type byte and the other is of type short, then the type of the conditional expression is short.
How can a conditional expression be short I thought it is boolean? About which operands is being refered here?

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Fri Aug 10, 2018 8:55 pm
by admin
The first line of the explanation is, "Note that it is not permitted for either the second or the third operand expression of the ? operator to be an invocation of a void method."

So, the explanation is talking about the expression built using the conditional operator ?:. This expression is a conditional expression, whose type is determined by the second and the third operands to this operator.

The explanation has been updated to make it more clear.

You may go through section "15.25 Conditional Operator ? :" of JLS for more details.

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Sat Aug 11, 2018 7:59 am
by flex567
How can I see the updated explanation?

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Sat Aug 11, 2018 8:32 am
by admin
You will see it after replacing your question bank file with the latest one from our site ( http://enthuware.com/downloads/japv8.ets ).

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Sat Aug 11, 2018 11:57 am
by flex567
from the new explanation
If one of the operands is of type byte and the other is of type short, then the type of the conditional expression is short.
Do you have an example of this expresion?
Why would it matter what type of conditional expresion it is?

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Sat Aug 11, 2018 8:48 pm
by admin
Type of a conditional expression (or of any expression, for that matter) matters when you try to assign its value to a variable. For example, the following trivial code will fail to compile because the type of the expression is short:

Code: Select all

      byte b = 10;
      short s = 30;
      b =  args.length == 0? b : s;

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Sun Aug 12, 2018 7:46 am
by flex567
I would expect that the type of a conditional expression would depend on the condition.
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.
What means "value is representable in type T" ? Can you provide an example?

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Sun Aug 12, 2018 10:48 am
by admin
It means the value should fit in the type of the variable. For example, if you have final int i = 100; then i is an int but it is a constant and its value is small enough to fit into a byte. so, byte b = i; will compile.

You can read about it in detail in "3.3.3 Assigning values to variables" of OCAJP 8 Fundamentals.

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Thu May 09, 2019 6:14 am
by flex567
I didn't understand this in the explanation:
Note that binary numeric promotion performs unboxing conversion (5.1.8) and value set conversion (5.1.13).
Is it permitted that only one of the 2 operands is a void method?

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Thu May 09, 2019 7:01 am
by admin
flex567 wrote:
Thu May 09, 2019 6:14 am
I didn't understand this in the explanation:
Note that binary numeric promotion performs unboxing conversion (5.1.8) and value set conversion (5.1.13).
You will need to go through these two sections 5.1.8 and 5.1.13.
Is it permitted that only one of the 2 operands is a void method?
What happened when you tried it out?
The first line of the explanation makes it very clear though, "Note that it is not permitted for the second and the third operand of the ?: operator to be an invocation of a void method.".
Logically also, if you understand why one of the second or third operand cannot be void, then you will know whether both of them can be void or not.

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Thu May 09, 2019 7:10 am
by flex567
Logically neither can be void but I would expect
"Note that it is not permitted for the second or the third operand or both of the ?: operator to be an invocation of a void method."
not
"Note that it is not permitted for the second and the third operand of the ?: operator to be an invocation of a void method."

But I am not a native speaker so I am not sure.

When I tried it out I got:

Code: Select all

Test.java:8: error: cannot find symbol
		System.out.println( i<20 ? out() : 8 );
		                           ^
  symbol:   method out()
  location: class Test
1 error
Which is an error that I am puzzled about.

Re: About Question enthuware.ocajp.i.v7.2.1180 :

Posted: Thu May 09, 2019 7:18 am
by admin
Your result implies that neither can be void.
The reason why it cannot be void is that ?: creates an expression. i.e. you must be able to assign it to some variable. Therefore, it must have a value. If you use a void method in its operand, there cannot be a value of that expression. Hence, it cannot be allowed to have void as second and/or third operand.

This is explained in detail in Section 6.2 "Create ternary constructs".