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
TejWidJava
Posts: 2
Joined: Sat Sep 07, 2013 6:16 am
Contact:

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

Post 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

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

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

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

victoranica
Posts: 1
Joined: Tue Mar 31, 2015 8:20 am
Contact:

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

Post 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

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

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

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

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

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

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

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

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

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

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

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

Post by shambhavi »

any body with an answer on my previous post ? :(

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

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

Post by admin »

The meaning of bnp is explained in the same section. do control f.
If you like our products and services, please help us by posting your review here.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

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

admin
Site Admin
Posts: 10036
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: 10036
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: 10036
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: 10036
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: 10036
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: 10036
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: 10036
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 55 guests