• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Wrapper arguments

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Please consider the following code.


I am getting a runtime exception on line 1 ,2 and 3.


C:\exercises\JavaExercises>java a

Exception in thread "main" java.lang.NumberFormatException: For input string: "0x1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:456)
at java.lang.Short.parseShort(Short.java:120)
at java.lang.Short.<init>(Short.java:331)
at a.main(a.java:15)


Can somebody please explain?
What are all the legal arguments for all the Wrapper objects?
Are signs allowed?

Thanks in advance.
Uttara Rishi.

[ March 10, 2008: Message edited by: Uttara Rishi ]
[ March 10, 2008: Message edited by: Uttara Rishi ]
 
Uttara Rishi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

I have another doubt regarding autoboxing. Please consider the following code.




The above code compiles while the following one doesnt.

I am getting a compiler error on line 1. Can somebody please explain?

Thanks in advance.

Uttara Rishi.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
0x1 only works when the jvm autoboxes the value.


will work.

I can only guess that the JVM internally parses the value to a 'short' and then calls the Short(short value) constructor.

Other than that, i'm sure you can find out the default constructors from the docs. Docs
 
Gaurav Arora
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Uttara Rishi:
Hi Ranchers,
The above code compiles while the following one doesnt.

I am getting a compiler error on line 1. Can somebody please explain?

Thanks in advance.

Uttara Rishi.



Compiles for me, check your compiler compliance level and try to compile manually using the -source switch.
 
Uttara Rishi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah it compiles for me too. Sorry .


Uttara Rishi.
 
Uttara Rishi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have one more doubt.


Why are we getting a compiler error and .equals output as false.
What goes on in the equals method?

Can somebody please explain?

Thanks in advance.

Uttara Rishi.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If you are trying to compare two objects then there will be no autoboxing happening. This is because of a wise decision by Sun that the code originally written without using autoboxing should not be affected.

That's the reason compiler gives an error when you try to compare the two objects which are incomparable.

Hope this helps!

Regards.
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey in the question Uttarishi had asked..
which he thought wasnt compiling...

Can we compare an object and a primitive variable with == operator.
 
Rohit Garg
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java 1.5, yes we can compare an object and a primitive
 
Rohit Garg
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but we can't compare a non wrapper with a primitive
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Uttara Rishi,

Integer as = 303;
long lo=303l;
System.out.println(as==lo); // Line 1

The compiler will throw error in line 1 as compiler cannot unbox Integer and Type case it to Long at the same time.

Integer as = 30;
Long lo=30L;
System.out.println("==" +(as==lo)); Line 1// compiler error
System.out.println(".equals" + (as.equals(lo))); // False
will not compile as well as Integer and Long are two different classes Although both classes extends the Number Classs you cannot equate them using ==.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav


Integer as = 303;
long lo=303l;
System.out.println(as==lo); // Line 1

The compiler will throw error in line 1 as compiler cannot unbox Integer and Type case it to Long at the same time.



Have you tried to compile the above code? There is no issue with the above code and it compiles without any errors and prints o/p: true

Murali...
 
Uttara Rishi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rohit. That helped.



Murali wrote....
Integer as = 303;
long lo=303l;
System.out.println(as==lo); // Line 1

The compiler will throw error in line 1 as compiler cannot unbox Integer and Type case it to Long at the same time.

Have you tried to compile the above code? There is no issue with the above code and it compiles without any errors and prints o/p: true



Here we are talking about Integer and Long objects not the primitive long.
Hope that cleared it .


Uttara Rishi.
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic