Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

setLength() ??

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will be the result of attempting to compile and run the following program?

Options:1)It will print5
2)It will print 10
3)It will print 8
4)Compilation error
5)None of the above
Ans :2) It will print 10
I would like to know why didnt it print 5.
as sb.setLength is called twice why did the main preferred only the second method?
Sonir
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second call is the last setLength() method called so this will define the length of the StringBuffer.
if you place your println statement after the first call setting it to (5) you will see that it prints out '5'.
Teh sb object retains the value set by the last call to its setLength() method.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
more interesting question would be what it will print after two statements like,
sb.setLength(5); sb.setLength(10);
S.o.p(sb);
it puts all ascii zero after length 5 when we set length to 5 and so when we set len to be 10 again it wont give u all previous chars but ascii zero.
regards
maulin.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is S.o.p() BTW?

Originally posted by Maulin, Vasavada:
Hi,
more interesting question would be what it will print after two statements like,
sb.setLength(5); sb.setLength(10);
S.o.p(sb);
it puts all ascii zero after length 5 when we set length to 5 and so when we set len to be 10 again it wont give u all previous chars but ascii zero.
regards
maulin.

 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println().
Saves some finger pain!!
Rob
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Maulin, Vasavada:
Hi,
more interesting question would be what it will print after two statements like,
sb.setLength(5); sb.setLength(10);
S.o.p(sb);
it puts all ascii zero after length 5 when we set length to 5 and so when we set len to be 10 again it wont give u all previous chars but ascii zero.
regards
maulin.


What do you mean by ascii zeros? Should we be able to see those when we print the output. Output from the following code doesn't seem to generate zeros. It's like the firs set to setLength() chopped off the last places in the String Buffer but it doesn't seem to replace them with anything that I can see...

gives output of:
12345678
12345
12345
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rick Reumann:

gives output of:
12345678
12345
12345


I think they mean print the length() you'll see the " ".
ie. System.out.println(sb.length() );
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
ascii zero is 'no char'. u wont see anything. i didnt say just zero. it is ascii zero which is refered as NULL in C and termination of char pointers in C.
regards
maulin.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
if u wonna put it other way rest of the len is 'padded'.
regards
maulin.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic