Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Few Tough questions

 
Sandeep Lodhia
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
Pls help me in this.
1. byte a = 10;
a= a+1 ;
This will give compile error.Great !!!
Where as
a+=1;
This does not give error ???

2. //char a = '\u000A'; This will give compile error.Again Great !!!
Where as
//char a = '\u0001';
This does not give error ???

Any logical ans for these?
Sandeep
 
aymen esawey
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. the result is int and must be cast
. auto conversion takes place with +=,*=.....
2.unicode char \u000a is translated by the compiler into a linefeed first thing before compilation so the result is:
1. char c='
2. ';
missing terminator on line one.
hope am right.
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sandeep,
1. byte b=1;
b= b+1;
//In this case b implicitly converted into int
//and the result of right side calculation is
//int. That's why you'll get an error. You can
//do following
b= (byte)(b+1);
b+=1
// In this case Java does conversion to int.
That's all.
2. '\u000A' is a "carriage return" in java. You
cannot use '\u000D' as well. Please search
forums, in this site, youl get a lot of topics
devoted this.
Thanx,
Jam
 
Sandeep Lodhia
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good replies Guys !!! Thanx
But my point in 2nd question is... even though the line is commented, still it gives compilation error.
Pls elaborate.
[ March 13, 2002: Message edited by: Sandeep Lodhia ]
 
Nazmul Huda Sarkar
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sandeep
When u compile any Java program and if this code contains any unicode literal then before compilation those unicode literal first converted to ASCII (or machine default) character literal. so before compilation of
1. //char a= '\u000A'; will be translated to
1. //char a='
2. ';

so during compilation line 2 will give compile time error.
Hope this will help u....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic