• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can we create our own String class in java?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

There is a String class in Java. But, I want to create my own String class, then how can I create our own String class?
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Besides having a class called 'String'\, what are your requirements?
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.String is just like a regular class, with just a few exceptions. These include special compiler support (using String literals, using + to concatenate), the String pool and its very frequent use in the core API (toString anybody?).
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String is a final class and you cannnot extend it and override it method.
So it is possible if you Create all the methods of Strings class as yourself
 
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
As already said, you can just create your own class that behaves like String.

What is not possible though is to replace the existing java.lang.String with your own String class, and make everything in the standard library etc. use your own String class instead of the standard one.
 
Ranch Hand
Posts: 231
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Creating a class called "String" is the easy part, making it behave like java.lang.String will be a challenge.

What are your requirements?

Not sure how you'd create a String-a-like, anyone have any ideas? Possibly do something with character arrays
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
implement CharSequence is probably the best you can hope for.
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Elsey wrote:Not sure how you'd create a String-a-like, anyone have any ideas? Possibly do something with character arrays


Bingo. That's exactly how java.lang.String (currently) does it. It has three fields for this:
- a char[] to store the characters
- an int that marks the index in the array of the first character
- an int that marks the number of characters

One single char[] would be enough. However, because String is immutable, several methods of String that return a new String actually return a String object that shares the same char[]. The following are snippets of code from String's source (as found in src.zip in the JDK folder):
As you can see, substring uses the package-private constructor to create a String object that shares the same char[], just with a different offset and length.

Note that this is only done for memory and speed improvements. They could have used the following constructor for that, that makes a defensive copy of the char[]:
You could then also drop the offset and count fields completely as offset would always be 0 and count would always be value.length.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic