File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


0
4
8

my guess was 0 2 4

don't understand difference
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think there's a '1' character at position 2?
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Why do you think there's a '1' character at position 2?


please elaborate little more.thanks
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or let me put it like this:

Is there a difference between



and

?

Or are they the same?
 
Chetan Sama
Greenhorn
Posts: 8
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
Notepad Tomcat Server Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic