Reputation:
Hi I have this method which I created. Its job is to receive an Integer A, which can either be 10 or 30. It is meant to return TRUE if the value is ten, and false otherwise.
public static boolean checkStatus(int a){
if(a.equals(10)){
return true;
}
return false;
}
For some reason I am getting a compilation error in the if(a.equals(10)) condition, which says INT CANNOT BE DEREFERNCED. If I'm not mistaken, isn't the .equals() method the way to go in this circumstance?
Thanks for your help!
Upvotes: 1
Views: 455
Reputation: 29140
equals
is used for non-primitives basically for Objects.
==
is used for primitives.
So, you can use it
public static boolean checkStatus (int a) {
if (a == 10)
return true;
return false;
}
Example 1: For object, if equals method are overridden, then "equals" method will return true.
public class Employee {
int id;
@Override
public boolean equals(Object obj) {
Employee e = (Employee) obj;
return id == e.id;
}
Employee(int id) {
this.id = id;
}
public static void main(String[] args) {
Employee e1 = new Employee(5);
Employee e2 = new Employee(5);
System.out.println("e1.equals(e2) is: " + e1.equals(e2));
System.out.println("(e1 == e2) is: " + (e1 == e2));
}
}
Output:
e1.equals(e2) is: true
(e1 == e2) is: false
Example 2: For object, if equals method are not overridden, then "equals" method works as "=="
public class Employee {
int id;
Employee(int id) {
this.id = id;
}
public static void main(String[] args) {
Employee e1 = new Employee(5);
Employee e2 = new Employee(5);
System.out.println("e1.equals(e2) is: " + e1.equals(e2));
System.out.println("(e1 == e2) is: " + (e1 == e2));
}
}
Output:
e1.equals(e2) is: false
(e1 == e2) is: false
Here "equals" method works as "==". So, don't forget to override the equals method for object.
Upvotes: 1
Reputation: 29
You can of course wrap the integer up as :
Integer i = new Integer(a);
Then the equals function can be used with 'i', the new Integer object.
Upvotes: 0
Reputation: 881303
You can use equals
for objects but an int
is a primitive type (a), rather than an object.
Hence you need something like:
public static boolean checkStatus (int a) {
if (a == 10)
return true;
return false;
}
or the shorter and more sensible (in this case):
public static boolean checkStatus (int a) {
return (a == 10);
}
(a) The purists will argue this is proof that Java is not really an object-oriented language, but that's because they're raving loonies :-)
Upvotes: 5
Reputation: 1046
You can do something like this with Integer Class
Integer x = 5;
Integer y = 10;
Integer z =5;
Short a = 5;
System.out.println(x.equals(y));
System.out.println(x.equals(z));
System.out.println(x.equals(a));
Output:
false
true
false
Upvotes: 0
Reputation: 5758
int is a primitive in Java and primitives does not have behaviours a.k.a methods.
hence you cannot call .equals
on int
. So the options here are to use a ==
public static boolean checkStatus(Integer a){
return (a==10);
}
or convert the int
to Integer which is a wrapper class
public static boolean checkStatus(Integer a){
return a.equals(10);
}
Upvotes: 0
Reputation: 35557
equals()
method belongs to Object
class of Java
and it has to override
each and every Object classes like String, Integer and MyObject(implemented class)
. But int
is not a Java Object
and there is no equals()
method there.
you can just use ==
with int values and you can simplify your code as bellow.
public static boolean checkStatus(int a){
return a==10;
}
Upvotes: 1
Reputation: 4361
You can use
public static boolean checkStatus(int a){
if(a==10){
return true;
}
return false;
}
or
public static boolean checkStatus(Integer a){
if(a.equals(new Integer(10))){
return true;
}
return false;
}
Upvotes: 1
Reputation: 10810
Primitives in Java (int
, long
, float
, etc..) don't have member methods, so the call
if (a.equals(10))
will not compile, as you're trying to de-reference a primitive. Instead, you want to use the ==
operator to compare primitive values:
if (a == 10)
and reserve the use of the equals()
method for non-primitive Objects
Upvotes: 8