Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting object

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i run the following code snippet..



I get the output 579 with no exception. Shouldn't there have been a cast inside the for loop? (line 14)
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The signature of parseInt is -

So, it returns int.

Regards,
Dan
 
Amey Ambulgekar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O. Ziggy wrote:When i run the following code snippet..



I get the output 579 with no exception. Shouldn't there have been a cast inside the for loop? (line 14)


hello O. Ziggy,

may be according to my knowledge at "public static int add(Object... values)" here Object mentioned means in Java everything is referred to with respect to Object Class that is why you did not get any error or exception

and you are making object of Object Class so it will automatically get cast, cause Object is Parents of all classes in Java..
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amey Ambulgekar wrote:
O. Ziggy wrote:When i run the following code snippet..



I get the output 579 with no exception. Shouldn't there have been a cast inside the for loop? (line 14)


hello O. Ziggy,

may be according to my knowledge at "public static int add(Object... values)" here Object mentioned means in Java everything is referred to with respect to Object Class that is why you did not get any error or exception

and you are making object of Object Class so it will automatically get cast, cause Object is Parents of all classes in Java..


Is a caste not required to be able to use the toString() method in the Integer wrapper object.
 
Stephan van Hulst
Bartender
Posts: 6337
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, because the toString() method is defined in the Object class.

Also, be careful to *never* do something like new String("123"). This will create a new object every time the expression is evaluated. Just use the String literal directly.

And a nitpick: You can't cast objects. You cast references to objects.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic