简体   繁体   中英

The method println(boolean) in the type PrintStream is not applicable for the arguments (void)

I am getting an error when I try executing the following code:

package Abc;

public class Class3 {

    public void another() {
        System.out.println("Hello World");
    }

    public static void main(String[] args) {
        Class3 obj1 = new Class3();
        System.out.println(obj1.another());
    }

}

The error is:

The method println(boolean) in the type PrintStream is not applicable for the arguments (void)

Your another() function return type is 'void' which essentially says it is defined to return nothing.

package Abc;

public class Class3 {
    public void another() {
       System.out.println("Hello World");
    }

   public static void main(String[] args) {
    Class3 obj1 = new Class3();
    obj1.another();
    }

}

Println() function expect something while your method doesn't return anything. That's why you are getting error.

Your another method has the return type "void" so basically it doesn't return anything. So you can't Print anything .If you want your code to work you just called obj1.another(). Whitout the System.out.println() method.

We can call any function in System.out.println(boolean) which returns any Object, String, int, boolean, char, char[], double, float, long value.

The method println(boolean) in the type PrintStream is not applicable for any function which have void return type.

package Abc;

public class Class3 {
 public String another(){
     return "Hello World";



 }
    public static void main(String[] args) {
        Class3 obj1 = new Class3();
        System.out.println(obj1.another());

    }

}

it will work because it returns String type value not void.

you want to print the string ("Hello World")? you can use the IDE tools to help you solve the problem easily; you can not print twice ,you need return. change like this

 package Abc; public class Class3 { public String another(){ return "Hello World"; } public static void main(String[] args) { Class3 obj1 = new Class3(); System.out.println(obj1.another()); } }

package Abc;

public class Class3 {
    public static void another(){
        System.out.println("Hello World!");
    }
    public static void main(String[] args) {
        another();
    }
}

That's all you have to do, I don't even know how this was running without the another() is static.

Its just a feature of jdk 1.8 (Not a big issue) In order remove this error from your project just degrade your jdk from 1.8 to 1.7 it will start behaving normally.

Steps : 1. Right click on project/Repository 2. Click on properties 3. Click Java Compiler 4. Choose jdk 1.7 from drop down 5. Click Apply & Close button

You are done, It will rebuild the project and you are good to go. Thanks.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM