File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting object 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 "Casting object" Watch "Casting object" New topic
Author

Casting object

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

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

Joined: Jul 09, 2001
Posts: 1180
The signature of parseInt is -

So, it returns int.

Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Amey Ambulgekar
Ranch Hand

Joined: Nov 22, 2011
Posts: 36
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

Joined: Oct 02, 2005
Posts: 430

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

Joined: Sep 20, 2010
Posts: 3647
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting object