• Post Reply Bookmark Topic Watch Topic
  • New Topic

"<<" operator and other q's  RSS feed

 
Kari Apple
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While learning how to implement the class DynamicArrayStack, I've run into some operators and syntax I'm unfamiliar with!


q1) why is capacity protected? I understand private, but haven't seen protected
q2)why is MINCAPACITY capitalized?
q3) I read that << is the bitwise shifter, don't see how it works at 1<<15....
q4) what does this(CAPACITY) mean in the constructor?
q5) here is << again, [length<<1] which I think means length*2?

Thanks for your help!!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Kari Apple wrote:q1) why is capacity protected? I understand private, but haven't seen protected

Because the person who designed this class maybe thought that if someone would make a subclass, this field should be accessible by subclasses. See Controlling Access to Members of a Class in Oracle's Java Tutorials for an explanation of what it means exactly.

It might also be that the programmer did not properly think about it. In my opinion, member variables should always be private unless there's a really good reason to make them more accessible.

Kari Apple wrote:q2)why is MINCAPACITY capitalized?

Probably because it is meant to be a constant, and all caps is a standard convention for naming constants. The all caps don't mean anything special, it's just a matter of style. The programmer however forgot to make it final, so that it's not actually a constant. It should have been public static final int.

Kari Apple wrote:q3) I read that << is the bitwise shifter, don't see how it works at 1<<15....

See Bitwise and Bit Shift Operators in Oracle's Java Tutorials. 1 << 15 means you take the number 1 and shift it up 15 times, which is the same as multiplying it by 2^15. So 1 << 15 == 32768.

Kari Apple wrote:q4) what does this(CAPACITY) mean in the constructor?

It calls the other constructor, the one that takes an int as an argument. I don't see the other constructor in your code, and there are also { and } missing around line 9. So the code you posted is incorrect and incomplete.

Kari Apple wrote:q5) here is << again, [length<<1] which I think means length*2?

Bit shifting one to the left means indeed multiplying by two.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why is capacity protected? Good question. It looks like a mistake to me, unless there is other information we don't know about. Making MIN_CAPACITY public looks like another mistake.
Yes, length << 1 (there should be spaces in that) means the same as length * 2 in 32‑bit two's complement arithmetic.
There are some inelegant features about that code, for example calling the size() method instead of simply writing stackRep.length. I presume you didn't write it yourself; where did you find that code?

You can read more about bitwise operators in this old thread. That should include <<.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!