Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

What 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

Void 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
Craig Dennis
Treehouse Teacher

Jami I think you forgot to change void to int in your last example.

Andrew Mitchell
Andrew Mitchell
2,739 Points

How do you make the black code squares like that? the only thing i could find where back-tick marks and they just italicize text

above 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.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Use 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
Andrew Shook
31,709 Points

Void 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
Andrew Mitchell
2,739 Points

void 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.

thank you soo much :)