Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

(Comparable)obj ?

Posted on 2006-05-02
9
Medium Priority
?
419 Views
Last Modified: 2010-03-31
Can  anyone tell me what exactly happens in the following statement (Comparable)obj  and why do you need to do this exactly.  Does casting an object to comparable actually makes the object implement the interface if it doesnt already? or is it only used to ensure the compiler that the object implements it? if that case is true, will you get a compile error?
0
Comment
Question by:yattias
7 Comments
 
LVL 3

Accepted Solution

by:
arun_kuttz earned 128 total points
ID: 16593588
> why do you need to do this exactly.
'obj' might be a refernce of type other than Comparable... so it needs to be cast to Comparable

>  Does casting an object to comparable actually makes the object implement the interface if it doesnt already?
NO

> will you get a compile error?
No... you will get a ClassCastException if the class of 'obj' does not implement Comparable

-KuTtZ

0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 124 total points
ID: 16593595
It means that you are type-casting something into a Comparable reference. For example, maybe you have:

Object o = "abc" ; // Object reference referring to a String, which is Comparable
Comparable c = ( Comparable ) o ; // you are type-casting 'o' so that you can hold it in a Comparable reference and hence use its methods
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16593605
For example, if you have:

public class YourClass implements Comparable
{
  public int compareTo ( Object o )
  {
    Comparable c = ( Comparable ) o ; // type-cast it to a Comparable reference 'c', now with 'c' you can use its methods
    return c.compareTo ( this ) ; // just an example
  }
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Assisted Solution

by:gauravkrtomar
gauravkrtomar earned 124 total points
ID: 16593797
0
 
LVL 20

Assisted Solution

by:Venabili
Venabili earned 124 total points
ID: 16595993
:) I would strongly advice for
try{

}catch (ClassCastException e){
//do something
}
structure in any cast ( except when you are SURE That it will work -- when you need to give an object to a class and so on -- however I believe I always cast with the try/catch... just in case something is wrong.).
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16596017
Or you can try:

YourClass c = null ;

if ( o instanceof YourClass )
  c = ( YourClass ) o ;

if ( c != null )
  return ( this.someProperty == c.someProperty ) ; // just as an example

>> return c.compareTo ( this ) ; // just an example

- will actually be an infinite loop :) anyway, don't blame me, it was just an example ;-)
0
 
LVL 20

Expert Comment

by:Venabili
ID: 16596075
Yeah. instanceof is also a way to go :) I thought someone already posted it but apparently I had read another thread.. ;)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month20 days, 22 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question