About Question enthuware.ocajp.i.v7.2.1059 :
Moderators: Site Manager, fjwalraven
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1059 :
I didn't know the return value of Math.random() can be represented like .05. Where is the specification?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1059 :
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Fri Aug 08, 2014 11:22 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1059 :
I wondered til now what the bounds were for the update clause.The third part (i.e. the update part) of the for loop does not allow every kind of statement. It allows only the following statements here: Assignment, PreIncrementExpression, PreDecrementExpression, PostIncrementExpression, PostDecrementExpression, MethodInvocation, and ClassInstanceCreationExpression.
Thank you, you've answered a nagging question for me
But where is this specified?
JLS §14.4 doesn't seem to cover it
EDIT-I'm looking now in JLS §15, Expressions to see if I can find the answer myself...
2nd EDIT-No luck in §15 but reading §2.2, Grammars has thrown light on what the grey syntax definition boxes are about
Maybe I need to take a day or two to read JLS from the start to get to grips with its layout... It's fairly terse
Am I on the right track Paul?
-
- Posts: 29
- Joined: Fri Aug 08, 2014 11:22 am
- Contact:
no luck answering my own question above
Think my original question has got a bit lost in among my own Edits/Postscripts
So just to re-ask:
JLS §14.4 doesn't appear to cover it
Thanks
So just to re-ask:
Where is this specified?The third part (i.e. the update part) of the for loop does not allow every kind of statement. It allows only the following statements here: Assignment, PreIncrementExpression, PreDecrementExpression, PostIncrementExpression, PostDecrementExpression, MethodInvocation, and ClassInstanceCreationExpression.
JLS §14.4 doesn't appear to cover it
Thanks
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1059 :
It is given in 14.14.1:
Paul.
14.8 defines StatementExpression:ForUpdate:
StatementExpressionList
StatementExpressionList:
StatementExpression
StatementExpressionList , StatementExpression
HTH,ExpressionStatement:
StatementExpression ;
StatementExpression:
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Fri Aug 08, 2014 11:22 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1059 :
Great stuff Paul
Thank you
Thank you
-
- Posts: 31
- Joined: Thu Feb 19, 2015 8:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1059 :
I do not understand, is this a right number " .05 "- What is this?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1059 :
Not sure what is your confusion but yes, .05 is a valid double. BTW, there is no primitive data type called "number" in Java.
Please see this: https://docs.oracle.com/javase/tutorial ... types.html
Please see this: https://docs.oracle.com/javase/tutorial ... types.html
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Wed Jul 15, 2015 4:05 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1059 :
Pretty tough question! I got it wrong even though I have actually read the for loop part of the JLS some time ago. I was wondering why some expressions/statements are valid in a for loop and some are not.
(I am currently banging my head against the wall for answering that "Math.random()<.05? break : continue" is valid...)
(I am currently banging my head against the wall for answering that "Math.random()<.05? break : continue" is valid...)
-
- Posts: 3
- Joined: Mon Jan 18, 2016 3:17 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1059 :
I'm not sure I understand the explanation for the following:
for(;;){ Math.random()<.05? break : continue; }
"This is an invalid use of ? : operator. Both sides of : should return the same type (excluding void)."
The code bellow works just fine, even if both sides of ":" do not return same type:
String s = "true";
boolean b = false;
for(int i = 0; i < 2; i++){
System.out.println(i==1 ? s : b);
}
Am I missing something here?
for(;;){ Math.random()<.05? break : continue; }
"This is an invalid use of ? : operator. Both sides of : should return the same type (excluding void)."
The code bellow works just fine, even if both sides of ":" do not return same type:
String s = "true";
boolean b = false;
for(int i = 0; i < 2; i++){
System.out.println(i==1 ? s : b);
}
Am I missing something here?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1059 :
You are right. The explanation is incorrect.
As per JLS section 15.25:
The explanation has been updated.
thank you for your feedback!
Paul.
As per JLS section 15.25:
So basically, the type of the value returned by both the operands is actually converted to a common type by using one of the above rules. In your example, the second last line of the above quote applies.The type of a conditional expression is determined as follows:
If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.
If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (§5.1.7) to T, then the type of the conditional expression is T.
If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.
Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:
If one of the operands is of type byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.
If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression (§15.28) of type int whose value is representable in type T, then the type of the conditional expression is T.
If one of the operands is of type T, where T is Byte, Short, or Character, and the other operand is a constant expression (§15.28) of type int whose value is representable in the type U which is the result of applying unboxing conversion to T, then the type of the conditional expression is U.
Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.
Note that binary numeric promotion performs value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).
Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2.
The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2) (§15.12.2.7).
The explanation has been updated.
thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 57 guests