aspose file tools*
The moose likes Beginning Java and the fly likes type casting error! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "type casting error!" Watch "type casting error!" New topic
Author

type casting error!

yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110

public class TestReturn {
public static void main(String[] args) {

Adder adder = new Adder();
byte y= adder.addAndReturn((byte)1, (byte)2); ---------> arguments casted and passing to the function
System.out.println("Added value is"+y);

}
}

class Adder{
byte addAndReturn(byte firstNum, byte secondNum){
byte t = firstNum + secondNum; -------------> this statement
return (t);
}
}

why it is showing cannot convert int to byte?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

When you add, subtract, multiply, divide etc any two variables, it will take the largest type of those variables. For instance, long + int => long. However, it will never ever be byte, short or char; byte + byte still results in an int!


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
but i have casted and then only passing to the function. i used only byte not integer and all. but it is showing integer conversion error.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Like I said, byte + byte results into an int. Therefore, firstNum + secondNum returns an int. You need to cast that again:
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
thank you rob! but this only is correct.
byte t=(byte) (firstNum +secondNum);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

D'OH!
You're right of course, the cast should have been to byte all along.
Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
if i change this code to



then why is it showing me result as -2
point to be noted is: byte range is -128 to 127
so what it should do on line 14 is add the two number
127+127 i.e, 254
there will be no changes in the value since 127 is in the range of byte
now it will do type casting and will display the reduced modulo(the remainder of an integer division by the) byte's range(which i guess is 127)

and the result should be

254/127=2 with remainder =0
thus 0 should be the result but it is showing me -2 ?........Please explain
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
You are not adding 127 and 128 at all. Remember 128 is outwith the range for a byte. Your arithmetic is

0111_1111 = 127
1000_0000 = -128
1111_1111

I can't get -2 from your arithmetic. When I ran your class I got a completely different answer.
Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
soory for the misteake......it was 127+127
and was getting -2.....
please try once more
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
127 + 127 = -2 Easy. Just write out the bits and you will see how it works.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
You have gone back and altered the previous post. It said (byte) 128 and (byte) 127 before and you have changed the 128 to 127.

That makes the whole discussion nonsense.
Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
i know that and i also apologized for it
actually i gave the 128 value by mistake in that post so please dont be annoyed
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Apology accepted. Please read this FAQ.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: type casting error!