aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question on conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "question on conversion" Watch "question on conversion" New topic
Author

question on conversion

mohamed hamdy
Ranch Hand

Joined: Feb 13, 2003
Posts: 72
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?
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
java language specification (chapter 5.2) states that there is an implicit narrowing conversion with compile time constants!!!
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You were taught incorrectly. As long as there are no variables on the right hand side there isn't a problem.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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

Joined: Dec 20, 2001
Posts: 3271
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.)
Karin Paola Illuminate
Ranch Hand

Joined: Oct 18, 2002
Posts: 109
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 ]

I not only use all the brains that I have, but all that I can borrow. [Laurence J. Peter]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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
Mirek Malicki
Greenhorn

Joined: Mar 06, 2003
Posts: 1
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: question on conversion
 
Similar Threads
Khalid Test
narrowing conversion
Casting
Character manipulation questions?
8-Bit problem