85

Please explain what, exactly, happens when the following sections of code are executed:

int a='\15';
System.out.println(a);

this prints out 13;

int a='\25';
System.out.println(a);

this prints out 21;

int a='\100';
System.out.println(a);

this prints out 64.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
VAr
  • 2,551
  • 1
  • 27
  • 40
  • 22
    Looks like it is interpreting the number as an octal. – Nikhil Oct 01 '13 at 04:34
  • 5
    `int a='\15'` first creates a `char` based on octal number "15" (decimal 13, as a char, it's the ascii "Carriage Return"), which you then recast to an `int` integer. This is legal since chars are 16 bit integers, so it's a safe upcast and no notices about casting are issued by Java. The other answers cover the whole octal/decimal thing already. – Mike 'Pomax' Kamermans Oct 01 '13 at 04:43
  • 3
    possible duplicate of [Why is '\117' a valid character literal in Java?](http://stackoverflow.com/questions/14950593/why-is-117-a-valid-character-literal-in-java) – gparyani Oct 01 '13 at 21:10

3 Answers3

116

You have assigned a character literal, which is delimited by single quotes, eg 'a' (as distinct from a String literal, which is delimited by double quotes, eg "a") to an int variable. Java does an automatic widening cast from the 16-bit unsigned char to the 32-bit signed int.

However, when a character literal is a backslash followed by 1-3 digits, it is an octal (base/radix 8) representation of the character. Thus:

  • \15 = 1×8 + 5 = 13 (a carriage return; same as '\r')
  • \25 = 2×8 + 5 = 21 (a NAK char - negative acknowledgement)
  • \100 = 1×64 + 0×8 + 0 = 64 (the @ symbol; same as '@')

For more info on character literals and escape sequences, see JLS sections:

Quoting the BNF from 3.10.6:

OctalEscape:
    \ OctalDigit
    \ OctalDigit OctalDigit
    \ ZeroToThree OctalDigit OctalDigit

OctalDigit: one of
    0 1 2 3 4 5 6 7

ZeroToThree: one of
    0 1 2 3
Ry-
  • 218,210
  • 55
  • 464
  • 476
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 3
    I can't find it in section 3.10.1.. It's in 3.10.4, Character Literals – Rafi Kamal Oct 01 '13 at 04:52
  • 1
    The section on integer literals is irrelevant. See [Section 3.10.4](http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.4). Also see [Section 3.10.6](http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.6) for character and string escape sequences. – Ted Hopp Oct 01 '13 at 04:53
  • `1-3 digits` -- it should have been `0-3`. And its for 3 digit octal, two digit octal can still have from `0-7`. ex. '\77' is still valid. – Jayamohan Oct 01 '13 at 04:55
  • 6
    @Jayamohan I meant 1-3 in quantity, not value – Bohemian Oct 01 '13 at 05:04
  • @Bohemian. Nice answer. I'd suggest you add the syntax of octal escape literal here, so it will be more clear. :) – Rohit Jain Oct 01 '13 at 05:08
  • @RohitJain Thanks. You're pretty good at encyclopaedic answers and references - is that edit what you meant? – Bohemian Oct 01 '13 at 08:13
  • @Bohemian. hehe :) Not exactly. I believe answer containing words from official source is far better than answer just containing your own words. But it should be a mixture of both. Yes, that is what exactly I meant. :) – Rohit Jain Oct 01 '13 at 08:39
  • SO, it needs to be 100 ha! :) OK... +1, btw, nice answer – Sage Dec 25 '13 at 23:49
19

The notation \nnn denotes an octal character code in Java. so int a = '\15' assigns the auto-cast'ed value of octal character 15 to a which is decimal 13.

BahmanM
  • 1,420
  • 10
  • 18
  • 5
    The notation `\nnn` does _not_ denote an octal number in Java. In OP's code, it denotes an octal character escape sequence. The surrounding quote marks are essential. The character is then widened to an integer value by the assignment. (Octal numbers are denoted by a leading zero digit.) – Ted Hopp Oct 01 '13 at 04:50
  • Good point; should have mentioned the auto-cast in the answer. Updated the answer. – BahmanM Oct 01 '13 at 05:01
6

The fact that you put the digits in quotes makes me suspect it is interpreting the number as a character literal. The digits that follow must be in octal.

Nikhil
  • 2,298
  • 13
  • 14