Answer the question
In order to leave comments, you need to log in
How does void work in Java?
What up, programmer.
For example, 2 methods
public void First_method(){
System.out.println("Hello!");
}
and
public string Second_method(){
System.out.println("Hello!");
}
The only explanation for all this is void - this type does not return a value to you.
How does it not return if I call both methods and the output "Hello" is returned the same way.
Answer the question
In order to leave comments, you need to log in
Nevermind how "beginner" you are
System.out.println("Hello!"); - this is the output to stdout, that is, the line is given to the OS, and it outputs to the console.
If you need one method to pass something to another method, then you need to use return
here is the correct example
public string Second_method(){
return "Hello";
}
public void First_method(){
String out = Second_method();
System.out.println(out);
}
It's more correct like this:
// В Java используется стиль кодирование camelCase (для методов)
public void firstMethod()
{
// Выводит строку в консоль
System.out.println("Hello!");
}
public string secondMethod()
{
// Выводит строку в консоль
System.out.println("Hello!");
// Возвращает значение в string
return "Done!";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question