The other mathematical shorthand operators which Java provides are the += (a = a + number), *=, and so on.

The complete list of operators can be obtained from here.

Answer from npinti on Stack Overflow
🌐
Jenkov
jenkov.com › tutorials › java › math-operators-and-math-class.html
Java Math Operators and Math Class
The - operator will replace the two values with the difference between the two values, just like with the + operator. You can create longer chains of subtractions and thus subtract more values from each other. Here is an example of subtracting two values from another in a single Java statement: ... This works for variables too. Remember, minus minus equals plus, just like in normal math.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › op1.html
Assignment, Arithmetic, and Unary Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
The Java programming language provides operators that perform addition, subtraction, multiplication, and division. There's a good chance you'll recognize them by their counterparts in basic mathematics.
Discussions

operators - Java math operations shorthand? - Stack Overflow
I've been searching but no luck. I want to know if there is a way to perform shorthand operations with the number 1. I know how to do it with addition and subtraction: /* * Addition: variableName+... More on stackoverflow.com
🌐 stackoverflow.com
Is it possible to pass arithmetic operators to a method in java? - Stack Overflow
Use a functional language for JVM to implement this part of your code (clojure, scala et el), wrap lambda functions around math operators and pass those functions as parameters · Get an expression evaluator for Java like http://www.singularsys.com/jep/ (and there must be many free alternatives ... More on stackoverflow.com
🌐 stackoverflow.com
Using math operator + in system.out.println
The concatenate operator has precedence over the add operator so it will concatenate first before it adds, but then the second + also becomes a concatenate operator and the output is 22. Add parentheses around (2+2) to fix this. More on reddit.com
🌐 r/learnjava
11
34
March 10, 2020
What are Unary, Binary, and Ternary operators in Java?
Unary operator: an operator that has 1 argument Binary operator: an operator that has 2 arguments Ternary operator: an operator that has 3 arguments There's only one ternary operator that I'm aware of that looks like int count = obj == null ? 0 : obj.getCount(); Unary operators are like a negative sign, the ! operator, ++, --. Binary operators are like most math operators (add, subtract, greater than, less than, etc). Note that - is both a unary AND binary operator, and Java has to figure out which is which based on context. More on reddit.com
🌐 r/learnprogramming
4
2
September 17, 2020
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › nutsandbolts › arithmetic.html
Arithmetic Operators
The Java programming language supports various arithmetic operators for all floating-point and integer numbers. These operators are + (addition), - (subtraction), * (multiplication), / (division), and % (modulo).
🌐
Tutorialspoint
tutorialspoint.com › java › java_basic_operators.htm
Java Operators
Java operators are the symbols that are used to perform various operations on variables and values. By using these operators, we can perform operations like addition, subtraction, checking less than or greater than, etc.
🌐
DataCamp
datacamp.com › doc › java › java-operators
Java Operators
Java operators are special symbols that perform operations on variables and values. They are used to manipulate data and variables in expressions. Java provides a wide array of operators categorized into several types based on their functionality. ... Arithmetic operators are used to perform basic mathematical operations...
🌐
iO Flood
ioflood.com › blog › java-operator
Using Java Operators: From Basics to Advanced
February 29, 2024 - Java provides several basic arithmetic operators that allow us to perform simple mathematical operations.
Find elsewhere
Top answer
1 of 9
49

No, you can't do that in Java. The compiler needs to know what your operator is doing. What you could do instead is an enum:

public enum Operator
{
    ADDITION("+") {
        @Override public double apply(double x1, double x2) {
            return x1 + x2;
        }
    },
    SUBTRACTION("-") {
        @Override public double apply(double x1, double x2) {
            return x1 - x2;
        }
    };
    // You'd include other operators too...

    private final String text;

    private Operator(String text) {
        this.text = text;
    }

    // Yes, enums *can* have abstract methods. This code compiles...
    public abstract double apply(double x1, double x2);

    @Override public String toString() {
        return text;
    }
}

You can then write a method like this:

public String calculate(Operator op, double x1, double x2)
{
    return String.valueOf(op.apply(x1, x2));
}

And call it like this:

String foo = calculate(Operator.ADDITION, 3.5, 2);
// Or just
String bar = String.valueOf(Operator.ADDITION.apply(3.5, 2));
2 of 9
10

Method arguments in Java must be expressions. An operator by itself is not an expression. This is not possible in Java.

You can, of course, pass objects (maybe enum constants) that represents those operators, and act accordingly, but you can't pass the operators themselves as parameters.


Additional tips

Since you're just starting Java, it's best to ingrain these informations early on to ease your future development.

  • Method names starts with lowercase: calculate instead of Calculate
  • Variable names starts with lowercase: operator instead of Operator
  • Double is a reference type, the box for primitive type double.
    • Effective Java 2nd Edition, Item 49: Prefer primitive types to boxed primitives
  • Don't return "error...". Instead, throw new IllegalArgumentException("Invalid operator");

See also

  • Java Language Guide/Autoboxing
  • Java Lessons/Exceptions
  • Coding Conventions/Naming
    • Unfortunate typo in this document: How is this statement making sense? (Sun’s naming convention for Java variables)
🌐
Scaler
scaler.com › topics › java › arithmetic-operators-in-java
Java Arithmetic Operators - Scaler Topics
February 26, 2024 - Arithmetic operators in Java perform mathematical calculations like addition, subtraction, multiplication, division, and modulus.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-arithmetic-operators-with-examples
Java Arithmetic Operators with Examples - GeeksforGeeks
July 12, 2025 - This program demonstrates how to implement basic arithmetic operations using user input in Java. The Scanner class makes it easy to read user input from the console, and the basic arithmetic operations are performed using standard mathematical operators in Java.
🌐
ScholarHat
scholarhat.com › home
Arithmetic operators in Java
September 6, 2025 - Arithmetic operators are symbols or characters that perform mathematical operations on numeric operands. The primary arithmetic operators in Java include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
🌐
Sanfoundry
sanfoundry.com › java-program-illustrate-use-arithmetic-operators
Arithmetic Operators in Java - Sanfoundry
March 9, 2023 - This Java program explains various arithmetic operators in Java with examples, focusing on addition, subtraction, multiplication, and division operators.
🌐
Coderanch
coderanch.com › t › 568212 › java › arithmetic-operations-operator-stored-variables
doing arithmetic operations when the operator are stored as variables (Beginning Java forum at Coderanch)
February 22, 2012 - You have two choices: 1. Write some code that executes the appropriate code path based on the operator. That could be a series if if (oper.equals(...)) { ... } else if ... steps. Or it could be done using the Command Pattern, or it could be done using Java enums with each operator's enum value implementing the operation differently.
🌐
PW Skills
pwskills.com › blog › java developer › arithmetic operators in java with syntax
Arithmetic Operators In Java With Syntax
Ans. Arithmetic operators are vital in Java programming. They perform math operations and define expressions and formulas in programs. java has addition, subtraction, multiplication, division, and modulus operators.
Published   December 23, 2025
🌐
Reddit
reddit.com › r/learnjava › using math operator + in system.out.println
r/learnjava on Reddit: Using math operator + in system.out.println
March 10, 2020 -

Following calculates correctly:

System.out.println(2+2);    //4

The following will calculate correctly too:

System.out.println("The result is " + 2*2 ); //The result is 4

But then why would this not calculate?

System.out.println("The result is " + 2+2 );    //The result is 22

How can I calculate 2+2 directly and display along with the string to get the output The result is 4?

🌐
Pega
community.pega.com › sites › pdn.pega.com › files › help_v718 › definitions › m › mathematicaloperator.htm
mathematical operators
PRPC supports 20 operators in expressions. Most correspond directly to Java operators and Java language processing:
🌐
W3Schools
w3schools.com › Java › java_operators_arithmetic.asp
Java Arithmetic Operators
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... Arithmetic operators are used to perform common mathematical operations.
🌐
Princeton CS
introcs.cs.princeton.edu › java › 11precedence
Appendix A: Operator Precedence in Java
When an expression has two operators with the same precedence, the operators and operands are grouped according to their associativity. For example 72 / 2 / 3 is treated as (72 / 2) / 3 since the division operator is left-to-right associate. You can use parentheses to override the default operator associativity rules. Most Java operators are left-to-right associative.
🌐
Wikipedia
en.wikipedia.org › wiki › Diamond_operator
Diamond operator - Wikipedia
3 weeks ago - In Unicode, the diamond operator is represented by the character U+22C4 ⋄ DIAMOND OPERATOR.
🌐
W3Schools
w3schools.com › java › java_operators.asp
Java Operators
Operators Arithmetic Assignment Comparison Logical Precedence Code Challenge Java Strings · Strings Concatenation Numbers and Strings Special Characters Code Challenge Java Math Java Booleans