• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RolesOfCompiler

 
rafeeque mohd abdul
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am confuse about datatypes like.
if I declare byte b=12;
then byte c=b+1;
then compiler give lose of precision error
but if I say
byte b=12;
then byte c=++b;
then it will compile with out error.
then please tell me with is the role beside this. I have more doubt like this.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write b+1 it means ((int)b) + 1 and the result of that is an int. That's because the literal 1 is of type int. You need then to cast the resultant int to a byte.

When you write ++b the result is still a byte, so no casting is required.

These things should be described in any elementary Java book - try the the free one "Thinking in Java" from Bruce Eckel for example.
[ November 21, 2006: Message edited by: Barry Gaunt ]
 
rafeeque mohd abdul
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Then that means all lettral are (int) by default.means 1 to ...... all are int.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rafeeque mohd abdul:
Hi,

Then that means all lettral are (int) by default.means 1 to ...... all are int.


Yes, even the 12 in the code is an int.

This is also interesting:


See if you can find out what happens here.
 
rafeeque mohd abdul
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

byte b = 12;
byte c = (b += 1);

I think byte c=b=(byte)b+1;


Is correct???
Thanks for answering.
 
rafeeque mohd abdul
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I know there is one role if you done any arithemtic on byte,short and char it is automatically converted/updated to int.Please tell me is it correct.
 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
When performing binary options(such as +,-,*,/) on data types which have size less than that of int datatype, it converts them to int(concept of widening) and performs the binary operation. Now the result has size of int. Because of this you cannot store the value in datatype less than the size of int resulting in a loss of precision error.

In the case of increment/decrement operators it will perform the increment operation and castes that result to the data type in which it should fit into. Because of this id doesn't raise any error of loss of precision.

Regards,
Ravindranath.
 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is correct, this is the way the compiler witll interpret it.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost. The compiler does:



It takes b and converts it to an int. Then it adds 1. Then it casts back to a byte, which it then assigns to b again and then assigns to c.
 
rafeeque mohd abdul
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravindarnath,
Is it means compiler do automatic cast in case of increment and decrement.
 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the compiler will do automatic casting in the case of inc/dec operations.

Thanks,
Ravindranath.
 
rafeeque mohd abdul
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then why compiler not casting like you told.To my code which is
byte b=12;
byte c=b+1;
why compiler not doing byte c=(byte)b+1;// automatically
because it is casting automatically in byte c=(b +=1);
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rafeeque mohd abdul:
Then why compiler not casting like you told.To my code which is
byte b=12;
byte c=b+1;
why compiler not doing byte c=(byte)b+1;// automatically
because it is casting automatically in byte c=(b +=1);


That's because you are doing the incrementing, where as in b+=1 it is the compiler.

It's just the way Java works - so we have to accept these rules.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also do this:
 
Joe Harry
Ranch Hand
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry,

It's just the way Java works - so we have to accept these rules.


That is 100% true.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic