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
• Liutauras Vilda
• Paul Clapham
Sheriffs:
• paul wheaton
• Tim Cooke
• Henry Wong
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Piet Souris
Bartenders:
• Mike London

# Casting

Ranch Hand
Posts: 51
• Number of slices to send:
Optional 'thank-you' note:
Can you assign short to char

Sheriff
Posts: 9109
12
• Number of slices to send:
Optional 'thank-you' note:
Did you try it?

Ranch Hand
Posts: 3271
• Number of slices to send:
Optional 'thank-you' note:
Think about the range of a short and the range of a char. Are they the same? Are the sizes the same?
Now that you've got that, can you assign a char to a short, or vice versa?
Corey

Tanuja Vaid
Ranch Hand
Posts: 51
• Number of slices to send:
Optional 'thank-you' note:
Yes I have tried it! hence the question
I tried this code and i am getting a compiler error both on line 1 & 2
short s=10;
char c=s; //1
s=c; //2
Can somebody explain why?

Tanuja Vaid
Ranch Hand
Posts: 51
• Number of slices to send:
Optional 'thank-you' note:
Yes I have tried it! hence the question
I tried this code and i am getting a compiler error both on line 1 & 2
short s=10;
char c=s; //1
s=c; //2
Can somebody explain why?

Ranch Hand
Posts: 75
• Number of slices to send:
Optional 'thank-you' note:
No u cant assign short to char or visa versa.
Look at their sizes....
char is 0-65535
and short is -2^17 to 2^17-1
Conversions between char and short are narrowing conversions...so cast is always needed.
so ,
short s=10;
char c=(short)s;//ok.
so this is why u get compiler error.
even conversions b/w byte and short are narrowing conversions.
Check out this topic in KM..its explained there very well.
hope this helps
Swapna

Corey McGlone
Ranch Hand
Posts: 3271
• Number of slices to send:
Optional 'thank-you' note:
Well, since the compiler is giving you an error when you try to assign a short to a char, then I guess you can't do it - at least not implicitly. Why not?
What is the size of a char? 2 bytes
What is the size of a short? 2 bytes
Based on that, it would seem as if you could assign one to the other interchangably. Now, think about the other question. Are their ranges the same?
NO!
A char, in Java, is unsigned while a short is signed. Therefore, whenever you try to assign a short to a char (or vice versa) there is a chance for information loss. Due to this, the compiler views this as a narrowing conversion and refuses to implicitly perform a cast. However, if you're sure you're not going to lose data (or don't care), you can cast it explicitly, like this:

I hope that helps,
Corey
By the way, please don't be upset by the answers that Marilyn and I gave earlier. From what you had posted, it looked like you had asked a question that can be looked up in any text. We were simply trying to get you to think about it - hence, really learn it. Personally, I'd rather teach you how to learn Java than teach you Java.

Ranch Hand
Posts: 71
• Number of slices to send:
Optional 'thank-you' note:
This works though.. dont forget about compile time constants

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
--final short s = 10;
--char c=s;
That works because the compiler can determine that the range of the final ``s" will always be in range for ``c" therefore no need to cast, right?

Corey McGlone
Ranch Hand
Posts: 3271
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Ryan Upton:
--final short s = 10;
--char c=s;
That works because the compiler can determine that the range of the final ``s" will always be in range for ``c" therefore no need to cast, right?

Not quite. This works because the compiler knows that the variable s contains the value 10 (because of the attribute, final) so writing this is really no different than writing this:

That's why it works. The range of s (a short) is not always within the range of c (a char) because s can be negative while c can not.
Corey

Ranch Hand
Posts: 62
• Number of slices to send:
Optional 'thank-you' note:
so why the following cant work:
Long val= new Long("11");
byte b=(byte)val;

thanks
Krussi

Corey McGlone
Ranch Hand
Posts: 3271
• Number of slices to send:
Optional 'thank-you' note:
Don't get confused between primitives and references. What you wrote can't work because you can't cast a reference type to a primitive type. The variable val references a Long object, while the variable b contains a byte primitive. This doesn't work for the same reason this doesn't work:

An object and a primitive are very different in Java.
However, if this is what you meant:

This will work. The cast of l to a byte is required because a long has a much wider range than a byte does. Therefore, if you try to assign a long to a byte, there is a chance for data loss - this is known as a narrowing conversion. But, if you apply the cast, you'll be fine.
Corey

krussi rong
Ranch Hand
Posts: 62
• Number of slices to send:
Optional 'thank-you' note:
I got it!
thanks Corey !

Bartender
Posts: 2205
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Corey McGlone:

This will work. The cast of l to a byte is required because a long has a much wider range than a byte does.

I think you want your code to be this, to match your text description:
long l = 11;
byte b = (byte)l;
or
long var = 11;
byte b = (byte)var;
But not
long var = 11;
byte b = (byte)l;

Corey McGlone
Ranch Hand
Posts: 3271
• Number of slices to send:
Optional 'thank-you' note:
Oops. Too many variables!

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?