• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Operands and Variables

 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between an operand and a variable?

I am not able to understand below:
Variable increment (++) and decrement (--) operators come in two flavors: prefix and postfix.These unary operators have the side effect of changing the value of the arithmetic operand, which must evaluate to a variable.
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An operand is any value that is part of an operation. Examples of valid operands:
  • 3 + 4 3 and 4 are int constant operands
  • getIndex() * getModifier() getIndex() and getModifier() are method operands (as long as the methods return a value)
  • total / count total and count are variable operands
  • 3 + getIndex() * (total / count) And of course you can mix and match them


  • What the message "changing the value of the arithmetic operand, which must evaluate to a variable" means is that these are not possible:
  • 3++ Operand 3 is an int constant, not a variable
  • --getIndex() Operand getIndex() is a method not a variable

  • [ August 17, 2008: Message edited by: Steve Luke ]
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20372
    44
    Chrome Eclipse IDE Java Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Steve Luke:
    --getIndex()

    Something that can be possible in C++ with references return types, but let's not get that far into that. Java doesn't allow it, that's all you need to know.

    Array elements can be considered as variables too, so array[i]++ is allowed.
     
    Phillipe Rodrigues
    Ranch Hand
    Posts: 165
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Is it like the below:

    short a =9; ....(1)

    a++; ....(2)

    System.out.println(a) ....(3)


    At(1) a acts as a variable and at (2),(3) a acts as an operand

    If no,please explain in terms of an example
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47229
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not quite. An operand is something an operator acts on.

    At 1 a is only a variable, similarly at 3, because there is no operator.
    At 2 it is an operand of the ++ operator, and is still a variable.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic