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!

5
  • "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. Commented May 22, 2018 at 22:23
  • Possible duplicate of Concat an integer to a String - use String literal or primitive from performance and memory point of view? Commented May 22, 2018 at 22:23
  • 14
    That code won't compile. str has to be a String, not a char Commented May 22, 2018 at 22:23
  • 2
    Your first line char str = 'foo'; doesn't make sense. May be you mean String str = "foo"; Commented May 22, 2018 at 22:26
  • 2
    @DyZ It is not a duplicate of that question : there is no String concatenation here Commented May 22, 2018 at 22:34

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Actually, it's the UTF-16 code unit, one or two of which form a Unicode codepoint. Try String foo="🙂";.
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.

2 Comments

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.)
I will toot my own horn here and add a link to another question I answered that involves arithmetic on char types: stackoverflow.com/questions/33770180/…
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).

2 Comments

You're missing the very important 4.2. Primitive Types and Values: "char, whose values are 16-bit unsigned integers representing UTF-16 code units (§3.1)", to truly answer the question about "ASCII value".
@andreas that was implicit from 4.2.1, but ok I added it thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.