File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question (implicit conversion)  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 "Question (implicit conversion)  " Watch "Question (implicit conversion)  " New topic

Question (implicit conversion)

Yuva Raj Sah

Joined: Apr 28, 2004
Posts: 6
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 !!!


(Thread title changed to be more informative)
[ May 24, 2004: Message edited by: Barry Gaunt ]
meeta verma

Joined: May 15, 2004
Posts: 19
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

Joined: Apr 12, 2003
Posts: 425
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.

Try your free <a href="" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Wendal Park
Ranch Hand

Joined: Aug 28, 2001
Posts: 39
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.

MSc, BSc, SCJP 1.4<br /> <img src="graemlins/banghead.gif" border="0" alt="[banghead]" /> SCBCD
victor pereira

Joined: Mar 10, 2004
Posts: 3
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

Joined: May 25, 2004
Posts: 47
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.
I agree. Here's the link:
subject: Question (implicit conversion)
jQuery in Action, 3rd edition