Explain me this code's working please

https://code.sololearn.com/cd2m6X2qZPnF/?ref=app Also what if we replace printf with print why does it give error

8/4/2019 3:47:59 AM

Ayush Pandey

4 Answers

New Answer


Ayush Pandey System.out.printf prints the string and returns printstream object which is consumed by if statement by checking for null value. System.out.print function does not return anything as it's return type is void, and void is not a valid type, it's just a placeholder indicating "no type". if doesn't know how to deal with void, so it gives error


more detailed, java.io.PrintStream.printf() and java.io.PrintStream.append() return PrintStream object so you can test if it is null. Printstream send chars from printf() and append() methods to output, ( to screen ). It is possible use printf() with only one parameter because other parameters are in Object... args, which can be empty. print return void so you cant test if it is null, because void is not object, it is keyword which means "no value is return". class java.io.PrintStream PrintStream printf​(String format, Object... args) PrintStream append​(CharSequence csq) void print​(String s)


check how it returns object PrintStream import java.io.PrintStream; ... var s = new PrintStream(System.out); Object ps = s.printf("some text\n"); // some text System.out.println(ps == s); // true


zemiak bro you r too techinical I have not very advance in java but yeah I am getting somethig .It would be a great of u if u explain properlu