• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
}
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
James Quinton
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this thread,

https://coderanch.com/t/259303/java-programmer-SCJP/certification/primitive-type-char
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic