wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting" Watch "Casting" New topic
Author

Casting

Tanuja Vaid
Ranch Hand

Joined: Mar 07, 2002
Posts: 51
Can you assign short to char
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Did you try it?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Corey McGlone
Ranch Hand

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


SCJP Tipline, etc.
Tanuja Vaid
Ranch Hand

Joined: Mar 07, 2002
Posts: 51
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

Joined: Mar 07, 2002
Posts: 51
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?
swapna sivaraju
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
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


SCPJ2
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
This works though.. dont forget about compile time constants
Ryan Upton
Greenhorn

Joined: Apr 17, 2002
Posts: 1
--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

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

Joined: Jan 30, 2002
Posts: 62
so why the following cant work:
Long val= new Long("11");
byte b=(byte)val;

thanks
Krussi
Corey McGlone
Ranch Hand

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

Joined: Jan 30, 2002
Posts: 62
I got it!
thanks Corey !
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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;



Rob
SCJP 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Oops. Too many variables!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting