. Advertisement .
..3..
. Advertisement .
..4..
The string is among the most frequently used classes in Java. Thus, it is essential for you, as a beginning coder, to learn and understand different string functionalities to use them properly.
Continuing with the Java series, today we give a tutorial on how to return a string in Java.
Check it now!
What Does “Return A String” Mean?
A return is a value returned by calling a script or function after it is done. The value is either of four variable types: string, object, integer, or handle – depending on the data types specified in the declaration.
The string data type we discuss in this article is a string or a sequence of elements, usually connected characters. If the return type is a string, the return value is preceded by the string in ” “double quotations such as “Hello World”; “abc123”; or “How to return a string in Java?.”
How To Return A String In Java?
We would like to give examples and explain to you how to return a string in Java.
public class ReturnString {
public static void main(String[] args) {
String studentName;
studentName = fullName("Stepphanie", "Greence");
/**
We are using the fullName method that accepts arguments of a string, including first name and last name and concatenates them into a string object. The first @parameter is the first name, and the other @parameter is the last name.
*/
System.out.println(studentName);
}
public static String fullName(String first, String last) {
String name;
name = first + " " + last;
return name;
/**
@return A reference to a String object combined from the first and last names.
*/
}
}
Our purpose is to return the string StudentName.
- In the driver class above, we define a public function ReturnString that returns a String value. The public keyword indicates that the return values are visible.
- In the driver class above, we define a public function ReturnString that returns a String value. The public keyword indicates that the return values are visible.
- The static keyword is not mandatory, so the returning method can be called without generating the driver class instance. The main function is a fixed value directly called without the need for the driver class’s name.
- Continue to define the structure of students’ names following the fullName method – joining the first and last names.
- The line System.out.println(studentName) is to print the string method.
- return type is the next value. The type states which primitive data can be returned. In our example, the return type is a string fullName.
- Name the method. You can choose any name, excluding pre-fixed keywords in Java. Above, the string name is “name” containing the parameters passed first + ” ” + last.
- The return statement is the last line, and the return keyword is preceded with “”double quotations.
- Wrap the returning function’s beginning and end using curly braces {}.
Final Words
The example is simple yet helpful in understanding how to return a string in Java. You can refer to the sample itself and the explanation to write your own string returning method.
Let us know if you have any difficulties!
Leave a comment