This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# String

meeta gaur
Ranch Hand
Posts: 305

0
4
8

my guess was 0 2 4

don't understand difference

Matthew Brown
Bartender
Posts: 4568
9
Why do you think there's a '1' character at position 2?

meeta gaur
Ranch Hand
Posts: 305
Matthew Brown wrote:Why do you think there's a '1' character at position 2?

Henry Wong
author
Marshal
Posts: 21778
85
• 1
meeta gaur wrote:
Matthew Brown wrote:Why do you think there's a '1' character at position 2?

Nothing to elaborate. Why do you think that there is a "1" at position 2? Everyone else looking at your code sees a "2" at position 2. Perhaps you are referring to another code example?

Henry

Mansukhdeep Thind
Ranch Hand
Posts: 1158
Or let me put it like this:

Is there a difference between

and

?

Or are they the same?

Chetan Sama
Greenhorn
Posts: 8
String s = "1+2" creates only 1 string which contains 1+2. Not 3, but 1+2 in string format.

String s = "1" + "2" creates three strings. One string is 1. second string is 2. Third string is the concatenation of the first and second string literal i.e 12.

To get in depth knowledge of how this works, you can study string pool and concatenation from any java book.

meeta gaur
Ranch Hand
Posts: 305
Henry Wong wrote:
meeta gaur wrote:
Matthew Brown wrote:Why do you think there's a '1' character at position 2?

Nothing to elaborate. Why do you think that there is a "1" at position 2? Everyone else looking at your code sees a "2" at position 2. Perhaps you are referring to another code example?

Henry

Oh sorry you are correct i have another same program with little changes, i was executing that and posting this another code.thanks

Henry Wong
author
Marshal
Posts: 21778
85
Chetan Sama wrote:String s = "1+2" creates only 1 string which contains 1+2. Not 3, but 1+2 in string format.

I think that Mansukhdeep was asking the question rhetorically, so that the OP can figure out the problem herself.

Chetan Sama wrote:
String s = "1" + "2" creates three strings. One string is 1. second string is 2. Third string is the concatenation of the first and second string literal i.e 12.

To get in depth knowledge of how this works, you can study string pool and concatenation from any java book.

Actually, no. You forgot about the compile time constant optimizations. Since "1" + "2" is a concatenation of two compile time constants, which in turn, is another compile time constant, the compiler just puts "12" in the string pool. Only one string is created.

Henry

Mansukhdeep Thind
Ranch Hand
Posts: 1158
Henry Wong wrote:
Chetan Sama wrote:String s = "1+2" creates only 1 string which contains 1+2. Not 3, but 1+2 in string format.

I think that Mansukhdeep was asking the question rhetorically, so that the OP can figure out the problem herself.

Henry

Correct Henry. I wanted her to figure it out for herself. That way she will remember it.

Shashank Gollapudi
Ranch Hand
Posts: 33
• 1
meeta gaur wrote:

0
4
8

my guess was 0 2 4

don't understand difference

As per the program, you are trying to find out the position of '1' using indexof function, but you are not counting '+' as valid character in a string, See here is how it goes to store a total of ten characters

CHaracter 1 + 2 + 1 + 3 + 1 + 4
Location: 0 1 2 3 4 5 6 7 8 9 10

Now i think you understand why the output is 0 4 8 ??