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

Moderator: admin

Post Reply
Aditya553
Posts: 15
Joined: Sun Aug 06, 2017 2:37 am
Contact:

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

Post by Aditya553 » Wed Sep 13, 2017 1:57 am

Explain how this works -
i = b << b ;
s <<= b ;

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

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

Post by admin » Wed Sep 13, 2017 2:26 am

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

TwistedLizard
Posts: 58
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

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

Post by TwistedLizard » Sun Mar 11, 2018 5:39 am

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.

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

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

Post by admin » Sun Mar 11, 2018 7:03 am

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

TwistedLizard
Posts: 58
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

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

Post by TwistedLizard » Sun Mar 11, 2018 7:23 am

Thanks.

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

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

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

Post by admin » Sun Mar 11, 2018 10:24 am

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.

TwistedLizard
Posts: 58
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

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

Post by TwistedLizard » Sun Mar 11, 2018 5:39 pm

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
  }
}

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

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

Post by admin » Sun Mar 11, 2018 10:01 pm

No, I was just not sure. Good that you tried :)
Btw, you should also try &&= and not just &=.
If you like our products and services, please help us by posting your review here.

TwistedLizard
Posts: 58
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

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

Post by TwistedLizard » Mon Mar 12, 2018 6:16 am

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
  }
}

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests