• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

StringBuffer and String Confused!!

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote this piece of code


Till line no.2 i understood the output, but line no.3 output i didnt understand how the output is rattbFoar.
pls someone explain me
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Asha,




From where you found output. I wrote code I got output "atto". That was I also answered.


It looks like you are taking "Java.Inquisition" mock exam. I am also taking that only. I have just posted my doubt about "Drag & Drop" question. If you know can you please help me. I am little stuck over there. I appreciate your help.
 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dolly,

I got the output by executing on my machine..yes even i am using the same mock exam.Even i thought the output should have been "atto".
But after executing this code i found that
String s7 = sb.toString().substring(1, 5);
gives magically this output "rattbFoar", i wanted to know how toString() function performs.

Asha
 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Dolly if i know the answer to your question i can surely help you out
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by asha ganapathy:
Hi Dolly,

I got the output by executing on my machine..yes even i am using the same mock exam.Even i thought the output should have been "atto".
But after executing this code i found that
String s7 = sb.toString().substring(1, 5);
gives magically this output "rattbFoar", i wanted to know how toString() function performs.

Asha


Hello Asha, are you really sure that you got "rattbFoar"? Because I tried running it and got "atto". If you look at the call: substring(1, 5) will give you a string of 4 characters. So the output can never be "rattbFoar".

So check your code - probably your test program is not exactly the same as the code above, or you have an old version of a class file somewhere, etc.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i wanted to know how toString() function performs.



toString()converts StringBuilder or StringBuffer in String.
 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh i guess it was running with an older version of .class file
Thanks a lot
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by asha ganapathy:
oh i guess it was running with an older version of .class file



I doubt that would cause a failure as described.



javac 1.5.0_12

java version "1.5.0_12"

In general, toString often constructs a new String reference.

But not always:

[asha ganapathy:] ... wanted to know how toString() function performs.



Try not to guess unless you are investigating borderline conditions or doing intentionally exploratory work.



re:


One more clarification required..what is equals() method used for?
s1.equals(s2) -> checks if both Strings have same value or not?
please correct me if i am wrong.


posted Sunday, August 05, 2007 8:07 PM in Java (beginner) Strings
t = 023603 (topic)
f=33 (forum)

In general, == should be used to test for equality of primitives,

use dot equals for everything else .... there is no substitute for testing.

[ August 30, 2007: Message edited by: Nicholas Jordan ]
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic