Having trouble understanding code.

edited October 2016 in Questions about Code

@GoToLoop

can anyone help me understand why static and short where used in the local variables in this code?

http://studio.sketchpad.cc/sp/pad/view/ro.9LZizxKsIAY4F/latest?

Tagged:

Answers

  • Are you asking what static and short are?

    short is, like int, one of the Java Primitive Datatypes. It is half an int. If you are storing some small numbers, you could store them in shorts to save a little bit of space:

    short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

    static, declares a variable to be an unchanging constant when used with final.

    In a short sketch this careful specification of small, unchanging variables might be about good style rather than functionality. If you try clone-modifying @GoToLoop 's sketch and replace final static short with int... does anything change?

  • edited October 2016

    static, declares a variable to be an unchanging constant when used with final.

    Actually a primitive or String final field variable doesn't need static in order to reach compile-time constant! L-)

    Rather, static is used to prohibit fields to be created more than once when its class is instantiated. :-B

  • soooooo, im still a wee bit confused on what static and final mean. I am sorry im just pretty new to programming and I havent learned these things in my class yet (the language im using im pretty sure is processing). ( I read your link @jeremydouglass ) The only thing i understand from this is that short CAN be replaced with int right? because short is half of an int (which is pretty cool too). Sorry guys thank you both so much for the help.

  • edited October 2016

    The language I'm using, I'm pretty sure, is Processing.

    Actually it is a long stretch to call Processing a programming language! @-)
    It's rather a framework + PDE (its IDE) on top of the Java language! ~O)
    And it's not bound to Java only! It's available for JS, Python, Android, etc. \m/

    I'm still a wee bit confused on what static and final mean.

    • final -> Such variable can only use the assignment operator = (and its "siblings") once only!
    • static -> Such field is created once only, no matter how many times its class is instantiated!
  • Im still kind of confused with final. So, then why did you use it the way you did in the jumping block example? and im still confused with static omg im so sorry again im really new to all of this .-.

  • edited October 2016

    Both final & static aren't necessary for any program to function.
    You can keep or remove them. Won't make any actual difference! :-j

  • hehehe ok thanks man ^_^"

Sign In or Register to comment.