Take the 2-minute tour ×

In the following code, the block under if(timesout[entry] == "exit") will never execute. I have verified timesout[entry] for the current loop is set as "exit" in debugging mode, as well as by printing out the variable before the if statement is evaluated, but no matter what, the block never executes when I enter exit at the prompt, and I am stumped as to why.

import java.util.Scanner;

public class timetracker {
public static void main(String args[]) {
    boolean exit = false;
    String[] reasons = new String[30];
    String[] timesout = new String[30];
    String[] timesin = new String[30];
    int entry = 0;
    Scanner keyinput = new Scanner(System.in);

    while(exit == false) {
        //record info

        System.out.println("Enter time out:");
        timesout[entry] = keyinput.nextLine();

        if(timesout[entry] == "exit") {
            exit = true;
            break recordloop;   

        System.out.println("Enter reason:");
        reasons[entry] = keyinput.nextLine();
        System.out.println("Enter time in:");
        timesin[entry] = keyinput.nextLine();

        entry = entry + 1;


    System.out.println("Times away from phone:\n ----- \n");
    int count = entry;
    entry = entry + 1;

    while(count < entry) {
        System.out.println(reasons[count] + ": " + timesout[count] + " - " + timesin[count] + "\n");
        count = count + 1;
share|improve this question
for string always use .equals() to compare. –  Dinup Kandel Sep 3 '12 at 5:48
possible duplicate of How do I compare strings in Java? –  Makoto Sep 3 '12 at 5:48
The Java tag wiki has a small collection of common issues. Comparing strings is one of many common issues that Java programmers come across. –  Makoto Sep 3 '12 at 5:48

3 Answers 3

up vote 8 down vote accepted
timesout[entry] == "exit"

use equals() to compare String, == compares reference equality


share|improve this answer
Wow. I'm a c# programmer so I'd have struggled with this too. So does this imply that java doesn't use immutable strings? Don't worry, I'll google it ;-) –  Sepster Sep 3 '12 at 5:49
java does, but it doesn't cache all the String in pool, so its better to use equals() to compare the Object equality –  Jigar Joshi Sep 3 '12 at 5:51
Ah yes, a quick google search revealed that too, thanks. It's strange that comparing a string variable against a string literal doesn't cause a compiler warning, then? I mean, it's perfectly clear what the programmer is trying to do (and I can't think of a scenario where I'd want to check if my variable references the same string that was created for me in "the back end" to represent my literal). –  Sepster Sep 3 '12 at 5:54
Thank you! I'm a PHP Programmer by nature so I wasn't sure what was going on here. –  Morgan Sep 3 '12 at 5:58
@Morgan you are welcome –  Jigar Joshi Sep 3 '12 at 5:59

Instead of

if(timesout[entry] == "exit") 





more information different between == and equals()

share|improve this answer

Can you try


instead of

if(timesout[entry] == "exit")

As @Jigar Joshi pointed you should see the difference and meaning of == and equals()

share|improve this answer

Your Answer


