Java class that extends String (or similar)

This is mainly out of curiosity. How would you (if you can) create a class that mimics String. You cannot extend String as final , so it’s not necessary, but purely for the percent factor, I was thinking of creating an object that could be literally initialized ( String s = "string"; ).

Is it possible? If so, how? If not, why?

+6
source share
2 answers

No, it is not possible to submit your own literals in Java. The language specification provides syntax for numeric, character, and string literals and what it is.

You cannot even add your own implicit conversions from an existing type with a literal representation to your own types (as you can in C # - as an example of a "close relative") in Java.

+7
source

The string is final for security reasons.

However, with Java 1.4, String was an implementation of the CharSequence interface. If you write your code in terms of CharSequences, you can run it against any implementation of this interface, including String.

+3
source

Source: https://habr.com/ru/post/986569/


All Articles