• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

String and StringBuilder

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a question about String and StringBuilder.

The task is:
Assuming that the following method will always be called with a phone number in the format ddd-ddd-dddd (where d stands for a digit), what can be inserted at //1 so that it will return a String containing the same number except its last four digits will be masked with xxxx?




one of the correct answers is: return new StringBuilder(fullPhoneNumber).substring(0, 8)+"xxxx".

My question is: is it always ok to return StringBuilder if in the method the return type is String?

Thanks,

Tonya
 
Marshal
Posts: 70636
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tonya Hales wrote:. . . is it always ok to return StringBuilder if in the method the return type is String? . . .

No, it is never possible to return StringBuilder from a method declared to return String. You would manage it if the return type were CharSequence or similar. But you have forgotten two things:-
  • 1: The return type of StringBuilder#substring() is String.
  • 2: One of the operands for the String concatenation operator + is a String already ("xxxx"). That means it can take any operand on the other side and turn it into a String and the whole expression is of type String. That means anything except a method call returning void.
  •  
    Saloon Keeper
    Posts: 22649
    153
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The idea behind StringBuilder (and its older sibbling StringBuffer) is to provide a low-overhead place to assemble a string from bits and pieces. You would then decant the assembled String.

    There's no point in constructing a StringBuilder if you've already assembled the string to begin with!
     
    Tonya Hales
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot:))
     
    Campbell Ritchie
    Marshal
    Posts: 70636
    288
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But this is a cert exam revision question; it doesn't need a point.
     
    Get out of my mind! Look! A tiny ad!
    the value of filler advertising in 2020
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic