About Question com.enthuware.ets.scjp.v6.2.346 :
Moderator: admin
-
- Posts: 15
- Joined: Sun Aug 06, 2017 2:37 am
- Contact:
About Question com.enthuware.ets.scjp.v6.2.346 :
Explain how this works -
i = b << b ;
s <<= b ;
i = b << b ;
s <<= b ;
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.346 :
Can you tell what exactly is that you do not understand about it?
See http://www.java-samples.com/showtutoria ... rialid=268 to know how the left shift operator works.
b<<b just shifts the bit pattern of b by a certain number. This number is the value currently held by b.
See http://www.java-samples.com/showtutoria ... rialid=268 to know how the left shift operator works.
b<<b just shifts the bit pattern of b by a certain number. This number is the value currently held by b.
If you like our products and services, please help us by posting your review here.
-
- Posts: 57
- Joined: Sat Mar 01, 2014 1:48 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.346 :
Option 2:
https://docs.oracle.com/javase/tutorial ... mmary.html
no such compound operator is listed. Could you say where is it documented?
In fact, '%=' as in
com.enthuware.ets.scjp.v6.2.165
isn't listed there either.
Looking at the list of operatorss <<= b ;
'All compound assignment operators internally do an explicit cast.'
https://docs.oracle.com/javase/tutorial ... mmary.html
no such compound operator is listed. Could you say where is it documented?
In fact, '%=' as in
com.enthuware.ets.scjp.v6.2.165
isn't listed there either.
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.346 :
Section 15.26.2 of JLS:
Btw, the page that you've referred to doesn't mention any compound operator at all. That is probably the reason why it doesn't mention <<= and %=.15.26.2. Compound Assignment Operators
A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
If you like our products and services, please help us by posting your review here.
-
- Posts: 57
- Joined: Sat Mar 01, 2014 1:48 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.346 :
Thanks.
So there's not a list as such. Looks like a compound assignment can be constructed out of any binary operator.
So there's not a list as such. Looks like a compound assignment can be constructed out of any binary operator.
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.346 :
binary arithmetic or bitwise operator for sure. But try it with boolean operators.
If you like our products and services, please help us by posting your review here.
-
- Posts: 57
- Joined: Sat Mar 01, 2014 1:48 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.346 :
Are you implying that compound assignment doesn't work with boolean operators?admin wrote:But try it with boolean operators.
Seems to work correctly, at least for &= and |= :
Code: Select all
class TestCompoundAssignment{
public static void main(String[] args){
boolean b1, b2;
b1 = true; b2 = false;
System.out.printf("b1: %b b2: %b\n", b1, b2); //true false
b1 &= b2; //ie b1 = b1 & b2
System.out.printf("b1: %b\n", b1); //false- as expected
b1 = true; b2 = false;
System.out.printf("b1: %b b2: %b\n", b1, b2); //true false
b1 |= b2; //ie b1 = b1 | b2
System.out.printf("b1: %b\n", b1); //true- as expected
}
}
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.346 :
No, I was just not sure. Good that you tried
Btw, you should also try &&= and not just &=.
Btw, you should also try &&= and not just &=.
If you like our products and services, please help us by posting your review here.
-
- Posts: 57
- Joined: Sat Mar 01, 2014 1:48 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.346 :
That's interesting. Syntax isn't allowed for 'short-circuit' boolean operators.
Code: Select all
class TestCompoundAssignment2{
public static void main(String[] args){
boolean b = true;
b = b && true; //compiles ok
//attempt compound assignment syntax
b &&= true; //compile error: illegal start of expression
}
}
-
- Posts: 9
- Joined: Mon Dec 14, 2020 8:53 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.346 :
Hi there,
"Anything bigger than an int can NEVER be assigned to an int or anything smaller than int ( byte, char, or short) without explicit cast."
Could you please help me understand the last part of this sentence? Because when i test the following code, it works without a cast:
Thanks.
"Anything bigger than an int can NEVER be assigned to an int or anything smaller than int ( byte, char, or short) without explicit cast."
Could you please help me understand the last part of this sentence? Because when i test the following code, it works without a cast:
Code: Select all
int i = 0;
short s = 0;
i = s; // no cast to int needed.
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.346 :
You are assigning a short value to an int variable. Short is smaller, not bigger than an int. So, no cast is needed in your example.
The explanation is talking about assigning a bigger value to a smaller variable. For example, an int value to a short variable. That will require a cast unless the value is a compile time constant and is small enough to fit into the target variable type.
The explanation is talking about assigning a bigger value to a smaller variable. For example, an int value to a short variable. That will require a cast unless the value is a compile time constant and is small enough to fit into the target variable type.
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 138 guests