0

I have been looking around on the internet at caesar ciphers and while I understand the loop I don't understand why this line of code is able to shift a char to another char? I don't understand this line here:

        letter = (char)(letter - 26);

When I take (char) out it doesn't work and I have never seen it with the type being in parentheses followed by an operation.

Hopefully this is an easy question and thanks for the help.

for (int i = 0; i < buffer.Length; i++)
    {
        // Letter.
        char letter = buffer[i];
        // Add shift to all.
        letter = (char)(letter + shift);
        // Subtract 26 on overflow.
        // Add 26 on underflow.
        if (letter > 'z')
        {
//The following line is the line I don't understand.  Why char in parentheses then another parentheses?
        letter = (char)(letter - 26);
        }
        else if (letter < 'a')
        {
        letter = (char)(letter + 26);
        }
        // Store.
        buffer[i] = letter;
    }
DarthShiznit
  • 13
  • 1
  • 1
  • 5
  • Ugh, my hand slipped. Anyways, please don't use code snippets for code that isn't javascript. Java well not play well with those. – Compass Dec 04 '14 at 21:08
  • The char in parenthesis is to do a [cast][1] in java. [1]: http://stackoverflow.com/questions/5306835/casting-objects-in-java – cesar_sr91 Dec 04 '14 at 21:09

2 Answers2

1

(char) is a cast. That means that it takes a value which is of one type, and converts it to a value of another type. Thus, if x is an int, (double)x yields a double whose value is the same value as the integer value.

The reason (char) is necessary in this expression is that Java does all its integer arithmetic on values of type int or long. So even though letter is a char, in the expression letter + 26, letter will be automatically converted to an int, and then 26 is added to the integer. (char) converts it back to a char type (which is an integer value from 0 to 65535). Java will not automatically convert a larger integer type (int, whose values are from -2147483648 to 2147483647) to a shorter integer type (char), therefore it's necessary to use a cast.

However, Java does allow this:

letter += 26;

which has the same effect, and does not require a cast.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • "which has the same effect, and does not require a cast." actually it does an implicit cast mandated by the specification in that case. – Gábor Bakos Dec 04 '14 at 21:14
  • @GáborBakos I think it's clear I was talking about explicit casts. The implicit cast is in the language rules to define the semantic effect, but there is no reason for the underlying implementation to perform an actual 32-bit addition and a conversion. – ajb Dec 04 '14 at 21:19
0

There are 26 letters in the english alphabet, and char is an integral type

char ch = 'Z' - 25;
System.out.println(ch); // <-- A

JLS-4.2.1 - Integral Types and Values says (in part),

For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249