Results 1 to 6 of 6
  1. #1
    TheGame is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Exclamation What Type is a Binary Operator?

    hi, i want to know what "type" binary operators like + - / * == != <= > >= < | &
    are...?

    because i have an operator like these in a string, and i need to know what type they are to convert from string to binary operator..

    i also need to initialize an instance variable to one of these operators, so i need to know what type they are..

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    They don't have a type in Java. Variables and expressions have type. (by extension the class that was used in the creation expression of an object allows us to speak of the type of an object.)

    [Edit] so, to make it explicit: you cannot convert from string to a nonexistent binary operator type, nor can this nonexistent type be used in a creation expression.

    So what are you really trying to do? Because very likely there is a straightfoward and possible way to do that.
    Last edited by pbrockway2; 10-13-2010 at 04:53 AM.

  3. #3
    TheGame is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    ok, but how am i supposed to store the operator into an instance variable? i need to do that for an assignment..

    or how do i know which operator to use for example:
    if i have a class that performs operations on two variables, and i pass in the string representation of the operator, how do i use that string representation to perform the operation?
    i don't think i'm supposed to have a huge switch statement for my assignment..

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    if i have a class that performs operations on two variables, and i pass in the string representation of the operator, how do i use that string representation to perform the operation?
    i don't think i'm supposed to have a huge switch statement for my assignment..

    That's sounding good.

    One method would use enums.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Another method might be to have an Op class: or maybe a bunch of them BinaryOp, LogicalOp, UnaryOp etc. This class would define an appropriate apply() method.

    You create some instances of the Op class with the right sort of apply(). For instance you might have:

    Java Code:
    Op add = new Op() {
        public int add(int left, int right) {
            return left + right;
        }
    }

    Then you make a map whose keys are characters (or more generally lexical items which is a term broad enough to encompass "+" but also "!=") and whose values are the Op instances. You have avoided a huge switch by replacing it with a map.

  6. #6
    TheGame is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    is there any other way i can do this? i should say that this is just a very small part of my assignment to create an interpreter for a compiler...

    i'm trying to code the BOP bytecode class that pop top 2 levels of the runtime stack and perform the indicated operation on them..

    the bytecode looks like bop <binary op>
    and i pass the <binary op> into the init() method of the BopCode class as a string..so i don't know how to use this string representation..

    so i'm not sure if i'm supposed to be using stuff like enums..it seems like there's a simpler way to do it..

Similar Threads

  1. bitwise not ~ operator
    By kezkez in forum New To Java
    Replies: 2
    Last Post: 02-10-2010, 07:49 AM
  2. How save Image BMP Binary type?
    By shakall in forum SWT / JFace
    Replies: 3
    Last Post: 12-22-2009, 02:50 PM
  3. Increment Operator Example
    By abimaran in forum New To Java
    Replies: 10
    Last Post: 11-03-2009, 05:45 PM
  4. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 07:24 PM
  5. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •