| Author |
Short Wrapper Class Question
|
Bob Nedwor
hangman
Ranch Hand
Joined: Aug 17, 2005
Posts: 215
|
|
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>
|
Bob N
SCJP - 1.4
SCJD - (B&S) Used 1.5 And It Runs On Solaris10
SCWCD - Thanks to HFSJ!!
|
 |
naraharirao mocherla
Ranch Hand
Joined: Aug 16, 2005
Posts: 45
|
|
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
Joined: Aug 16, 2005
Posts: 45
|
|
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
Joined: Aug 16, 2005
Posts: 45
|
|
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
Joined: Aug 17, 2005
Posts: 215
|
|
|
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.
|
 |
 |
|
|
subject: Short Wrapper Class Question
|
|
|