Please explain?

Member Avatar for coil

println prints out a new line, then your message, and print just prints out the message. printf prints out a formatted string.

the printf(...) ,which is a C-style output, is used starting from JDK1.5.
for the details about java printf() see
6. 14. 1. Using Java's printf( ) Method

My teacher stresses on using

println than printf , when i asked him why?
He replied " Cus printf has some issues with java, so he recommends using print or println than printf?

Do you think there is any issue?
also, Can you tell me which is most important situation when we use printf?


example of teacher:

I used:
System.out.printf (" The principal is %d \n number of years: %d, \nrate is: %f \n The calculated simple interest is:%f\n ", principal,noy,rate,si);
where principal, noy is int and rate and si is double.

Teacher recommended:
System.out.println (" The principal is"+principal+ "\n number of years:" +noy+ "\nrate is:"+rate+"\n The calculated simple interest is:"+si);

Both works, but which is better to use in real life programming??

Why not just ask him "What issues?" I am not aware of any myself, except that it is a bit more difficult to read and will throw an exception if there is a formatting problem.

Why not just ask him "What issues?" I am not aware of any myself, except that it is a bit more difficult to read and will throw an exception if there is a formatting problem.

I didn't ask him.. didnt wanna piss him off. I want A in my class but moreover i wanna learn the language well. So i rather decided to put this on net than asking him.

It shouldn't piss him off if you merely said something like, "I'm curious, what are the issues with printf, so I can avoid problems in my future programs?" (Unless he really doesn't know and is just blowing smoke - then he might be pissed off)

Fixing incorrect format strings is definitely more trouble than using println with concatenation, so maybe he just wants to avoid a lot of the hassle with it.

"Issues" is a rather broadly generic word, so he could be referring to any number of things. I'm curious what they might be.

Edit: I do agree though that you should just do it the way the prof wants it done. He's the gatekeeper you have to pass for that 'A'.

Yes, please do ask him about this, and let us know what he says.

I don't think it's likely that you can annoy a teacher by showing an interest and trying to learn more. It might be best to pursue it during office hours or after class, because he's probably got plenty of material to go over in his lectures, but do follow up on it.

ok..my class is 2mrw i will ask him.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.