1
1
123qwe2015-08-04 14:46:05
Java
123qwe, 2015-08-04 14:46:05

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

2 answer(s)
N
nagibator8000, 2015-08-04
@Yonghwa

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);
}

the Second_method() method returns a string, and the First_method() method calls it and receives this string, then prints it to the console.

M
Mokhirjon Naimov, 2015-08-04
@zvermafia

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 question

Ask a Question

731 491 924 answers to any question