Question:
Identify an example of method overloading in the String class, and explain its purpose. JAVA HELP!!!?
Cor
2012-05-13 06:59:50 UTC
I know what overloading is when I write a constructor but not sure about this.
Four answers:
Silent
2012-05-13 07:15:01 UTC
Overloading is the same concept for other methods as for constructors: multiple methods with the same name, but different number or type of parameters.



So, to answer this question, take a look at the official documentation for the String class, which lists all its methods (see link below). Find some overloaded methods (methods with the same name) and explain what they do and why they're overloaded.
Connor
2012-05-13 07:56:34 UTC
Overloading is making methods/constructors with different parameters but same name ! and one of most common Method Overloading in the String Class is :

STRING.substring(int);

STRING.substring(int,int);





STRING.valueOf(int);

STRING.valueOf(double);

STRING.valueOf(float);

STRING.valueOf(long);

and many more of valueOf method !



here , STRING refers to your String name !



Overloading helps us to manipulate the Method with whatever info we have like and may even perform different tasks or same tasks differently like this :

in SUBSTRING methods :

First one takes only one argument and the String is splitted form that index !

and in the second one , there are 2 argument where the string is splitted between those two indices !

Hope this helps :)
anonymous
2016-05-17 13:23:38 UTC
Seems like a pretty straightforward assignment. What are you stuck on? Have you looked at the official documentation for the String class? See the link below.
lovekumars82
2012-05-13 07:49:10 UTC
As you already know overloading concept, we need overloading in String class for applying string operations on different data types. Very common example is converting anything to string,



.toString(int)

.toString(decimal)

.toString(long)



all these overloaded method converts your input parameter to string and accepts different datatypes.



tostring() is one operation. Similarly any other string operation like .tolowercase(), .touppercase().

.tolowercase(char) and .tolowercase(string) are two overloaded methods doing same thing but can accept both char and string.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...