If I want to print the string "Output:" followed by whatever result a method returns, the only one that will do so on the same line as "Output:" is a method that returns a String type. For instance:
public static void main(String[] args){
System.out.println(returnString("Hello"));
}
public static String returnString(String s){
return "Output:" + s;
}
If, however, I want to print the string "Output:" followed by the method return of any other variable type, the return would have to be on a separate line. For instance:
public static void main(String[] args){
System.out.println(returnInt(2));
}
pubic static int returnInt(int n){
System.out.println("Output:");
return n;
}
or by changing the return type to String and using a built-in conversion method that is appropriate for the return type. For instance:
public static void main(String[] args){
System.out.println(returnInt(2));
}
public static String returnInt(int n){
return "Output:" + String.valueOf(n)
}
or
public static void main(String[] args){
System.out.println(returnArray(new int[]{1, 2, 3}));
}
public static String returnArray(int[] array){
return "Output:" + Arrays.toString(array);
}
or
public static void main(String[] args){
System.out.println(returnBoolean(false));
}
public static String returnBoolean(boolean b){
return "Output:" + Boolean.toString(b);
}
So three questions:
1) It's not possible to declare two return types when initializing a method the same way you can declare two input types (i.e. methodName(int n, String S)), correct?
2) Is there any way (such as an escape sequence) that pulls the next line up ot the same line when returning a non-string?
2) And if not, is there a built-in method for returning a List (the way Arrays.toString or String.value Of do for their respective types? I can't find anything similar such as List.ToString etc.