Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2005

Tip: Looking for answers? Try searching our database.

Question about Object

Thread view: 
Jim Davis - 12 Mar 2005 03:15 GMT
If a method takes an Object parameter, can this parameter be type casted by
any class?

Example:
public int compareTo(Object o) {
   Name n = (Name) o;
}

public int MultiplyPrint(Object o) {
   String testString = (String) o;
   Int num = Integer.parseInt(testString);
   int theResult = Math.pow(num,2);
   return theResult;
}

public Orange Apple2Orange(Object o) {
  Apple apple = (Apple) o;
  // Convert to Orange
 Orange orange = (Orange) apple;
 return Orange;
}

Also, is it poor programming style to rely on type casting?

TIA

James Davis
Dan Nuttle - 12 Mar 2005 03:54 GMT
You can TRY to cast an Object to another type, but of course, if the Object
isn't an instance of that type, you'll get a ClassCastException.  For
instance, if the Object is actually a String, and you try to cast it as an
OutputStream, it won't work.

Whether or not it's poor programming to cast a type all depends on the
situation.  I don't think there's a single simple answer to that question.

> If a method takes an Object parameter, can this parameter be type casted by
> any class?
[quoted text clipped - 23 lines]
>
> James Davis


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.