File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt" Watch "Doubt" New topic
Author

Doubt

Raj Neets
Ranch Hand

Joined: Nov 12, 2002
Posts: 46
Hai
Iam reading mughal book for JCP, on pg 48 author say that u need explicit cast to assign character to short or byte.
but short s1 ='a';
byte b1='A';
the above piece of code compile and gives result.
can some explain it to me.
Thax
Raj
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Now try this:

does that help explain things?
[ February 06, 2003: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Rahul Gupta
Ranch Hand

Joined: Nov 20, 2002
Posts: 37
hi raj,
here is the complete picture
byte->short->int->long->float->double
                    ^
                    |
                    char
This means that while traversing in the direction of the arrow no explicit cast is required. But while traversing in the opposite direction explicit cast is required. Also explicit cast is required when there is any conversion between (byte or short) and the char VARIABLES (not literals).
Hope that helps
rahul

[ February 06, 2003: Message edited by: Rahul Gupta ]
[ February 06, 2003: Message edited by: Rahul Gupta ]
[ February 06, 2003: Message edited by: Rahul Gupta ]
[ February 06, 2003: Message edited by: Rahul Gupta ]

regards,<p>rahul <p>SCJP 1.4<br />--------<br />Life is great, make it meaningful<br />---------------------------------
Pallavi Chakraborty
Ranch Hand

Joined: Jan 18, 2003
Posts: 93
Hello Jessica,
I wanted to clarify something, from your answer.
When char c1 = 'a' is not declared as final, it means that the value of a may change into something which will not fit into a char or a byte and the compiler doesn't want to take chances.
With - final char c2 = f - the complier knows for sure.
Thank you
Pallavi
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203

I wanted to clarify something, from your answer.
When char c1 = 'a' is not declared as final, it means that the value of a may change into something which will not fit into a char or a byte and the compiler doesn't want to take chances.

In the chain of clarifications I want to correct Pallvi.
when char c1='a'; value of c1 can change to some thing else later but not 'a'. When c1 is assigned to a short without cast compiler generates error.
Pallavi Chakraborty
Ranch Hand

Joined: Jan 18, 2003
Posts: 93
Hello Sarma,
Can you explain the last sentence of your answer in a little more detail?
I think I am missing something in my understanding.
Thank you very much
Pallavi
Tom Adams
Ranch Hand

Joined: Feb 07, 2003
Posts: 56
I think the point here is what char values will or will not fit into a short. Since short is signed its high order bit is not avail for data.
short only has 15 bits avail for data where char has 16.
A = 0x0041 (fits fine)
32767 (0x7FFF) would be the largest char that would fit into a short.
Thus the compiler will only let the statement pass if he knows the data can fit into 15 bits. This can only be the case with literals or final variables.
At least that is my theory...
[ February 07, 2003: Message edited by: Tom Adams ]

Tom
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203

Iam reading mughal book for JCP, on pg 48 author say that u need explicit cast to assign character to short or byte.
but short s1 ='a';
byte b1='A';
the above piece of code compile and gives result.
can some explain it to me.


Pallvi,
This entire POST startwed with the question what can be assigned to a short and what not.
So my clarification is in accordance to what is asked above.
char c1='a';
short s1=c1; // error
final char c2='a';
short s2= c2; // OK
char c3='a';
short s3=(short)c3;
Pallavi Chakraborty
Ranch Hand

Joined: Jan 18, 2003
Posts: 93
Hello Tom, Sarma
Thank you ..... Got it now.
Pallavi
Pallavi Chakraborty
Ranch Hand

Joined: Jan 18, 2003
Posts: 93
Hello Tom, Sarma
Thank you ..... Got it now.
Pallavi
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
Thanks everybody ,It cleared my concepts about conversions
-Archana
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt