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

Moderator: admin

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

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

Post by admin » Mon Aug 21, 2017 2:14 am

Well, you can try out a one line code to see if that compiles!

flex567
Posts: 66
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 » Fri Aug 10, 2018 11:49 am

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?

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

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

Post by admin » Fri Aug 10, 2018 8:55 pm

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.

thank you for your feedback!

flex567
Posts: 66
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 » Sat Aug 11, 2018 7:59 am

How can I see the updated explanation?

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

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

Post by admin » Sat Aug 11, 2018 8:32 am

You will see it after replacing your question bank file with the latest one from our site ( http://enthuware.com/downloads/japv8.ets ).

flex567
Posts: 66
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 » Sat Aug 11, 2018 11:57 am

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?

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

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

Post by admin » Sat Aug 11, 2018 8:48 pm

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;

flex567
Posts: 66
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 » Sun Aug 12, 2018 7:46 am

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?

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

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

Post by admin » Sun Aug 12, 2018 10:48 am

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests