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

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

Moderator: admin

Post Reply
admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Well, you can try out a one line code to see if that compiles!
If you like our products and services, please help us by posting your review here.

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

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

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

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

Post by flex567 »

How can I see the updated explanation?

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

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

Post 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 ).
If you like our products and services, please help us by posting your review here.

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

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

Post 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?

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

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

Post 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;
If you like our products and services, please help us by posting your review here.

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

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

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

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

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

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

Post 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.
Last edited by flex567 on Thu May 09, 2019 7:23 am, edited 2 times in total.

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

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

Post 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".
If you like our products and services, please help us by posting your review here.

Dreamweaver
Posts: 32
Joined: Mon Dec 29, 2014 4:14 pm
Contact:

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

Post 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?

Dreamweaver
Posts: 32
Joined: Mon Dec 29, 2014 4:14 pm
Contact:

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

Post by Dreamweaver »

:D sorry I see now: its about only the methods "signatures"

Post Reply

Who is online

Users browsing this forum: No registered users and 34 guests