Results 1 to 3 of 3
  1. #1
    gatzke is offline Member
    Join Date
    Jan 2009
    Posts
    11
    Rep Power
    0

    Default Char as an arithmetic operator

    Is it possible to use a char as an arithmetic operator?
    Is there some kind of conversion of the char that has to take place first?

    For example:

    Java Code:
    char addIt = '+';
    int a, b, c;
    
    a = 5;
    b = 3;
    
    c = a addIt b;

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Not the way you're trying to use it. It's possible to map a Character to a binary method and use that method to do the calculation you desire.

    It won't be nearly as clean looking. If you want operator overload, you'll need to learn C++.

    For example:
    Java Code:
    import java.util.HashMap;
    import java.util.Map;
    
    public class TestBinaryMethod
    {
      public static Map<Character, BinaryMethod> methodMap = new HashMap<Character, BinaryMethod>();
    
      public static void fillMap()
      {
        methodMap.put('+', new BinaryMethod()
        {
          public double execute(double d1, double d2)
          {
            return d1 + d2;
          }
        });
        methodMap.put('-', new BinaryMethod()
        {
          public double execute(double d1, double d2)
          {
            return d1 - d2;
          }
        });
      }
    
      public static void main(String[] args)
      {
        fillMap();
        System.out.println(methodMap.get('+').execute(100, 40));
        System.out.println(methodMap.get('-').execute(100, 40));
      }
    }
    
    interface BinaryMethod
    {
      public double execute(double d1, double d2);
    }
    Last edited by Fubarable; 01-27-2009 at 03:13 AM.

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    char is a primitive type that represents a two-byte Unicode character.

    Character is an object wrapper around the primitive type.

    The Character class has various means of obtaining an integer representation of Unicode characters (code points), which can then be converted back to char.

Similar Threads

  1. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 01:49 PM
  2. Arithmetic Stacks
    By unc123w in forum New To Java
    Replies: 22
    Last Post: 10-21-2008, 09:24 PM
  3. Illegal Arithmetic Operations?
    By Cruor in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 05:46 PM
  4. Replies: 0
    Last Post: 04-15-2008, 08:36 PM
  5. How to parse postfix arithmetic expressions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:36 PM

Posting Permissions

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