Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Is there a way to put a method INSIDE a system.out.println?

+6 votes
So a short explanation would be the following. Dr. Stonedahl said that the more repetitive your code is, the less polished your code becomes, so my question is, pretend i already have my method declared, what i want is something like this:

System.out.println("blablablabla" + method "blablabla");
asked Sep 10 in CSC 211 (F17) by Kevin Monroy (591 points)

1 Answer

+1 vote
Yes if your method returns a value.

For example you could have something like

public static void main(String[] args) {

System.out.println("boa"+r()+"d");

}

public static String r(){

return "r";

}
answered Sep 10 by Jared Haeme (285 points)
can you expand a little more on this, im dont understand it
You will learn about returning values in methods later on so no need to worry if you don't understand this now. You have the option when declaring a method to have it return a value so that every time you call that method it will give you that value which you can store in a variable or use in some other way.
Normally you've been declaring a method so using "void" such as
public static void noReturn(){
System.out.println("7")
}
"Void" means there is no return value.
When you call this method it will simply print 7 in the console window.
However you could use value types such as "int", "char", "double", "String", etc. in the place of void to say that this method returns a value of that type. For example
public static int returnSeven(){
 return 7;
}
If you were to call this method nothing would happen. However if you were to call this method inside of a print statement like System.out.print(returnSeven()) it would print out 7 in the console window
...