Tangible Software Solutions

The Most Trusted Name in Source Code Conversion

Java Equivalent to C# Operator Overloading

Most programming languages allow 'overloading' operators to implement specialized behavior for the operator when used on instance of a type. Java doesn't allow this, but the same behavior is achieved through method calls:

C#:

public class SomeType
{
   private int IntValue;

   public static int operator +(SomeType X, SomeType Y)
   {
      return X.IntValue + Y.IntValue;
   }

   public void OperatorTest()
   {
      SomeType o = null;
      int i = o + o;
   }
}

Java:

public class SomeType
{
   private int IntValue;

   public static int OpAddition(SomeType X, SomeType Y)
   {
      return X.IntValue + Y.IntValue;
   }

   public final void OperatorTest()
   {
      SomeType o = null;
      int i = SomeType.OpAddition(o, o);
   }
}

If you need to convert from C# to Java and you are depending on the results being reliable and accurate, then you will want to have C# to Java Converter, the best C# to Java converter, at your fingertips.

Copyright © 1997 - 2017 Tangible Software Solutions Inc.