How to compare string with received String in one.jsp?

I have an html page and it sends a form with post type method, I take the data like this:

String email =  request.getParameter("user");

When I compare the email string with another string containing the same text it turns me false. Why does it happen ? how can I solve ?

I'm doing the normal comparison

if(email == "[email protected]"){
    out.println("executou aqui");
}

In case "[email protected]" it would be the same value of the String I picked up in the post method.

Author: Davi Henrique, 2018-03-12

1 answers

When you use ==, you test if two objects are identical, look at the example:

String texto1 = "Mundo genial";
String texto2 = "Mundo genial";

In the above case if you compare with == you will receive the desired value, but since you are bringing this information, it is the same as doing this:

String s1 = new String("Mundo genial");
String s2 = new String("Mundo genial");

In this case you are comparing content and the comparison has to be done with equals,

boolean comparar = s1.equals(s2); // resultado = true
boolean comparar = s1 == s2; // resultado = false

If you want to continue with the = = you would have to somehow point to the same object, something like like this:

String s1 = new String("Mundo genial");
String s2 = s1;

In the above case if you do the comparison s1 = = s2, the value would be true as they are pointing to the same object.

 1
Author: Edjane, 2018-03-12 21:55:37