This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to assign some numeric values like 1 ... to a short variable ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to assign some numeric values like 1 ... to a short variable ?" Watch "how to assign some numeric values like 1 ... to a short variable ?" New topic

how to assign some numeric values like 1 ... to a short variable ?

raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Thank you for reading my post
I have a short value like :

somewhere in my code i tried to assign some values like

but it shows an error which say , you can not assing an int to a short variable

Also i tried to use java.lang.Short class like :

and it said that there is no constructor which can take an int value.

Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Try casting 0 to a short. ie a = (short)0 or initializing it on the same line that you declared a.

Short only has 2 constructors:

Short(short value) and Short(String s). So either cast it to a short or pass it as a String if you wish to use this wrapper class.

I realize you just gave a simple example, but try to get in the habit of using meaningful names for variables. It helps self-document your code.
[ April 12, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
marc weber

Joined: Aug 31, 2004
Posts: 11343

short a;
a = 0;

...should work, because you're assigning a literal value that's within range.

However, the Short constructor does require an explicit cast with an int literal...

Short s = new Short((short)10);

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Your code will work since it assigns a short to a constant value.
You might be referring to assigning to some non-constant value.

This will not compile because 'b' is not a constant.
Here, b is a constant:

...therefore, the code will compile.
JLS 15.28 2e defines a constant - it may have changed with the third edition.

A constant is not to be confused with a final

Tony Morris
Java Q&A (FAQ, Trivia)
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
look just declare an int like...

int i = 0;

then do

short r = (short)i;

that should solve your question....just remember you can always downcast...

or maybe is would be better so say "upcast" like from an int to a float, or short, or long..

but you cant down from a float to an int...


You down with OOP? Yeah you know me!
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: how to assign some numeric values like 1 ... to a short variable ?
It's not a secret anymore!