Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question (implicit conversion)

 
Yuva Raj Sah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the following code



Which gives compilation error which is understood.

but if I change the code to



It compiles and the output is true. But I was expecting the same error as in previous code. Please can somebody explain !!!

regards,
Yuvaraj

(Thread title changed to be more informative)
[ May 24, 2004: Message edited by: Barry Gaunt ]
 
meeta verma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class EBH012 {
public static void main (String[] args) {
byte x = 3;
byte y = ~x + 1; //1
System.out.print(-x == y); //2
}}

The problem here is in statement 1 where ~x+1 results in an integer and you try to assign in a byte and so the compiler gives an error.
statement 2 is fine perfectly. You can compare a byte with an int etc.

Even I was confused with this and posted a topic on "Comparison and equality operators" a couple of days ago.
The compiler will widen the smaller operand , in this case byte into an int and check for equality.
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your second example the compiler will promote both operands to an int numeric type.

In comparison with the first example there is no assignment but only a numeric comparison after promotion.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The commented out assignment failed because the result of the righthand side is an int, and the lefthand side requires a byte.

For the == comparison, the righthand side is an int too. Also the lefthand side, the -x, evaluates to an int. So we are comparing an int with an int and that compiles without error.
 
Wendal Park
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any bitwise operations (~, ^, |, &) will automatically convert the operands into int type prior performing the operation. When done, int type will always be returned.

Special care is needed here to determine if an explicit type casting is needed.
 
victor pereira
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I declare line 4 like the following

class EBH012 { //1
public static void main (String[] args) { //3
byte x = 3; //3
int y = ~x + 1; //4
System.out.print(-x == y);
}
}

The code compiles and run fine.But I do not understand the result will be "true" .Could some one tell me please
 
Baps Vakkalagadda
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, for any given number -ve number is generated using 2's complement method. In 2's complement method, initially each bit of the given number is inverted and then 1 is added to the result which gives the -ve number of the given number.

For example,


~ is an inversion operator.

-ve number of any number can be represented as ~x+1.
So, -x = ~x+1. In your case y is nothing but -x.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic