Page 1 of 1

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".

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

Posted: Thu Aug 06, 2020 6:14 am
by Dreamweaver
Assume that out1 and out2 methods have the following signatures: public void out1(); and public void out2();
why have out1(); out2(); no body? are this two methods not abstract?

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

Posted: Thu Aug 06, 2020 6:18 am
by Dreamweaver
:D sorry I see now: its about only the methods "signatures"