Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.346 :

Posted: Wed Sep 13, 2017 1:57 am
by Aditya553
Explain how this works -
i = b << b ;
s <<= b ;

Re: About Question com.enthuware.ets.scjp.v6.2.346 :

Posted: Wed Sep 13, 2017 2:26 am
by admin
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.

Re: About Question com.enthuware.ets.scjp.v6.2.346 :

Posted: Sun Mar 11, 2018 5:39 am
by TwistedLizard
Option 2:
s <<= b ;
'All compound assignment operators internally do an explicit cast.'
Looking at the list of operators

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.

Re: About Question com.enthuware.ets.scjp.v6.2.346 :

Posted: Sun Mar 11, 2018 7:03 am
by admin
Section 15.26.2 of JLS:
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.
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 %=.

Re: About Question com.enthuware.ets.scjp.v6.2.346 :

Posted: Sun Mar 11, 2018 7:23 am
by TwistedLizard
Thanks.

So there's not a list as such. Looks like a compound assignment can be constructed out of any binary operator.

Re: About Question com.enthuware.ets.scjp.v6.2.346 :

Posted: Sun Mar 11, 2018 10:24 am
by admin
binary arithmetic or bitwise operator for sure. But try it with boolean operators.

Re: About Question com.enthuware.ets.scjp.v6.2.346 :

Posted: Sun Mar 11, 2018 5:39 pm
by TwistedLizard
admin wrote:But try it with boolean operators.
Are you implying that compound assignment doesn't work 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
  }
}

Re: About Question com.enthuware.ets.scjp.v6.2.346 :

Posted: Sun Mar 11, 2018 10:01 pm
by admin
No, I was just not sure. Good that you tried :)
Btw, you should also try &&= and not just &=.

Re: About Question com.enthuware.ets.scjp.v6.2.346 :

Posted: Mon Mar 12, 2018 6:16 am
by TwistedLizard
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
  }
}

Re: About Question com.enthuware.ets.scjp.v6.2.346 :

Posted: Tue Feb 23, 2021 9:53 am
by fortesp
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:

Code: Select all

        int i = 0;
        short s = 0;
        i = s; // no cast to int needed.
Thanks.

Re: About Question com.enthuware.ets.scjp.v6.2.346 :

Posted: Tue Feb 23, 2021 11:13 am
by admin
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.