This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

why "...." is valid?  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would anyone like to tell me why the following 2 declaration statement are valid?
char b = '\61';
char c = '\061';
thanks!
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The octal escape sequences \000 to \377 represent the same characters as the Unicode escape sequences \u0000 to \u00ff.
A character literal is expressed as a character or an escape sequence enclosed in single quotes: '1' '\u0031' '\061'
Try this,
char c1 = '\061';
char c2 = '\u0031'; //0x31 == 061
char c3 = '1';
char c4 = (char)49; //49 == 0x31 == 061
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
System.out.println("A very\061long\u0031string1of text");
int i1 = c1, i2 = c2, i3 = c3, i4 = c4;
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
[ July 27, 2003: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a little precision:

char c4 = (char)49; //49 == 0x31 == 061


In this case, is also legal.
Reason:


Java relaxes its assignement conversion rule when a literal int value is assigned to a narrower primitive type (byte, short, or char), provided the literal value falls within the legal range of primitive type


Here, 0 <= 49 <= 2^16 - 1, so narrower assignement is legal.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Cyril. I could not decide whether to include the cast or not. I decided to include it to subliminally suggest '\061' '\u0031' '1' are of type char whereas 49 is of type int. The first three are character literals and 49 is an integer literal. I am glad you added your remarks.
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!