It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparable interface question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Comparable interface question" Watch "Comparable interface question" New topic
Author

Comparable interface question

Fedry Kemilau
Ranch Hand

Joined: Aug 06, 2007
Posts: 44


Compiler says that "int cannot be dereferenced". Anyone can help me to explain this?

Thank you.
[ August 10, 2007: Message edited by: fedry kemilau ]

Solve this code:<br /> <br />10010101100001111011011000010100000111001011011111100011110101111100110100001
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hey,

Java is not pure object oriented;
behold there are primitive types(int,char,long..etc).

But every primitive can be represented as an Object using wrapper classes.
like , (Integer,Long..).

Though 1.5 JDK uses Autoboxing, It's recommended to write readable code. like this

Integer size = 3;
public void compareTo(Object){

---
---
return size.compareTo(_ _ _ _ );
}
[ August 11, 2007: Message edited by: Srinivasan thoyyeti ]

Thanks & Regards,<br />T.Srinivasan,<br />SCWCD 1.4(89%),SCJP 5.0(75%)<br />"That service is the noblest which is rendered for its own sake." - Mahatma Gandhi
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
At line #1 you attempt to invoke compareTo() using primitive variable size which as you know is incorrect. Methods must be invoked using object refrence variables if they are instance methods or using class names if they are static methods.
return size.compareTo(sizeToCompare)
Thanks
Deepak


SCJP, SCWCD, SCBCD
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by fedry kemilau:
Compiler says that "int cannot be dereferenced"
int is a primitive type, and as such it has no methods.
Fedry Kemilau
Ranch Hand

Joined: Aug 06, 2007
Posts: 44
owh ic, my bad. I did not realize the argument types.
thank you all.
Seems like I have to download java APIs
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Comparable interface question
 
Similar Threads
Discussing errata for K&B, SCJP 6
TreeSet Question
Casting to Interface
Java Rule Round Up: Question 80
Doubt about Trees Implementations