• 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 on conversion

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i was tought that this line:
char c='c'-'a';
will give compile error because of the promotion of character literals (or any integral literals narrower tha the type int) to int.
can someone comment me?
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java language specification (chapter 5.2) states that there is an implicit narrowing conversion with compile time constants!!!
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You were taught incorrectly. As long as there are no variables on the right hand side there isn't a problem.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Paul:
You were taught incorrectly. As long as there are no variables on the right hand side there isn't a problem.


Well, in the case that the value to be assigned fits within the assigned variable, there isn't a problem.
Notice that this works:

while this causes an error:

The reason for this is that the compiler can determine if the value being assigned can fit within the variable that it is being assigned to. This can be done because you're assigning a constant value. If you were to use a variable, the compiler wouldn't know if it fit in a variable or not and, hence, would always throw an error.
This works:

while this doesn't:

Even though we can see that 3 will be assigned to the char and we know that 3 fits within the range, the compiler isn't sure what the value of i will be when the assignment occurs. Therefore, an error is issued.
I hope that helps,
Corey
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Corey's last example, if i is made final then it will work since the compiler knows that the value of i can never change.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Paul:
In Corey's last example, if i is made final then it will work since the compiler knows that the value of i can never change.


In which case, the compiler treats the variable as if it was a literal. (Hence, it works - good point, Thomas.)
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know what the range is of a char:
\u0000 to \uffff, or
0 to 65535
But, I don't know how to determine if 'a' * 2 fits in the range of a char and 'a' * 2000 does not fit in the range. It looks to me like "acacadabra".
Should I know the char value of 'a' * 2 for the exam, for example?
Thanks.
My Study Notes
[ March 19, 2003: Message edited by: Karin Paola Illuminate ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need to know the values of individual characters for the exam. Just for reference, however, 'a' is equivalent to 97. Therefore, 'a' * 2 = 194 and 'a' * 2000 = 194000, which is out of the range of a char which only goes up to 2^16 (65535).
If you have to know the value of a particular character for a question on the exam, you'll be given that information.
Corey
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mohamed hamdy:
i was tought that this line:
char c='c'-'a';
will give compile error because of the promotion of character literals (or any integral literals narrower tha the type int) to int.
can someone comment me?


You can initialize char and byte primitive with the integer const expression if it fits the size of the relevant type
for instance
byte b = 10;
// ^byte ^int
char c = 1;
// ^char ^int
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic