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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

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 »

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

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

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

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

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

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

Post 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.

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

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

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

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

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

Post by TwistedLizard »

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: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

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

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

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

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

Post by admin »

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: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

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

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

fortesp
Posts: 9
Joined: Mon Dec 14, 2020 8:53 am
Contact:

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

Post 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.

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

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

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

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 44 guests