Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I got this error"operator + cannot be applied to java.lang.string void"

I tried to call a method in printlnfrom a super class using the object that I have created for the super class. I got this error

"operator + cannot be applied to java.lang.string void"

System.out.println("Contents of objsuper: " + objsuper.showij());
like image 881
Sumanth Reddy Avatar asked Mar 04 '26 23:03

Sumanth Reddy


1 Answers

Replace the following line

System.out.println("Contents of objsuper: " + objsuper.showij());

with

System.out.println("Contents of objsuper: ");
objsuper.showij();

The reason for this is, the return type of objsuper.showij() is void and therefore, operator + cannot be applied to it. A void method is like doing something but not returning a value and since there is no value returned by it, you can not append it to anything using + operator; you need to call it separately the way I have done above.

like image 158
Arvind Kumar Avinash Avatar answered Mar 06 '26 11:03

Arvind Kumar Avinash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!