How to fix the error "Cannot invoke the compareTo() on primitive type int" with Integer.compare(x,y)
Coding River
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.compare(double x , double y) (Fix error : Cannot invoke the compareTo(int) Java In this video, we will talk about how fix this error “Cannot invoke the compareTo(int). I guess that you are getting this error because you want to compare two integers or two decimal numbers. So, the problem is with the method you are using. Instead of applying the compareTo(), you rather have to use the Integer.compare(int x , int y) method if the comparison is between two integer or the Double.compare(double x , double y) The rule for these methods is the same if the two numbers are equal the method will return 0. If the first number is less than the one in the brackets the method will return -1 If the first number is superior to the one in the brackets the method will return 1 public class Test {
public static void main(String args[]) { Integer x = 5;
System.out.println(x.compareTo(3));
System.out.println(x.compareTo(5));
System.out.println(x.compareTo(8));
} }
#codingriver #java #programming ... https://www.youtube.com/watch?v=YTCYrNDeo7E
11315906 Bytes