string - What does a[c - 'a' ]++ mean in Java? - Stack Overflow
syntax - What is the Java ?: operator called and what does it do? - Stack Overflow
what is the difference between a++ and ++a or a-- and --a in java? - Stack Overflow
I don’t understand a concept with increments. A++ = A + 1. However in formulas that use A++ + ++A, my answers are always incorrect. Why??? It’s driving me crazy that I cannot get any of these outputs correct
Videos
'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.
Characters in Java can be manipulated as small integers in some contexts. The expression c - 'a' means "the integer representation of c" minus "the integer representation of 'a'". In other words, you're getting the offset of c with respect to 'a' in the ASCII character set, i.e., the index of c in the lowercase alphabet.
Thus a[c - 'a']++ means increment the counter in a with index i, where i is the position of c in the alphabet.
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
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.