'a' is of type char which - coming from a C tradition - can be interpreted as a number type. That is, you can do arithmetic with it just like with int or long.

The value of a char is traditionally given by the ASCII table where 'a' happens to be the same as 97.

In your code, c is also a char, and if you assume s1 and s2 to only contain lower-case letters, you know that c is somewhere in the range [97,122]. Subtracting 'a' (i.e., 97) from that then gives you the more convenient range [0,25] to work with.

I say "more convenient" because here, c - 'a' is used to index the array a, and as you know, array indices start at 0.

Answer from Thomas on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_operators.asp
Java Operators
Arrays Loop Through an Array Real-Life Examples Multidimensional Arrays Code Challenge · Java Methods Java Method Challenge Java Method Parameters · Parameters Return Values Code Challenge Java Method Overloading Java Scope Java Recursion · Java OOP Java Classes/Objects Java Class Attributes Java Class Methods Java Class Challenge Java Constructors Java this Keyword Java Modifiers · Access Modifiers Non-Access Modifiers Java Encapsulation Java Packages / API Java Inheritance Java Polymorphism Java super Keyword Java Inner Classes Java Abstraction Java Interface Java Anonymous Java Enum
🌐
Tutorialspoint
tutorialspoint.com › java › java_basic_operators.htm
Java Operators
There are few other operators supported by Java Language. Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable. The operator is written as − · variable x = (expression) ? value if true : value if false ... In this example, we're creating two variables a and b and using ternary operator we've decided the values of b and printed it.
🌐
GeeksforGeeks
geeksforgeeks.org › java › operators-in-java
Java Operators - GeeksforGeeks
Logical Operators are used to perform "logical AND" and "logical OR" operations, similar to AND gate and OR gate in digital electronics. They have a short-circuiting effect, meaning the second condition is not evaluated if the first is false. ... import java.io.*; class Geeks { // Main Function public static void main (String[] args) { // Logical operators boolean x = true; boolean y = false; System.out.println("x && y: " + (x && y)); System.out.println("x || y: " + (x || y)); System.out.println("!x: " + (!x)); } }
Published   November 12, 2025
🌐
Programiz
programiz.com › java-programming › operators
Java Operators: Arithmetic, Relational, Logical and more
In the above example, we have used +, -, and * operators to compute addition, subtraction, and multiplication operations. ... Note the operation, a / b in our program. The / operator is the division operator. If we use the division operator with two integers, then the resulting quotient will also be an integer. And, if one of the operands is a floating-point number, we will get the result will also be in floating-point. In Java, (9 / 2) is 4 (9.0 / 2) is 4.5 (9 / 2.0) is 4.5 (9.0 / 2.0) is 4.5
🌐
DigitalOcean
digitalocean.com › community › tutorials › addition-assignment-operator-in-java
What is += Addition Assignment Operator in Java? | DigitalOcean
August 3, 2022 - The first line here gives an error as int can’t be added to a double. ... However, when using the += operator in Java, the addition works fine as Java now converts the double to an integer value and adds it as 1.
🌐
NTU
www3.ntu.edu.sg › home › ehchua › programming › java › J2_Basics.html
Java Basics - Java Programming Tutorial
The type char represents a single ... For example, character '0' is 48 (decimal) or 30H (hexadecimal); character 'A' is 65 (decimal) or 41H (hexadecimal); character 'a' is 97 (decimal) or 61H (hexadecimal)....
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › operators.html
Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
As we explore the operators of the Java programming language, it may be helpful for you to know ahead of time which operators have the highest precedence. The operators in the following table are listed according to precedence order. The closer to the top of the table an operator appears, the higher its precedence.
Find elsewhere
🌐
ScholarHat
scholarhat.com › home
Operators in Java - Types of Operators in Java ( With Examples )
Full-stack Java jobs pay ₹15–30 LPA in just 2 years. Don’t wait, Enroll now in our Java Full Stack Training to grow fast. Different operations are carried out on variables by Java operators. Examples include adding two integers together, as in 2 + 3.
Published   September 6, 2025
Top answer
1 of 16
224

Yes, it is a shorthand form of

int count;
if (isHere)
    count = getHereCount(index);
else
    count = getAwayCount(index);

It's called the conditional operator. Many people (erroneously) call it the ternary operator, because it's the only ternary (three-argument) operator in Java, C, C++, and probably many other languages. But theoretically there could be another ternary operator, whereas there can only be one conditional operator.

The official name is given in the Java Language Specification:

§15.25 Conditional Operator ? :

The conditional operator ? : uses the boolean value of one expression to decide which of two other expressions should be evaluated.

Note that both branches must lead to methods with return values:

It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.

In fact, by the grammar of expression statements (§14.8), it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear.

So, if doSomething() and doSomethingElse() are void methods, you cannot compress this:

if (someBool)
    doSomething();
else
    doSomethingElse();

into this:

someBool ? doSomething() : doSomethingElse();

Simple words:

booleanCondition ? executeThisPartIfBooleanConditionIsTrue : executeThisPartIfBooleanConditionIsFalse 
2 of 16
34

Others have answered this to reasonable extent, but often with the name "ternary operator".

Being the pedant that I am, I'd like to make it clear that the name of the operator is the conditional operator or "conditional operator ?:". It's a ternary operator (in that it has three operands) and it happens to be the only ternary operator in Java at the moment.

However, the spec is pretty clear that its name is the conditional operator or "conditional operator ?:" to be absolutely unambiguous. I think it's clearer to call it by that name, as it indicates the behaviour of the operator to some extent (evaluating a condition) rather than just how many operands it has.

🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › op1.html
Assignment, Arithmetic, and Unary Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
You can also combine the arithmetic operators with the simple assignment operator to create compound assignments. For example, x+=1; and x=x+1; both increment the value of x by 1.
🌐
GeeksforGeeks
geeksforgeeks.org › java › basic-operators-java
Basic Operators in Java - GeeksforGeeks
September 12, 2023 - For example, x--. Note: The increment and decrement operators are called unary arithmetic operators as they work with a single operand whereas the rest of arithmetic operators are called binary arithmetic operators as they operate on two operands. ... // Java Program to Illustrate Arithmetic Operators import java.util.*; // Class 1 class A { // Main driver method public static void main(String args[]) { int a = 10, b = 4, res; // printing a and b System.out.println("a is " + a + " and b is " + b); res = a + b; // addition System.out.println("a+b is " + res); res = a - b; // subtraction System.out.println("a-b is " + res); res = a * b; // multiplication System.out.println("a*b is " + res); res = a / b; // division System.out.println("a/b is " + res); res = a % b; // modulus System.out.println("a%b is " + res); } }
🌐
Quora
quora.com › What-is-the-value-of-a-a-a-a-How-is-it-done-in-Java
What is the value of a = ++a + ++a + ++a? How is it done in Java? - Quora
Answer (1 of 13): Evaluation order in Java is strictly defined, left to right. See the Java Language Specification, section 15.7. This is a Java translation of a C “riddle”, which is not a “riddle”, it’s a demonstration that C has an undefined expression evaluation order, when the code is lacki...
🌐
W3Schools
w3schools.com › java › java_examples.asp
Java Examples
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
🌐
Unstop
unstop.com › home › blog › operators in java | 9 types, precedence & more (+examples)
Operators In Java | 9 Types, Precedence & More (+Examples)
December 9, 2024 - Unary operators are those that operate on a single operand to alter its value or state. For example, the logical complement operator (!) inverts a boolean value, while the unary minus (-) changes the sign of a number. These Java operators are applied directly to the operand, streamlining operations like sign reversal and logical inversion.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › characters.html
Characters (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
There are times, however, when you need to use a char as an object—for example, as a method argument where an object is expected. The Java programming language provides a wrapper class that "wraps" the char in a Character object for this purpose. An object of type Character contains a single field, whose type is char.
🌐
W3Schools
w3schools.com › java › java_for_loop.asp
Java For Loop
In this example, the loop starts with i = 10. The condition i < 5 is already false, so the loop body is skipped, and nothing is printed. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Quora
quora.com › What-does-“-”-mean-in-Java
What does “?” mean in Java? - Quora
Answer (1 of 9): The ? is part of the ternary operator or conditional operator ? : The ternary operator is somewhat like an if-else statement. Example- [code]if (a>b) c=a; else c=b; [/code]Becomes [code] c = (a>b)? a:b ; [/code]Though it should be noted that the nested form becomes rath...