• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

"Short " - Why does this happen?

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,
Short s = new Short(4);//not ok???#$$
Incompatible type for constructor. Explicit cast needed to convert int to short. Short s = new Short(4);
isnt the above code valid

but when I say
short st = 4;
and then pass
Short s = new Short(st); // this is ok
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vivek!
I'll try to put some dark... oops, I wanted to say some light.
Short s = new Short(4);
"4" here is a literal of int type, and short = int considered narrowing conversion, which is never performed during method invocation
short s = 4; is a
special case
.
 
Vivek Nambiar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mapraputa,
But then how do u create a new object of Short with out the usual object creation code of Short s = new Short();
Are there any other ways?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the following -

short i = 4 ;
Short s = new Short(i) ;
OR
Short s = new Short("4") ;

Ajith
 
Vivek Nambiar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ajit,
Agreed on ur point, but doesnt Short class recognise that the the value passed has to be the one related to short value, like the foll:
Double i = new Double(1277);
Float k = new Float(2f);
Integer = new Integer(4);
In all these cases the value is recognised without explicitly putting the values in "". I mean all these classes recognise it implicitly the values passed, but why it diff for Short?
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek,
By default(without using L, F or D) integral leterals are type int and floating point literals are type double.
Double i = new Double(1277);
Here the argument is taken as an int and is promoted to type double.
Float k = new Float(2f);
The letter 'f' explicitly specifies that the value is a float.
Integer = new Integer(4);
By default 4 is int and hence this works.
Hope this helps.
Vani
 
Vivek Nambiar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vani,
Yes I got the point clearly!
 
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!