• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

(again) Implicit conversion question

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 public class As{
2 int i = 10;
3 int j;
4 char z= 1;
5 boolean b;
6 public static void main(String argv[]){
7 As a = new As();
8 a.amethod(); }
9 public void amethod(){
10 System.out.println(j);
11 System.out.println(b);
12 }
3 }

1) Compilation succeeds and at run time an output of 0 and false
2) Compilation succeeds and at run time an output of 0 and true
3) Compile time error b is not initialised
4) Compile time error z must be assigned a char value
The answer is 1, but shouldn't be answer as 4 'coz we are assigning int to char in line 4.
I compiled this and i am getting results 0 and false.
Any comments?
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer you are looking for is this ...
Upon initialization of variables, the compiler tries to fit a literal value into the type. If the value is too big (i.e., byte b = 1000 ;), then it spits out an error about needing an explicit cast. If the value fits (i.e., byte b = 10 ;), then it will not complain. Same thing for char types, only the compiler tries to fit the literal value into a 16-bit unsigned integer range - if it doesn't fit, then it will throw an error.
Interestingly, if you try to do this: char a = -2, the compiler will also complain - even though -2 falls within the 16-bit unsigned integer range. I guess it encounters the negative sign and fails to convert the literal from signed to unsigned.
Regards,
SP
[This message has been edited by Stephen Pride (edited September 22, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't it violates whatever written that from conversion from int to char we need Explicit conversion. It's quite confusing from exam point of view.
Thanks for reply
Ashish
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Implicit narrowing primitive conversion on assignment CAN occur in cases where the source is an INT CONSTANT EXPRESSION whose value can be determined to be in the range on the destination type at compile time where the destintation type is byte, short or char. If the value is not known at compile time or is not an int expression, then a cast is required.
char is not signed! So -2 requires a cast because it is a loss of precision (the sign.)
Remember, the type of a number constant is int unless it has a decimal then it is a double. So.....
byte b = 127; // int constant 127, in range
short s = b; // widen ok
byte b1 = s; // not int, not constant, not ok
int i = 100;
byte b2 = i; // int, but not constant, not ok
final int i2 = 100;
byte b3 = i2; // int and constant and in range! ok!
final int i3 = 128;
byte b4 = i3; // int and constant NOT in range! not ok
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks scott, it clears my doubt!!!
thanks again
Ashish
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic