*
The moose likes Beginning Java and the fly likes Short initialize 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 » Java » Beginning Java
Bookmark "Short initialize" Watch "Short initialize" New topic
Author

Short initialize

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I want to use a Short:


I must declare it explicitly:



Is there a way to init it implicitly within code?

Something like:


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Have you tried it?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
This works:



but this works not:



I need Short.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38081
    
  22
There is a bit about boxing and unboxing here. Read that and see whether it provides what you want.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
You can't cast a primitive to an object.
There is no such thing as a short literal, so you must cast an int/long literal to a short - i.e. (short)1

Read the link above on auto[un]boxing to understand how Java 5 added conversions to/from primitives/objects in very specific conditions.

If you want to be specific (and verbose) about what you're doing:

will get done what you want.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Originally posted by Bill Shirley:
You can't cast a primitive to an object.

Actually you can since Java 5 with autoboxing. For instance, the following is perfectly legal:

This is because 5 gets autoboxed to an Integer, which can be implicitly cast to Object without a problem. Of course there are limitations. For instance, widening then boxing won't work:


If you want to be specific (and verbose) about what you're doing:

will get done what you want.

I'd choose Short.valueOf instead of new Short, because it uses caching for all numbers between -128 and 127 (inclusive). The same applies to Byte, Integer and Long.
[ October 09, 2008: Message edited by: Rob Prime ]
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
I think Bill meant that you can't explicitly cast from a primitive type to a reference type. In your examples, Rob, the change from primitive to object is always effected by means of an automatic boxing conversion, not an explicit cast like (Short)1.

What I wonder, nimo frey, is why you're using a Short in the first place. As a general rule, you should only use the primitive wrapper types (Short, Integer, Boolean, etc.) when you have to. Using primitives (short, int, boolean, etc.) whenever possible will tend to make your code more efficient, more readable, and less buggy. If you need to use the value in a place where an object is required, like storing it in a List, autoboxing will convert it for you at that time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Short initialize
 
Similar Threads
Autoboxing AutoUnboxing problem
Long s = 1 vs Short s = 1 - compiler error
to get a short from a string
Short Constructor (Dan's Q)
Adding short values