Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Programmer Certification (OCPJP)
datatypes
Andre Fean
Greenhorn
Posts: 4
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
Let's say we have following code:
int i1=1,i2=2,iResult; short s1=1,s2=2,sResult; long l1=1,l2=2,lResult; byte b1=1,b2=2,bResult; iResult=i1+i2; // ok lResult=l1+l2; // ok sResult=s1+s2; // compiler error, ok would be: sResult=(short)(s1+s2); bResult=b1+b2; // compiler error, ok would be: lResult=(byte)(b1+b2);
Why is it working for int and long and why I have to do a typecast for short and byte?
Thanks & regards
Andre
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The results of arithmetic operations are automatically promoted to int in these examples so you have to cast them back to byte or short.
Bill
Andre Fean
Greenhorn
Posts: 4
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
But that means it makes no sense to use for example a short in such a case.
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
S2.equals(7) is false, but (S2 == 7) is true. Short S2 = new Short((short)7); Why?
Conditional Operator with different return types
Doubt about primitives
Doubt in Devaka's Final Exam Question.
Boxing int-Literal
More...