This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes RolesOfCompiler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "RolesOfCompiler" Watch "RolesOfCompiler" New topic
Author

RolesOfCompiler

rafeeque mohd abdul
Greenhorn

Joined: Apr 23, 2006
Posts: 13
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

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

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
rafeeque mohd abdul
Greenhorn

Joined: Apr 23, 2006
Posts: 13
Hi,

Then that means all lettral are (int) by default.means 1 to ...... all are int.
Barry Gaunt
Ranch Hand

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

Joined: Apr 23, 2006
Posts: 13
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

Joined: Apr 23, 2006
Posts: 13
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

Joined: Nov 08, 2006
Posts: 71
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

Joined: May 21, 2006
Posts: 146
Yes it is correct, this is the way the compiler witll interpret it.
Barry Gaunt
Ranch Hand

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

Joined: Apr 23, 2006
Posts: 13
Hi Ravindarnath,
Is it means compiler do automatic cast in case of increment and decrement.
Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
Yes, the compiler will do automatic casting in the case of inc/dec operations.

Thanks,
Ravindranath.
rafeeque mohd abdul
Greenhorn

Joined: Apr 23, 2006
Posts: 13
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

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

Joined: Aug 03, 2002
Posts: 7729
You can also do this:
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Originally posted by Barry,

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


That is 100% true.


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RolesOfCompiler
 
Similar Threads
operator question
Confused in Assignment
javacaps mock 2 Q:25
Operators Question
type casting