I'd like to add a method AddDefaultNamespace()
to the String class in Java so that I can type "myString".AddDefaultNamespace()
instead of DEFAULTNAMESPACE + "myString"
, to obtain something like "MyDefaultNameSpace.myString"
. I don't want to add another derived class either (PrefixedString
for example).
Maybe the approach is not good for you but I personally hate using +
. But, anyway, is it possible to add new methods to the String class in Java?
Thanks and regards.
Remember that.. The dot ( . ) is used to access the object's attributes and methods. To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon ( ; ). A class must have a matching filename ( Main and Main.
Strings are a sequence of characters and are widely used in Java programming. In the Java programming language, strings are objects. The String class has over 60 methods and 13 constructors.
What is Overloading? For convenience, Java allows you to write more than one method in the same class definition with the same name. For example, you can have two methods in ShoppingCart class named computeCost. Having two or more methods named the same in the same class is called overloading.
So because of this String class not having reverse() method. No. One could transfer all characters to a char[] array, reverse the array, then create a String from the resulting array. No intermediade String objects required.
String
is a final class which means it cannot be extended to work on your own implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With