Java operator overload

Unused operators make my code unclear.

(aNumber / aNother) * count 

better than

 aNumber.divideBy(aNother).times(count) 

6 months after I did not write a single comment, I had to write a comment on the simple operation above. I usually reorganize until I need comments. And it made me realize that reading and perceiving mathematical symbols and numbers is easier than their written forms.

for instance

 TWENTY_THOUSAND_THIRTEEN.plus(FORTY_TWO.times(TWO_HUNDERED_SIXTY_ONE)) 

more obscure than

 20013 + 42*261 

Do you know a way to get rid of obscurity rather than using operator overloading in Java?

Update: I did not think that my exaggeration in the comments would cause me such a problem. I admit that I needed to write a comment a couple of times in 6 months. But no more than 10 lines. Sorry for that.

Update 2: Another example:

 budget.plus(bonusCoefficient.times(points)) 

more obscure than

 budget + bonusCoefficient * points 

I need to stop and think about the first, at first glance, it looks like a mess of words, on the other hand, I get the sense to first look for the second, it is very clear and tidy. I know that this cannot be achieved in Java, but I wanted to hear some ideas about my alternatives.

+4
source share
10 answers

In Java, just bypass it and use method names like BigInteger as the standard so you get used to them.

Or take a look at Scala.

At least what I do.

+1
source

I had some time ago a similar requirement for using BigInteger. It depends on what kind of sacrifice you want to perform:

For test code (without performance requirements), I created a parser:

 Parser.execute("20031 + 42*261"); 

For production code, I tried using a smart builder:

 ExpressionBuilder e = new ExpressionBuilder(); e.add(20031,e.mul(42,261)).solve(); 

But at the end of the day, people usually get used to the dumb code and even prefer it: you do not have operator overloading in jave, so live with it and not try to create a “smart” abstraction.

 BigInteger a = new BigInteger(20031); BigInteger b = new BigInteger(42); BigInteger a = new BigInteger(261); BigInteger res = a.plus(b.times(c)); 

In any case, check everything that works with your team.

EDIT: I did not specifically comment on your example. But by calling 42, FORTY_TWO does not make the code very clear, regardless of your approach. Choosing the right name for a constant is important, making the code clearer:

 BigInteger dailyOperatingCost = OFFICE_RENTAL.plus( CONTRACTOR_NUMBER.times(CONTRACTOR_RATE); 
+3
source

I think we can all be very happy that operator overloading is not supported at all in java.

 Frame myFrame = new Frame() + myButton + new JList() / new Separator() - 50; 

Who wants to support such a thing?

+2
source

You can use java-oo plugin for Java compiler.

+2
source

I like vdr idea about language. But you can skip the parsing and build a really simple eval. Here the stack is based eval. Put it together in 15 minutes and far from perfect, but you get this idea.

 import java.util.*; import java.math.BigInteger; public class BigEval { private static abstract class Op{ abstract void apply(LinkedList<BigInteger> stack); } private static Map<Character, Op> charToOp = new HashMap<Character, Op>(); static { Op plus = new Op() { void apply(LinkedList<BigInteger> stack) { stack.push(stack.pop().add(stack.pop())); } }; Op mult = new Op() { void apply(LinkedList<BigInteger> stack) { stack.push(stack.pop().multiply(stack.pop())); } }; Op dup = new Op() { void apply(LinkedList<BigInteger> stack) { stack.push(stack.peek()); } }; charToOp.put('+', plus); charToOp.put('*', mult); charToOp.put('d', dup); } public static BigInteger eval(Object ... expression){ return eval(new LinkedList(Arrays.asList(expression)), new LinkedList()); } private static BigInteger eval(LinkedList expression, LinkedList<BigInteger> stack){ while (expression.size()>0){ Object next = expression.pop(); if (next instanceof BigInteger){ stack.push((BigInteger)next); } else if (next instanceof Number) { stack.push(BigInteger.valueOf(((Number)next).longValue())); } else { charToOp.get(next).apply(stack); } } return stack.pop(); } public static void main(String[] args) { System.out.println( BigEval.eval(3, 4, '+', 5, '*') ); } } // end of class 
+1
source

You cannot overload operators in Java, and there is no way.

You can use the Java-like alternative JVM language. Groovy is one that is probably closer to Java itself than to other JVM languages. See Operator Overloading in Groovy .

0
source

In Java, there is no way to overload a statement. Why don't you switch to C # ?! =)

Or try writing shorter named methods in Java:

 aNumber.div(aNother).mul(count) 
0
source

If you are simply dealing with numeric values, standard wrapper classes (Integer, Double, etc.) will automatically be decompressed into their primitive types when used in expressions. The following is quite true:

  Integer five = new Integer(5); Integer ten = new Integer(10); System.out.println(five * ten); System.out.println(ten / five); 
0
source

The easiest way is probably to assign subexpressions to a variable with meaningful names.

  discriminant = b.times(b).plus(four.times(a).times(c)); 

You might also want to calculate bsquared and ac4 (fourac?).

These are functions as meaningful comments plus it holds the length of your source lines.

0
source

I was like you, now I believe that class names are convenient documentation to make self-documenting code. You will have to work in your own way, based on the difficulties associated with comment markers, since this is established in practice by Java. As for where your intentions are sought, what ends is a kind of cloud. Now, if I were somewhere close to the information that needs to be protected, the absolute last thing I want is to transfer customer data to the cloud with a little narrow code. I am expanding everything, working from a postprocessor in C and working in natural calls to something in Java, which I really intend to protect.

What I see in your original post, but not in order to take sides, is where you can invoke a fairly massive effort of code:

 doIt(); 

What a sorrow to be damned, not only asks for the code and does it, but it certainly won’t be anywhere in massive parallel systems that run critical code, unless you wrote both the compiler and the security domain. I am not and do not intend to be, even a little fascinating - trust is what you read in the news at 6 p.m. and love your competitors who want to take your lunch from you. There are dozens of languages ​​that do what you ask, why do you ask about it in the Java discussion?

Maybe it's time to step into linguistics that does this ==, there is no shortage of api and its proponents who do it. The code should be self-documenting; for Java, this is the name of the class as what it calls.

0
source

All Articles