Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes (again) Implicit conversion question 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 "(again) Implicit conversion question" Watch "(again) Implicit conversion question" New topic

(again) Implicit conversion question

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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?
Stephen Pride
Ranch Hand

Joined: Sep 14, 2000
Posts: 121
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.
[This message has been edited by Stephen Pride (edited September 22, 2000).]

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
scott irwin
Ranch Hand

Joined: Aug 07, 2000
Posts: 87
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
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
thanks scott, it clears my doubt!!!
thanks again
I agree. Here's the link:
subject: (again) Implicit conversion question
It's not a secret anymore!