• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Short Wrapper Class Question

 
Bob Nedwor
hangman
Ranch Hand
Posts: 215
Eclipse IDE Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me with this if you can. When I try to create a Short wrapper object using the "new" operation, I need quotes around the input value or else it won't compile (I get the "cannot resolve symbol" error). This makes no sense to me because the Integer wrapper class does not require this. Aren't both Short and Integer subclasses of java.lang.Number ??

Thanks for any help explaining this.
--Bob

C:\bobs_javas>type shortTestQuotes.java
class shortTestQuotes {
public static void main (String args[]) {
Integer i2 = new Integer (1);
Short s2 = new Short ("1");
}
}
C:\bobs_javas>javac shortTestQuotes.java

C:\bobs_javas>type shortTestNoQuotes.java
class shortTestNoQuotes {
public static void main (String args[]) {
Integer i2 = new Integer (1);
Short s2 = new Short (1);
}
}
C:\bobs_javas>javac shortTestNoQuotes.java
shortTestNoQuotes.java:4: cannot resolve symbol
symbol : constructor Short (int)
location: class java.lang.Short
Short s2 = new Short (1);
^
1 error

C:\bobs_javas>
 
naraharirao mocherla
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Short constructor will take only short values not int values...
if u declare short s= 1; and then pass it to the Short constructor it will compile.
as

short s=1;// primitive short
Short s2= new Short(s);// compile

Short se- new Short((short)1);//compile cast int to short
 
naraharirao mocherla
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Bob

Short constructor will take only short values not int values...(ofcourse it will accept strings)..
if u declare short s= 1; and then pass it to the Short constructor it will compile.,but when u pass int value directly it wont compile.
as

short s=1;// primitive short
Short s2= new Short(s);// compile

Short se- new Short((short)1);//compile cast int to short
 
naraharirao mocherla
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i've typed it wrong.
The third statement is Short s2 =new Short((short)1);// works fine..bcoz we are casting it to int.this is also the case with Byte Wrapper class..
 
Bob Nedwor
hangman
Ranch Hand
Posts: 215
Eclipse IDE Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, Thanks, Naraharirao. That also explains my next problem. I am learning that integers and shorts are more different from each other than I originally thought.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic