473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

What is the difference between "System.out.println" and "System.out.printf" in java

gautamz07
Expand|Select|Wrap|Line Numbers
  1. for(String x: s)
  2. System.out.printf("%s" , x); 
  3.  
Why is "%s" used here ? :((
Nov 3 '13 #1
1 6127
Nepomuk
3,112 Expert 2GB
While println will take a String, print it and add a new line at the end, printf will take a formatted String and not add a new line at the end. A formatted String means, that you can do stuff like this:
Expand|Select|Wrap|Line Numbers
  1. for(int i=1; i<=10; i++) {
  2.    System.out.printf("The given number is %d and it is %s\n", i, (i % 2 == 0 ? "even" : "odd"));
  3. }
This will print
Expand|Select|Wrap|Line Numbers
  1. The given number is 1 and it is odd.
  2. The given number is 2 and it is even.
  3. The given number is 3 and it is odd.
  4. The given number is 4 and it is even.
  5. The given number is 5 and it is odd.
  6. The given number is 6 and it is even.
  7. The given number is 7 and it is odd.
  8. The given number is 8 and it is even.
  9. The given number is 9 and it is odd.
  10. The given number is 10 and it is even.
  11.  
So in short: %s is used when you want to have a String at that place, %d is used for integers.
Nov 3 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Bengan | last post by:
Hi, I have made an java app and i want to do an install wizard for that. What do i need to do to make my java app work at another persons system that doesnt have anything with java installed. Is...
2
by: Kkiely | last post by:
what is the difference?
2
by: diadia | last post by:
string s = "hello"; const char *p = s.begin(); cout << p << endl; // print hello s = ""; char *p2= s.begin(); cout << p2 << endl; // print hello why?????
11
by: J Wang | last post by:
dear, I debug the program recently as follows. #include <sys/stat.h> int main(int argc, char *argv) { struct stat buf;
4
by: Kunle Odutola | last post by:
I'm trying to understand where the information in the META.INF directory including MANIFEST.MF etc is to be found for .NET assemblies. Also some projects such as Eclipse's OSGi kernel stores...
10
by: vivekian | last post by:
Hi , New to dot net and C# and was wondering what is the equivalent of a java package in dot net ? thanks, vivekian
0
by: RupeshDeenadayalan | last post by:
Is there any difference between a boot loader and a boot strapper.It would be better if any one can tell what operations does a general bootloader,boot strapper,BSP,Driver does???
5
by: bhushanbagul | last post by:
Hi All Please let me know exactly what difference i and g makes in oracle version i.e. when we say oracle 10g or oracle 11i Thanks Bhushan
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.