When you're learning, you need to have a pen and paper handy in order to understand the concepts. It takes a little practice to do this is your head and even after that people need to write things down in order to keep track of things when the problem is more complex in nature.
Here you ask two questions. I will answer the second question first
Quote:
what does "" do is it null ?
"" does not mean null.
string tricky = "";
Just means that an empty string is assigned to the variable tricky.
Hence tricky.length() will result in zero. Whereas,
string tricky;
means that the variable tricky has not been assigned a value yet (in other words tricky is null). Hence tricky.length() will result in a null pointer exception because you cannot call methods on null variables.
This answers your second question.
Now for your first question.
The for loop that is written in the method "mystery" is directed to run from when "i" is 0 till "i" reaches a value which is one less than the length of the "input", incrementing the value of "i" by one each time.
For a better understanding of loops in Java you can head over
here. Now that is established we can go ahead and try to figure out what happens when you call the method with input "abcd".
Here input.length() is 4, hence the loop will execute till the test condition i < input.length() fails i.e. i reaches the value 4.
For the first iteration value of i is 0, for which the test condition is true,
so the statement inside the loop executes.
Hence input.charAt(4-0-1) results to input.charAt(3), and since the indexing of any string starts with 0, index of "a" = 0, "b" = 1, c = "2" and d = "3" in the string "abcd" so the charAt(3) will result in "d".
The next statement concatenates this to tricky which is "" resulting in "d", next it assigns the result to tricky changing its value from "" to "d", and lastly increments the value of "i" making it 1.
The loop again goes to the test condition which comes out to be true and the process is repeated, only this time the values if "i" and "tricky" are 1 and "d" respectively.
Hence, input.charAt(4-1-1) = input.chatAt(2) = "c", next concatenation with tricky
"c" + "d" = "cd" and assign this value to tricky. Lastly, increment the value of "i" by 1 making it 2.
Test the condition again 2 < 4 = true, loop executes again
i = 2, tricky = "cd"
input.charAt(4-2-1) = input.charAt(1) = "b", concatenate to tricky "b" + "cd" = "bcd", assign this value to tricky, and increment "i" by 1 making it 3.
Test the condition again 3 < 4 = true, loop executes again
i = 3, tricky = "bcd"
input.charAt(4-3-1) = input.charAt(0) = "a", concatenate to tricky "a" + "bcd" = "abcd", assign this value to tricky, and increment "i" by 1 making it 4.
Test the condition again 4 < 4 = false, loop terminates.
The next statement in line for execution is,
return tricky;
which returns the value of tricky which is "abcd".
There you go, whew.