aspose file tools*
The moose likes Beginning Java and the fly likes String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String" Watch "String" New topic
Author

String

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305



0
4
8

my guess was 0 2 4

don't understand difference


My one regret in life is that I am not someone else.
- Woody Allen
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

Why do you think there's a '1' character at position 2?
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Matthew Brown wrote:Why do you think there's a '1' character at position 2?


please elaborate little more.thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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


please elaborate little more.thanks



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

Joined: Jul 27, 2010
Posts: 1157

Or let me put it like this:

Is there a difference between



and

?

Or are they the same?


~ Mansukh
Chetan Sama
Greenhorn

Joined: Aug 12, 2012
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.
Hope that answers your question.
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

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


please elaborate little more.thanks



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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Jan 19, 2012
Posts: 33
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 ??
 
 
subject: String