• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Que1.)
byte b=0;
b=b+1;
Compile will get 1 error.The reason I know.
But byte b=0;
b+=1;
can not make error.why ?I don't know.Who can give me an answer.
Thank you.
Que2.)
a char can get value from'\u0000' to '\uFFFF',but char a='\u000A' get 1 error when compiled.Why?
Que3.)I get confuse on String immutable.Where can I get good information about the String immutable?How use String functions?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hans:
Here is an answer to your Question# 1:
Assignment operators of the type operator= (for eg. +=) cast their result to the type of the left operand. So when you write
byte b=0;
b = 0;
b += 1;
the result (0 + 1 = 1) is type casted to int and assigned to the variable 'b'.
However, the '=' operator does not perform an implicit cast. That is the reason you get a compile-time error when you write
b = b + 1;
Note here that b+1 results in an int because of numeric promotion.
Hope this clarifies your doubt.
Cheers,
Balu
 
Balasubramanyam Kallavi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hans:
An explanation to your Question# 2 is provided in the Java Language Specification. Study Section #3.2 of Chapter# 3 (Lexical Translations) in conjunction with Section# 3.10.4 (Character Literals).
I am giving below excerpts of both the sections for a quick clarification.
[Quote-JLS]
Section# 3.10.4
Because Unicode escapes are processed very early, it is not correct to write '\u000a' for a character literal whose value is linefeed (LF); the Unicode escape \u000a is transformed into an actual linefeed in translation step 1 (�3.3) and the linefeed becomes a LineTerminator in step 2 (�3.4), and so the character literal is not valid in step 3. Instead, one should use the escape sequence '\n' (�3.10.6). Similarly, it is not correct to write '\u000d' for a character literal whose value is carriage return (CR). Instead, use '\r'.
Section# 3.2
3.2 Lexical Translations
A raw Unicode character stream is translated into a sequence of tokens, using the following three lexical translation steps, which are applied in turn:
A translation of Unicode escapes (�3.3) in the raw stream of Unicode characters to the corresponding Unicode character. A Unicode escape of the form \uxxxx, where xxxx is a hexadecimal value, represents the Unicode character whose encoding is xxxx. This translation step allows any program to be expressed using only ASCII characters.
A translation of the Unicode stream resulting from step 1 into a stream of input characters and line terminators (�3.4).
A translation of the stream of input characters and line terminators resulting from step 2 into a sequence of input elements (�3.5) which, after white space (�3.6) and comments (�3.7) are discarded, comprise the tokens (�3.5) that are the terminal symbols of the syntactic grammar (�2.3).
[Unquote JLS]
Hope this helps,please note that we cannot use '\u000d' too.
Cheers,
Balu
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi bala
that was a good exp. even i was facing the same problem now i understood why it was behaving so strange
but i have another prob. related to this just check out my posting subject "this is really amazing....."
Thanx
aNiL
reply
    Bookmark Topic Watch Topic
  • New Topic