I am making an archive for my family. There are no syntax errors, however whenever I type in "Maaz"
, it evaluates realName == "Maaz"
to false and goes to the else
statement.
import java.util.Scanner; public class MainFamily { public static void main (String [] args) { System.out.println("Enter you're name here"); Scanner name = new Scanner(System.in);//Scanner variable = name String realName; realName = name.nextLine();//String variable = user input System.out.println("Name: "+ realName); if (realName == "Maaz") { System.out.println("Name: Maaz"); } else { System.out.println("This person is not in the database"); } } }
In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .
You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.
This is because the two Integer objects have the same value, but they are not the same object.
Using the “==” operator for comparing text values is one of the most common mistakes Java beginners make. This is incorrect because “==” only checks the referential equality of two Strings, meaning if they reference the same object or not.
You wrote (this doesn't work):
realName == "Maaz"
You meant this:
realname.equals("Maaz")
or this:
realname.equalsIgnoreCase("Maaz")
In Java (and many other Object-Oriented programming languages), an object is not the same as a data-type. Data-types are recognized by the runtime as a data-type.
Examples of data-types include: int, float, short.
There are no methods or properties associated with a data-type. For example, this would throw an error, because data-types aren't objects:
int x = 5; int y = 5; if (x.equals(y)) { System.out.println("Equal"); }
A reference is basically a chunk of memory that explicitly tells the runtime environment what that data-block is. The runtime doesn't know how to interpret this; it assumes that the programmer does.
For example, if we used Integer instead of int in the previous example, this would work:
Integer x = new Integer(5); Integer y = new Integer(5); if (x.equals(y)) { System.out.println("Equal"); }
Whereas this would not give the expected result (the if condition would evaluate to false):
Integer x = new Integer(5); Integer y = new Integer(5); if (x == y) { System.out.println("Equal"); }
This is because the two Integer objects have the same value, but they are not the same object. The double equals basically checks to see if the two Objects are the same reference (which has its uses).
In your code, you are comparing an Object with a String literal (also an object), which is not the same as comparing the values of both.
Let's look at another example:
String s = "Some string"; if (s == "Some string") { System.out.println("Equal"); }
In this instance, the if block will probably evaluate to true. Why is this?
The compiler is optimized to use as little extra memory as is reasonable, although what that means depends on the implementation (and possibly runtime environment).
The String literal, "Some string"
, in the first line will probably be recognized as equivalent to the String literal in the second line, and will use the same place in memory for each. In simple terms, it will create a String object and plug it into both instances of "Some string". This cannot be relied upon, so using String.equals is always a better method of checking equivalence if you're only concerned with the values.
do this instead
if (realName.equals("Maaz"))
equals()
should be used on all non-primitive objects, such as String in this case
'==
' should only be used when doing primitive comparisons, such as int
and long
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With