• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

type casting error!

 
yuvaraj KumarAmudhan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
yuvaraj KumarAmudhan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you rob! but this only is correct.
byte t=(byte) (firstNum +secondNum);
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D'OH!
You're right of course, the cast should have been to byte all along.
 
Deepakk Verma
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49842
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
soory for the misteake......it was 127+127
and was getting -2.....
please try once more
 
Campbell Ritchie
Sheriff
Pie
Posts: 49842
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
127 + 127 = -2 Easy. Just write out the bits and you will see how it works.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49842
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49842
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology accepted. Please read this FAQ.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic