-2

When I converted the alphabet characters from char to int in java gave me the values 97 to 122, in alphabetical order.

int a = (char) 'a'; //97
int b = (char) 'b'; //98
...

What are the char values when converted to int gives the values 1 to 96?

6
  • 8
    asciitable.com Oct 18, 2014 at 21:18
  • 1
    @cmbasnett: Except that Java characters represent UTF-16 code units rather than ASCII characters. It's true that the two sets will agree in the range 0-127, but a good answer would explain this explicitly -- and also explain the (much greater) range where they disagree.
    – ruakh
    Oct 18, 2014 at 21:23
  • did you even consider System.out.println((char)96);?
    – user177800
    Oct 18, 2014 at 21:29
  • 1
    (I cannot see how this question is "a simple typographical error". It's a valid question, demonstrating insight that many novice programmers lack.)
    – Hot Licks
    Oct 18, 2014 at 21:43
  • 1
    @HotLicks: I agree that one shouldn't wade through the whole mess at once, but I reach the opposite conclusion from you: I think that ASCII should be mentioned, and understood, only after one is familiar with the rudiments of Unicode. We old-timers view Unicode as a sort of extended ASCII, but to a newbie whose first language is Java, Unicode is (or should be) the simple/normal/usual case, and ASCII is (or should be) a technical footnote. (Of course, this is not to suggest that all the details of Unicode need to be understood at once!)
    – ruakh
    Oct 18, 2014 at 21:55

2 Answers 2

1

The character set used by Java is known as "Unicode" (in particular, "UTF-16", but that's too much detail for now). "Unicode" is a scheme based on "ASCII" (The American Standard Code for Information Interchange), except that Unicode allow for (a lot) more characters.

So in order to understand Unicode it's helpful to understand ASCII.

ASCII was originally developed in the early 1960s as a code for Teletype-style communications. It consists of 7 data bits and one (optional) parity bit. So an ASCII character has a value between zero and 127 (the values you can represent in 7 bits).

The first 32 combinations are "control characters" (such as "Carriage Return", "Line Feed", "Tab", which we recognize as '\r', '\n', and '\t'). In addition, the all-ones combination is generally considered a control character, leaving 95 possible "graphic" characters (if you count "space" as a graphic).

The characters are arranged in groups, with "space" as the lowest-valued graphic (0x20), and some of the punctuation characters (!"#$%'()*+,-./) after that. Starting at 0x30 are the digits 0-9. Starting at 0x41 is capital A, and the upper-case alphabet continues uninterrupted through Z at 0x5A. The lower-case alphabet then runs 0x61 through 0x7A. The remaining punctuation and non-numeric, non-alphabetic characters fill in the remaining spaces.

enter image description here

Unicode differs from ASCII in that, while it completely includes ASCII as the first 128 character positions, a single Unicode character can occupy 16 or 32 bits. This means that not only can, say, the accented characters used in Scandinavian languages be represented, but also even the complex graphics of the Chinese and Japanese languages can be represented.

0
0

To a first approximation,1 a Java char represents a Unicode character. Unicode is a standard that defines, among other things, a set of characters, each of which has a unique integer value — its "codepoint".

Unicode codepoints are normally written in hexadecimal (base 16), with enough leading zeroes so they're at least four hexadecimal digits long. Plus a U+ at the front, because, Unicode. :-)   So, for example, the integer value 65 is usually written as U+0041 (41 because 4 × 16 + 1 = 65).

To find out what character is represented by U+0041 — that is, the character whose codepoint is the integer value 65 — you can look that up in the Unicode character charts. Or, you can just search Google for 'U+0041'. Either way, you'll find that it represents the uppercase letter 'A'.

Alternatively, you could write something like this:

char character0041 = (char) 65;
System.out.println(character0041);

and you'll see that it prints an A.

A few caveats for the latter approach:

  • Many Unicode characters do not actually represent characters that you might write on paper. For example, a line break is represented by Unicode characters; if you tried to print those to the screen, you would not really see what is printed, because the effect of printing a line break is, just moving to the next line.
  • New Unicode characters are defined on a regular basis. There are many possible values that are not currently used, and also many values that are guaranteed to never be used. So Java will not print anything useful if you try to print one of those characters.
  • Your console might not support the full range of Unicode characters. If you try to print a character whose integer value is greater than about 127, you might find it prints gibberish. (But then again, it might be smarter than that. Give it a try!)

Footnotes:

  1. I say "to a first approximation", because Java chars only go up to U+FFFF, whereas the largest Unicode codepoint is U+10FFFF (seventeen times bigger). The way it works is, Unicode defines a range of codepoints called "surrogates" that are guaranteed never to be characters. There's then a whole tricky scheme called UTF-16 for using pairs of these unassigned codepoints in order to represent characters outside the range of char. But fortunately, this is something that Java programmers do not usually need to worry about.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.