aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Operands and Variables " Watch "Operands and Variables " New topic
Author

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.


Thanks,
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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 ]

    Steve
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19720
        
      20

    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.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39409
        
      28
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Operands and Variables