0

Kindly consider this bit of Java code. It works, but I'm trying to understand what it does.

char str   = 'foo'
long prime = 503;
long hash  = 0;
hash = prime + str.charAt(1);

I'm confused because this is mathematical addition of different data types. Would I be correct if I thought the character was converted to its ASCII value for the purposes of this math operation?

Thanks!

  • "Would I be correct if I thought the character was converted to its ASCII value for the purposes of this math operation?" a char is a numerical data type, there is no _conversion_. – tkausl May 22 '18 at 22:23
  • Possible duplicate of [Concat an integer to a String - use String literal or primitive from performance and memory point of view?](https://stackoverflow.com/questions/14782804/concat-an-integer-to-a-string-use-string-literal-or-primitive-from-performance) – DYZ May 22 '18 at 22:23
  • 14
    That code won't compile. `str` has to be a String, not a `char` – Malt May 22 '18 at 22:23
  • 2
    Your first line `char str = 'foo';` doesn't make sense. May be you mean `String str = "foo";` – Thomas Fritsch May 22 '18 at 22:26
  • 2
    @DyZ It is not a duplicate of that question : there is no String concatenation here – C.Champagne May 22 '18 at 22:34

3 Answers3

6

First, your syntax is wrong. I corrected that (see code below). Now the value of hash will be 614. Because hash = prime + str.charAt(1) means you are adding the ASCII value of the character at index 1 of str i.e. o. The ASCII value of o is 111. So the value of hash is 614 after the addition.

String str   = "foo";
long prime = 503;
long hash  = 0;
hash = prime + str.charAt(1);
System.out.println(hash); // This line prints 614. Because value prime is 503 and the ASCII value of `o` is 111.

You can play with this Ideone https://ideone.com/EcbP8x and run the code to see output.

EDIT

As @Henry pointed out, the value of character is not limited to only ASCII value in Java. So the better term to use here is Unicode code point instead of ASCII value. To know more details please refer to What's the difference between ASCII and Unicode?

Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56
  • strictly speaking, it is not the ASCII value but the Unicode code point of the character (as long as it is in the BMP). Both agree in the ASCII range but many more characters can be used in Java. – Henry May 23 '18 at 04:57
  • 1
    Actually, it's the UTF-16 code unit, one or two of which form a Unicode codepoint. Try `String foo="";`. – Tom Blodget May 23 '18 at 10:40
4

Yes. According to the Java Language Specification, §5.6.2, when you apply the addition operation + to a long and char, a "widening primitive conversion" will be performed on the char, and addition will be performed with two long values.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • A slightly different way of saying this is that `char` and `long` aren't actually different types. They are both integers. So just like you can add `int` and `byte` together (and byte is widened to int) and you can add an `int` and a `long` (int is widened to long) so you can add `char` to other types of integers. (And integers can be converted to floating point, so you can add those to `char` and other ints as well.) – markspace May 22 '18 at 22:41
  • I will toot my own horn here and add a link to another question I answered that involves arithmetic on `char` types: https://stackoverflow.com/questions/33770180/how-do-i-store-these-printed-letters-into-an-array/33770240#33770240 – markspace May 22 '18 at 22:46
2

Would I be correct if I thought the character was converted to its ASCII value for the purposes of this math operation?

Yes (except you need to change your first line to String str = "foo";. Basically char is an integral type which can be used in arithmetic expressions.

Javadoc says:

charAt

public char charAt(int index)

Returns the char value at the specified index.

JLS (Java Language Specification) says:

Chapter 4. Types, Values, and Variables

The numeric types are the integral types byte, short, int, long, and char

and

4.2. Primitive Types and Values:

char, whose values are 16-bit unsigned integers representing UTF-16 code units (§3.1)"

and

4.2.1. Integral Types and Values

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

and

5.6.2. Binary Numeric Promotion

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:

...

Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

...if either operand is of type long, the other is converted to long.

and

15.18.2. Additive Operators (+ and -) for Numeric Types

The binary + operator performs addition when applied to two operands of numeric type, producing the sum of the operands.

Binary numeric promotion is performed on the operands (§5.6.2).

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • 2
    You're missing the very important [**4.2. Primitive Types and Values**](https://docs.oracle.com/javase/specs/jls/se10/html/jls-4.html#jls-4.2): *"`char`, whose values are 16-bit unsigned integers representing **UTF-16** code units (§3.1)"*, to truly answer the question about "ASCII value". – Andreas May 22 '18 at 23:08
  • @andreas that was implicit from 4.2.1, but ok I added it thanks – Jean-Baptiste Yunès May 23 '18 at 04:50