This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on primitive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on primitive" Watch "Question on primitive" New topic
Author

Question on primitive

Thiyagarajan Kamala
Ranch Hand

Joined: Mar 16, 2003
Posts: 30
short s1=6;
s1+=20000;

if I rewrite this to
short s1=6;
s1=s1+20000;

Compile complains ...Can someone explain?.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
This will work...


The problem is that without the cast you try to assign an int to a short, which will make the compiler choke.

Using the short assignment (+=) I think the compiler does that for you, but I'm not 100% sure.


JDBCSupport - An easy to use, light-weight JDBC framework -
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Read this...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Thiyagarajan Kamala
Ranch Hand

Joined: Mar 16, 2003
Posts: 30
Thanks Ankit.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Sebastian was also right, so thanks to him too ...
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Ankit Garg wrote:Sebastian was also right, so thanks to him too ...


sweet
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Sebastian Janisch wrote:sweet


Sugar .......... Ooops, I forgot this is not the word-association thread
Thiyagarajan Kamala
Ranch Hand

Joined: Mar 16, 2003
Posts: 30
my bad ...Thanks to both of you...If i get this question in exam I will remember both of you.
 
Consider Paul's rocket mass heater.
 
subject: Question on primitive
 
Similar Threads
Primitive casting
Wrappers
overloading example from examlab
wrapper
SCJP 1.5, Chapter 6 question 4