Java if () doesn't work

When I run some code (shown below), I tell him to check if the string is == "1", if it is, output "It 1!", Otherwise output the string.

code:

double shirt3 = Math.random() * 9;
String shirt2 = Double.toString(shirt3);
char shirt1 = shirt2.charAt(0);
String shirt = Character.toString(shirt1);

if(shirt == "1") {
    System.out.println("It 1!");
} else {
    System.out.println(shirt);
}

      

Output:

7
4
8
1
7
1
7
7
6
0

+3


source to share


6 answers


You need to use

if (shirt.equals("1"))
    ...

      



This will compare the actual content of String objects, not their IDs.

+9


source


You made a Java beginner mistake when using ==

to test for string equality. Use the method instead equals

.

It takes longer to explain that a statement ==

in Java checks if two object references are identical; that is, it checks if the left and right operands are the same object. But you have two different strings that contain the same sequence of characters. They are "equal" strings, but not the same object.



As a general rule, you should always use equals

string comparisons.

(There are situations where it ==

will work, but you really need to understand what you are doing to be sure. And its just not worth the effort / risk in the vast majority of use cases.)

+5


source


To check if two strings are the same in Java use .equals()

:

"1" == new String("1") //returns false
"1".equals(new String("1")) //returns true

      

EDIT: Added a new String ("1") so that we talk about a new line.

+4


source


Also, you need to use equals method to compare strings in java

// it't allways a good idea to use constant.equals(...)
// to avoid possible NullPointerExceptions
if ("1".equals(shirt))
    ...

      

in your case you don't need to convert character to string you can also compare individual characters. The same, without creating an additional String object and you don't need to deal with the equals method.

if (shirt1 == '1')
        ...

      

+3


source


Use equals to compare strings.

if(shirt.equals("1"))
     System.out.println("It 1!");
    }else{
        System.out.println(shirt);
    }

      

+2


source


A more general rule of thumb is not to make the code more complex than necessary.

int shirt = (int)(Math.random() * 10); // shirt numbers from 0 to 9.
if(shirt == 1) 
    System.out.println("It 1!");
else 
    System.out.println(shirt);

      

This illustrates what ==

can be used to compare primitives. It can also be used to compare links, but not to compare the contents of objects.

Double d = 0.1;
Double e = 0.1;
System.out.println("(Double) 0.1 == (Double) 0.1 is " + (d == e));

double x = 0.1;
double y = 0.1;
System.out.println("0.1 == 0.1 is " + (x == y));

      

prints

(Double) 0.1 == (Double) 0.1 is false
0.1 == 0.1 is true

      

This shows that when comparing Double

such as strings, objects ==

do not compare content.

One confusion about all of this is when the cache is used, as is the case with String literals. This means that values ​​that are referenced in different places actually use the same object for performance reasons.

Integer d = 10;
Integer e = 10;
System.out.println("(Integer) 10 == (Integer) 10 is " + (d == e));

int x = 10;
int y = 10;
System.out.println("10 == 10 is " + (x == y));

      

prints

(Integer) 10 == (Integer) 10 is true
10 == 10 is true

      

The first example works because Java 5.0+ uses a cache of small integers. (Small integer size varies depending on command line options:})

Integer d = -129;
Integer e = -129;
System.out.println("(Integer) -129 == (Integer) -129 is " + (d == e));

int x = -129;
int y = -129;
System.out.println("-129 == -129 is " + (x == y));

      

prints

(Integer) -129 == (Integer) -129 is false
-129 == -129 is true

      

As far as strings are concerned, string literal cache is used. The compiler will also simplify constant expressions so that strings written in a different way may be the same.

final int one = 1;
int oneB = 1;
String a = "1";
String b = "" + 1;
String c = "" + one;
String d = "" + oneB;

System.out.println(a == b);
System.out.println(a == c);
System.out.println(a == d);

      

prints

true
true
false

      

The content of each line is the same, but oneB

it is not constant, so the expression is evaluated at run time and another line is created.

IMHO: Java is trying to hide the details from developers and it would be a better choice to make a ==

call equals

, whereas you could have an operator ===

if you really wanted to compare the actual links.

+1


source







All Articles