Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Short Wrapper Class Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Short Wrapper Class Question" Watch "Short Wrapper Class Question" New topic
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Short Wrapper Class Question
 
Similar Threads
Where can I learn SIMPLE explanation
"static class Inner construction"question required
cannot resolve symbol
Adding array to List
:rolling: Innovative functions !!!