"Cannot invoke compareTo(int) on the primitive type int"

You have to understand: the primitive type int isn't a reference type like Integer. You can't call methods on primitive values. When you want to compare two ints, you can simply do index < newNumber for example.

Alternatively, you could use that static method compare of the Integer class, like: Integer.compare(index, newValue). But as said: you don't need methods to compare primitive values, you can directly do that "in place".

Note: in other languages, like Kotlin, there is no such difference. There you only have Int objects, and call methods on those. But java makes that distinction. So study it, and learn how to use which approach.

Answer from GhostCat on Stack Overflow
🌐
Reddit
reddit.com › r/javahelp › cant use compareto() here. what am i doing wrong?
r/javahelp on Reddit: cant use compareTo() here. what am i doing wrong?
April 18, 2022 -

Everytime i try to use compareTo() in this. it just says "cannot invoke compareTo(int) on the primitive type int" can someone help me out here. what am i doing wrong?

``public int compareTo(Shape s) {

	this.getAreaInt().compareTo(s.getAreaInt());

}



protected abstract String getName();

protected abstract double getArea();

protected abstract int getAreaInt();

protected abstract double getPerimeter();

}

``

Top answer
1 of 4
4
getAreaInt() returns a primitive int. You can't call methods on a primitive. Since it returns a primitive though you can just compare with boolean operators (i.e. ==, >, <, etc)
2 of 4
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Narkive
programming.comp.narkive.com › tvMMSRFP › java-cannot-invoke-compareto-int-on-the-primitive-type-int
Java : Cannot invoke compareTo(int) on the primitive type int?
compareTo is a method of a class. Primitive data types aren't classes and have no methods you can call. You have to encapsulate your int in an instance of the class java.lang.Integer import java.lang.*; /* ...
🌐
YouTube
youtube.com › watch
How to fix the error "Cannot invoke the compareTo() on primitive type int" with Integer.compare(x,y) - YouTube
How to fix the error Cannot invoke the compareTo(int) on primitive type int using Integer.compare(int x , int y)Integer.compare(int x , int y) / Double.compa...
Published   May 14, 2020
🌐
The Coding Forums
thecodingforums.com › archive › archive › java
compareTo() for primitive types? | Java | Coding Forums
February 11, 2006 - But then again, compareTo gives you an int too, so this is exactly what you want. Only for floating point numbers would you have to watch out, because float1-float2 is not always 0, even when they have the ‘same’ value (or the inverse).
Find elsewhere
🌐
SlideShare
slideshare.net › home › education › java comparable interface video tutorial
Java Comparable Interface Video Tutorial | PPTX
Cannot invoke compareTo(int) on the primitive type int However, this not quite the whole story, because weight is not an object, but a primitive int value, which does not provide a compareTo method.
🌐
Reddit
reddit.com › r/javahelp › primitive type int?
r/javahelp on Reddit: Primitive type int?
June 30, 2022 -

I do not understand why sometimes you can use .toString on an integer and other times it will say something along the lines of “cannot invoke .tostring on the primitive type int”. I was able to use toString all the time in class

🌐
CodingTechRoom
codingtechroom.com › question › fix-compareto-error-cannot-invoke-compareto-int
How to Fix the `compareTo` Error: Cannot Invoke `compareTo(int)` on Primitive Type `int` - CodingTechRoom
The error 'Cannot invoke compareTo(int) on primitive type int' occurs in Java when trying to call the method compareTo() on a primitive type, which is not a valid operation since compareTo() is a method of the Integer wrapper class, not the primitive int type.
🌐
Chegg
chegg.com › homework-help › questions-and-answers › file-sortingjava-contains-sorting-class-lecture-slides-class-implements-selection-sort-ins-q19651496
Solved: The File Sorting.java Contains The Sorting Class F... | Chegg.com
1. Error occurs is "cannot invoke compareTo() on primitive type int" because since int is a pimitive type it does not have have any methods,only objects does. 2. to overcome this prob...
🌐
Chegg
chegg.com › engineering › computer science › computer science questions and answers › can i have some help fixing some errors in my java code. 1. public filechar() { ch = ' '; //error invalid character constant } 2. public int hashcode() { return ch.hashcode(); //error cannot invoke hashcode on primitive type char } 3. public int compareto(filechar char) { return ch.equals(char.ch); //error cannot invoke equals(char) on
Solved Can I have some help fixing some errors in my JAVA | Chegg.com
June 24, 2017 - return ch.hashCode(); //error cannot invoke hashCode on primitive type char · } 3. public int compareTo(FileChar Char) { return ch.equals(Char.ch); //error cannot invoke equals(Char) on primitive type char · } 4. public int compareTo(HuffElement he) { return chCount.equals(he.chCount); //error cannot invoke equals(int) on primitive type int ·
🌐
Stack Overflow
stackoverflow.com › questions › tagged › compareto
Frequent 'compareto' Questions - Stack Overflow
I need to write a compareTo method that will compare the last name and then the first name ... ... Is it better to write int primitive1 = 3, primitive2 = 4; Integer a = new Integer(primitive1); Integer b = new Integer(primitive2); int compare = a.compareTo(b); or int primitive1 = 3, primitive2 =...
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8304487
[JDK-8304487] Compiler Implementation for Primitive types in patterns, instanceof, and switch (Preview) - Java Bug System
Minor feedback, for the JLS changes: "P is a floating-point type and x and y are equal according to the result obtained as if by execution of Double.compare(x, y). P is java.math.BigDecimal and x and y are equal according to the result obtained as if by execution of x.compareTo(y)." Pedantically, the compare/compareTo method calls return an int value rather than a boolean; a more precise expression would be along the lines of Double.compare(x, y) == 0 etc.
🌐
Lxadm
lxadm.com › cannot-invoke-compareto-int-on-the-primitive-type-int
Cannot Invoke Compareto(int) On The Primitive Type Int (Resolved)
April 3, 2023 - In this guide, we showed you how to fix the "Cannot Invoke CompareTo(int) on the Primitive Type Int" error in Java. You can either convert the int values to Integer objects using the valueOf() method or use the Integer.compare() method to compare the int values directly.
🌐
IBM
ibm.com › support › pages › apar › PK38711
PK38711: COMPILATION ERROR "CANNOT INVOKE EQUALS(INT) ON THE PRIMITIVE TYPE" WHEN GENERATING WEB SERVICE SKELETON
Environment: - OS (name, language, version or SP): Microsoft Windows XP SP2 - Product name (Build ID or version/patch level): IBM Rational Application Developer 6.0.1.1 iFix003a IBM Websphere 6.0.2.17 Reproducible Y/N: Y Description of problem: When generating a Web Service, the following compilation errors are raised on some generated deserializers: 1) Cannot invoke equals(int) on the primitive type 2) e cannot be resolved 3) Syntax error, insert ") Statement" to complete IfStatement