Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialsyalih
16,468 PointsWhat is "void" and "return" in java???
I cant understand when to use void and when to use return please someone explain in berif
5 Answers
Jami Schwarzwalder
17,961 PointsVoid is used when you are creating a class that will not return any value. Java always needs to know what to expect. So if you will get a string after to perform the action you would need to label string, if you expect a number you would label int, double, or whatever type of number will come back .
public void name() {
a= a + b;
}
Above it says void because you are just changing value of a.
public int math() {
return 1+2;
}
In this one if you call math() it will display 3.
So if you are writing a program and you want to say display the value of a in addition to changing it you would write code like this:
public int name() {
a= a + b;
return a;
}
I hope this helps
Craig Dennis
Treehouse TeacherUse void
when the method is not expected to return anything. This tells callers to not expect a return value. Use return
in a method to send a value back to the caller of the method.
That clear things up?
Andrew Shook
31,709 PointsVoid is the Java keyword that tells the compiler that a function will not be returning any value after it is executed. Return is the Java keyword that tells the compiler what will be returned when a function is finished. For instance, you make two functions, square() and calcSquare(), which both calculate the square of a number. The difference is that square() will find the square of a specific variable call "input", while calcSquare() returns the square of the number passed as an argument to the function. In this example, square() would be void since it does not return anything, but instead directly alters a known variable. However, calcSquare() will not be void because it does return a value.
Edit: Brief version, use void when a function doesn't use return.
Andrew Mitchell
2,739 Pointsvoid means that the method will not return anything. return means that it will return a value for the method that called the method with value to use example public int getAge() { return mAge; }
would return an int(in this case the age) so that elephant.setAge() = zooelephants.getAge();
if you need more help just ask and i'll try to explain better sorry that im not very good at explaining.
syalih
16,468 Pointsthank you soo much :)
Craig Dennis
Treehouse TeacherCraig Dennis
Treehouse TeacherJami I think you forgot to change
void
toint
in your last example.Andrew Mitchell
2,739 PointsAndrew Mitchell
2,739 PointsHow do you make the black code squares like that? the only thing i could find where back-tick marks and they just italicize text
Jami Schwarzwalder
17,961 PointsJami Schwarzwalder
17,961 Pointsabove my tab key i can add "
" at first I thought it was ' but its actually "
"you can also click on the Markdown Cheatsheet and copy the format.
it takes 3 ` to make the black bars.
I hope that helps.