About Question enthuware.ocajp.i.v7.2.1180 :
Moderator: admin
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
From the explanation I don't understand this
How can a conditional expression be short I thought it is boolean? About which operands is being refered here?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.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
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.
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
How can I see the updated explanation?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
from the new explanation
Why would it matter what type of conditional expresion it is?
Do you have an example of this expresion?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.
Why would it matter what type of conditional expresion it is?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
I would expect that the type of a conditional expression would depend on the condition.
What means "value is representable in type T" ? Can you provide an example?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.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
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.
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
I didn't understand this in the explanation:
Is it permitted that only one of the 2 operands is a void method?Note that binary numeric promotion performs unboxing conversion (5.1.8) and value set conversion (5.1.13).
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
You will need to go through these two sections 5.1.8 and 5.1.13.
What happened when you tried it out?Is it permitted that only one of the 2 operands is a void method?
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
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:
Which is an error that I am puzzled about.
"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
Last edited by flex567 on Thu May 09, 2019 7:23 am, edited 2 times in total.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
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".
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.
-
- Posts: 32
- Joined: Mon Dec 29, 2014 4:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
why have out1(); out2(); no body? are this two methods not abstract?Assume that out1 and out2 methods have the following signatures: public void out1(); and public void out2();
-
- Posts: 32
- Joined: Mon Dec 29, 2014 4:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1180 :
sorry I see now: its about only the methods "signatures"
Who is online
Users browsing this forum: No registered users and 46 guests