File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Operands and Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Operands and Variables " Watch "Operands and Variables " New topic

Operands and Variables

Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
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

Joined: Jan 28, 2003
Posts: 4181

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

    Joined: Oct 27, 2005
    Posts: 20044

    Originally posted by Steve Luke:

    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.

    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
    How To Ask Questions How To Answer Questions
    Phillipe Rodrigues
    Ranch Hand

    Joined: Oct 30, 2007
    Posts: 165
    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

    Joined: Oct 13, 2005
    Posts: 43303
    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.
    subject: Operands and Variables
    It's not a secret anymore!