Reputation: 1199
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.
Upvotes: 0
Views: 300
Reputation: 1155
public class tst
{
public static void main(String[] args)
{
System.out.println(toString(1));
System.out.println(toString("hello"));
System.out.println(toString(false));
System.out.println(toString(new Integer[] { 1, 2, 3 }));
}
static String toString(Object object)
{
return "Output: (" + object.getClass().getSimpleName() + ") " + object;
}
}
The console output is:
Output: (Integer) 1
Output: (String) hello
Output: (Boolean) false
Output: (Integer[]) [Ljava.lang.Integer;@51de8adb
Upvotes: 0
Reputation: 279910
You're going about this all wrong.
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.
Absolutely not.
Take this method
public int returnAnInt() {
return 42;
}
You can print the value returned by this method like so
System.out.println("Output:" + /*possibleInstance.*/returnAnInt());
This is possible due to String
concatenation.
Don't write your methods with output in mind. For example, if you have a method that is supposed to calculate the sum of two integers
public int add(int a, int b) {
return a + b;
}
Just make it calculate the sum of two integers.
There's no need to put the output in the method itself. Make the code that calls this method output the value
int sum = add(16, 27);
System.out.println("The sum is: " + sum);
// or
System.out.println("The sum is: " + add(16, 27));
As to your 3 questions, which I think become rather moot, see the other answers. You might want to look at the toString()
method which all reference types inherit.
Upvotes: 0
Reputation: 9816
In java you can always write the following:
Object o = someMethod();
System.out.println("Output: " + o);
The JVM will implicitly call o.toString()
to convert it to a string and then append it to the string 'Output: '.
Upvotes: 2
Reputation: 23939
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?
Correct, in Java, if you want to return multiple things, you should make an object that represents the data you're returning, and return that. So instead of returning paws, head, body, legs and a tail object, you'd return a Dog
.
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?
You bet, just don't tell it to add a newline. That's what println is doing for you, it means "print line". Instead, just use print.
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.
Most List implementations will have a toString method, so you can definitely print them out, try it. It may not be exactly what you want, so you may have to write a helper method to format it how you're intending.
Upvotes: 1
Reputation: 64644
1) That's correct. A method could return an object that contained instance variables of multiple types, though.
2) If you use System.out.print()
instead of System.out.println()
it will not add a newline at the end.
3) This answer provides a simple way to print out the elements of a List.
System.out.println(Arrays.toString(stack.toArray()));
Upvotes: 2