aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q how does the above code give error and the code below doesn't??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q how does the above code give error and the code below doesn Watch "Q how does the above code give error and the code below doesn New topic
Author

Q how does the above code give error and the code below doesn't???

rimzy abdulkader
Greenhorn

Joined: Oct 04, 2006
Posts: 18
public class Test8{
public static void main(String a[]){
byte b = 1;
char c = 2;
short s = 3;
int i = 4;
c = b; // 1
s = b; // 2
i = b; //3
s = c * b; //4
}
}

Q how does the above code give error and the code below doesn't???











public class Test9{
public static void main(String a[]){
final byte b = 1;
char c = 2;
short s = 3;
int i = 4;
c = b; // 1
s = b; // 2
i = b; //3
s = c * b; //4
}
}


SCJP 5.0
Rajesh Kadle
Greenhorn

Joined: Sep 06, 2004
Posts: 26
Conversion between byte, short and char are narrowing conversions.

In the first case byte is not final. So when we assignment is done, it byte will be fist widened to int and then narrowed to char. So this causes the compilation error.

In second case the byte value is finalised and the value assigned to byte can be represented with byte type. So in second case, byte value will be directly widened to char. (int is not invloved in this case).

Ranchers please correct me...
[ October 05, 2006: Message edited by: Rajesh Kadle ]

-Raj
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
I have to second your opinion.

are you sure the following code can be compiled?
1. final byte b = 1;
2. char c = 2;
3. short s = 3;
4. int i = 4;
5. c = b;
6. s = b;
7. i = b;
8. s = c * b;

my jdk blows out at line 8 complaining loss of precision.

The code without "final" like below is even worse:
1. byte b = 1;
2. char c = 2;
3. short s = 3;
4. int i = 4;
5. c = b;
6. s = b;
7. i = b;
8. s = c * b;

Line 5 and line 8 both give compiling error.
Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
s = c * b;

this should give a compile error in any both the instance because when you multiply a char and a byte, basically what happens is that both 'c' and 'b' are widened to a int. so the result of c*b is an int and hence you are try to store it in a short you get a loss of precision error.

same thing happens if say one number is a decimal number and the other is a integer(e.g c=4 b= 4.5). so when you try to multiply basically what happens is here the integer is widened to a double and the result is a double and hence you have to store it in a double var.

hope this helps

Dinuka Arseculeratne


if who i am is what i have, and what i have is lost, then who am i?<br /> <br />SCJP 5.0<br />SCWCD 1.4<br />SCBCD preparing
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
then why could a byte variable implicitly casted to char cause the compiling error, if the byte variable is not final?
compiler complains possible loss of precision on c=b;
isn't char wider than byte?
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
See this thread,

http://www.coderanch.com/t/259303/java-programmer-SCJP/certification/primitive-type-char
Shivaprasad P Kanaganahallimath
Ranch Hand

Joined: Sep 25, 2006
Posts: 48
Mr Rimzy ,
the first code gives error at line 1 & 4 because character is unsigned and byte is signed primitive. when you asign a signed byte to unsigned char there is loss of precession.i.e. it will loose the value of leftmost bit which is used to represent the sign. here in this case its 'o'(zero)as positive number and in the program you can change the value of the variable any time. At times it can contain negative number also.


when you make it final,final char c; its a constant and doesn't give error as there is no loss of precession.

I hope you got it.
Bye Rimzy



Shiva Prasad P.K.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It sometimes is useful to reduce the clutter to get down to the essence of the problem. Assuming somebody is still interested in this topic please consider the following code:

It is helpful to read what the Java Language Specification says about converting a byte to a char:
byte to char
First, the byte is converted to an int via widening primitive conversion, and then the resulting int is converted to a char by narrowing primitive conversion.


I hope the comments within the code explains what's going on.


(updated code - relative to b4)

[ October 07, 2006: Message edited by: Barry Gaunt ]
[ October 07, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q how does the above code give error and the code below doesn't???