0

I am a newbie to Java. I wrote the following program and got the output below.

OUTPUT:

1

a out=

x=0 1 test..

x=1 1 gotcha..

If strings are immutable, how come I can apparently change the value of "thisCycle" from "0" to "1" and it prints out the new string value? Why do I get 2 lines of output in the "for" loop? Why does the first string have 2 periods appended to it?

Thanks

import java.util.ArrayList;


public class Main {

    /**
     * @param args
     */
    private static final ArrayList<String> ListContents=new ArrayList<String>();

    public static void main(String[] args) {
         // TODO Auto-generated method stub
         String thisCycle="Cycle";
         thisCycle="0";
        // advance to next cycle
         if (thisCycle.equals("0")) thisCycle="1";
        System.out.println(thisCycle);
        //
        String a1="test";
        String a2="gotcha";
        ListContents.add(a1);
        ListContents.add(a2);
        StringBuilder a_out=new StringBuilder("");
        System.out.println("a out="+a_out);
        for(int x=0;ListContents.size()>x;x++) {
            a_out.delete(0, a_out.length()+1);
            if (thisCycle.equals("0")) a_out.append(ListContents.get(x)).append(".     ");
            if (thisCycle.equals("1")) a_out.append(ListContents.get(x)).append("..    ");          
            System.out.println("x="+x+" "+thisCycle+" "+a_out);
        }
    }

}
0

3 Answers 3

1

Strings themselves are immutable, but you're changing the String value of a variable, not the String itself. That's an important difference - everything in Java is done by reference.

You got two lines of output because the loop executes twice, because you have two elements in ListContents.

Sign up to request clarification or add additional context in comments.

1 Comment

but the loop has 2 if stmts. only one of the if stmts can be true, correct?
0

String(s) are immutable. You're changing the reference.

String thisCycle="Cycle";
thisCycle="0"; // <-- changes the thisCycle reference.

To prevent that, mark the String as final.

final String thisCycle="Cycle";
thisCycle="0"; // <-- compiler error. Can't change a final reference.

You get two lines of output because your ListContents contains two String(s). Finally, you get the two periods because this

if (thisCycle.equals("0")) thisCycle="1"; // <-- changes "0" to "1"

5 Comments

i get the "final" idea. somewhat. but, don't i don't see why i get 2 lines of output. how is the variable equal to 2 things at once? even if i changed the reference, then the old reference should be gone, right?
@user2475400 if (thisCycle.equals("1")) a_out.append(ListContents.get(x)).append(".. "); is why you get two periods. You get two lines because of for(int x=0;ListContents.size()>x;x++) {
but, i have 2 if stmts. how is "thisCycle" equal to 0 and 1 at the same time? and, the 1st if has append with only one "." but prints out "..".
Because thisCycle isn't equal to 0. thisCycle is equal to 1. And that appends two periods.
yes. thisCycle is equal to 1. and, when equal to 1, it appends two periods. exactly my intention. then, the first if stmt shouldn't do anything, right? i shouldn't get this output: "x=0 1 test.. " i should only get "x=1 1 gotcha.."
0

Yes, this takes a little bit of thought to get your head round.

Think of it like this. A variable name is a bit like a label that you can put on a box; the contents of the variable is whatever's in the box. An immutable object means that once you've got the box and put something in it, the box is then sealed: you can look inside it, but you can't change the contents.

What you're doing in your code is changing what your String variable is pointing to; in other words, rather than changing the contents of the box, you're getting a new box, putting the modified contents in there, and the moving the label onto the new box. The old box contains exactly what it always did (it's immutable); but the label is now on the new box, and any attempt to access that variable will result in looking in the new box.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.