java printf

A

Alex Polite

I'm just starting out with Java.

Google tells me there's no function/method in the java standard
library the works Cs printf.

I find this hard to believe.

Do I really have to do stuff like:
String a = b + " c " + d + " e";

instead of

String a = "b %s d %s" % (c,e);

alex
 
M

Michael Rauscher

Alex said:
I'm just starting out with Java.

Google tells me there's no function/method in the java standard
library the works Cs printf.

I find this hard to believe.

Belive it. E.g. Cs printf uses a variable argument list (ellipsis).
There's nothing like this is Java. Of course, you're free to implement
an equivalent to printf.
Do I really have to do stuff like:
String a = b + " c " + d + " e";

instead of

String a = "b %s d %s" % (c,e);

Apart from that I don't see printf here:

String a = "b " + c + " d " + e;

Bye
Michael
 
S

Steve Horsley

Alex said:
I'm just starting out with Java.

Google tells me there's no function/method in the java standard
library the works Cs printf.

I find this hard to believe.

Do I really have to do stuff like:
String a = b + " c " + d + " e";

instead of

String a = "b %s d %s" % (c,e);

alex
Yes, you do.

Steve
 
S

Silvio Bierman

Alex Polite said:
I'm just starting out with Java.

Google tells me there's no function/method in the java standard
library the works Cs printf.

I find this hard to believe.

Do I really have to do stuff like:
String a = b + " c " + d + " e";

instead of

String a = "b %s d %s" % (c,e);

alex

As of 1.5 it is possible in Java.

Silvio Bierman
 
R

Roedy Green

Do I really have to do stuff like:
String a = b + " c " + d + " e";

instead of

String a = "b %s d %s" % (c,e);

What I find peculiar is that you find the second preferable. You must
remember the magic %x codes, and you must visually match them up. You
don't find out till run time if you used the wrong code or left one
out.
 
M

Michael Rauscher

Silvio said:
I'm just starting out with Java.

Google tells me there's no function/method in the java standard
library the works Cs printf.
[...]


As of 1.5 it is possible in Java.

One never stops to learn :)

Thank you.

Bye
Michael
 
J

Jezuch

U¿ytkownik Alex Polite napisa³:
I'm just starting out with Java.

Google tells me there's no function/method in the java standard
library the works Cs printf.

I find this hard to believe.

I find it hard to believe that somebody *really* wants printf :>
Do I really have to do stuff like:
String a = b + " c " + d + " e";

instead of

String a = "b %s d %s" % (c,e);

Java's "style" is more similar to C++'s ostream::eek:perator<<()
printf is not typesafe (although gcc can warn you about mismatches between
format string and argument list) and Java tries (tried?) to avoid this kind
of error-prone stuff.
--
Ecce Jezuch
"But it's not real and that's why its how I always want to feel
so let's die, before the secret gets revealed
I've tried but nothing ever can appeal
and if you don't mind I'd like to throw it all away" - J. Stem
 
D

Dimitri Maziuk

Roedy Green sez:
What I find peculiar is that you find the second preferable. You must
remember the magic %x codes, and you must visually match them up. You
don't find out till run time if you used the wrong code or left one
out.

You know perfectly well that string concatenation, unlike printf()
or C++ ostream << operator works only on strings. Plus, it pretends
to mutate an immutable object, creating a whole bunch of temporaries
in the process. And that sucks because Java String is not exactly
a light-weight type, and because temporaries don't ge destroyed
until gc gets a round tuit.

So I find it peculiar that you find concatenation preferrable.

Not to mention the fact that
String foo = "green", bar = "apples";
printf( "%d %s %s", 7, foo, bar )
is more compact than either
StringBuffer buf = new StringBuffer();
buf.append( 7 );
buf.append( " " );
buf.append( foo );
buf.append( " " );
buf.append( bar );
or
String baz = new Integer( 7 ).toString() + " " + foo + " " + bar;

But you knew that already.

Dima
 
R

Roedy Green

You know perfectly well that string concatenation, unlike printf()
or C++ ostream << operator works only on strings. Plus, it pretends
to mutate an immutable object, creating a whole bunch of temporaries
in the process. And that sucks because Java String is not exactly
a light-weight type, and because temporaries don't ge destroyed
until gc gets a round tuit.

Concatenation automatically invokes toString, which is exactly what
happens inside printf.

As for littering the world with String object temporaries, that
depends on your compiler. A smart compiler like Jet will allocate
those on the stack, just the way printf would.
 
S

Sergio

Dimitri said:
Not to mention the fact that
String foo = "green", bar = "apples";
printf( "%d %s %s", 7, foo, bar )
is more compact than either
StringBuffer buf = new StringBuffer();
buf.append( 7 );
buf.append( " " );
buf.append( foo );
buf.append( " " );
buf.append( bar );
or
String baz = new Integer( 7 ).toString() + " " + foo + " " + bar;



System.out.println(7+" "+foo+" "+bar);

kind of compact, right?
 
A

Andrew

Alex said:
Google tells me there's no function/method in the java standard
library the works Cs printf.

I find this hard to believe.

Of course, Java does have a C-like printf now! J2SE 1.5 is introducing
System.out.printf().

Cheers,
Andrew
 
L

Liz

Alex Polite said:
I'm just starting out with Java.

Google tells me there's no function/method in the java standard
library the works Cs printf.

I find this hard to believe.
Ya well google is the GOD of programming. If she can't do it, nobody can.
 
M

Malcolm Dew-Jones

Alex Polite ([email protected]) wrote:
: I'm just starting out with Java.

: Google tells me there's no function/method in the java standard
: library the works Cs printf.

try MessageFormat()

not a printf clone, but similar idea.
 
G

Gerry Murphy

That's useful to know. I just inherited a large Java
codebase that's using a third-party library to
use printf, sprintf and fprintf, etc. and I'd like to remove them
someday.

Thanks.
 
D

Dale King

Hello, Michael Rauscher !
You said:
Belive it. E.g. Cs printf uses a variable argument list (ellipsis).
There's nothing like this is Java. Of course, you're free to implement
an equivalent to printf.

As others have already mentioned, there is going to be a printf
style formatting API in 1.5 (along with something equivalent to
scanf, but based on regex). But to your point here there is also
going to be support for variable length argument lists in 1.5.

And to the OP there is also java.text.MessageFormat that also
does some of what you would do with printf. It allows you to
specify a format string with markers specifying where to insert
the arguments. It however does not give you fine control over how
that argument is formatted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top