r/javahelp • • Dec 14 '18

Why do we not import `System.out.println` to and avoid rerwriting it every time we print something to the console ?

I find myself writing System.out.println a thousand times just to print something to the console. And before jshell came along it was and still a hurdle to quickly print something to the console. Why is it not popular to simply important those specific static field or method like for example import static Java.lang.System.out;

23 Upvotes

19 comments sorted by

26

u/KamikazeSmurf Dec 14 '18

Most IDEs have shortcuts for it so it never really becomes an issue in practice.

e.g. in IntelliJ it's just 'sout<tab>' or 'serr<tab>' to auto complete to the full line with the cursor between the braces.

Also most code doesn't see a whole lot of System.out.println usage, usually you would have a logging system that covers most of the use cases you find yourself using it for as a beginner.

Instead of using

System.out.println("foo");

using

logger.info("foo");

or similar gives much more flexibility later on. For example you can use log config to mute logging from particular classes instead of having to comment out a whole bunch of lines. You also gain a log formatter so messages can be prefixed with timestamps, source class+method+thread, etc.

If you're not using it for logging, it's still usually not common to see direct System.out.println usage, a Writer or similar would be passed around instead. It makes it possible to write directly to a file, makes it possible to unit test and assert output, etc.

tl;dr: If you're finding yourself printing to the console a lot and it's actually logging, use a logger.

2

u/MRH2 Intermediate Brewer Dec 15 '18

Eclipse: 'syso'<CTRL-SPACE><ENTER>

15

u/evils_twin Dec 14 '18 edited Dec 14 '18

Because that wouldn't work. out is a PrintStream object not a class, so you don't import it like that. System is the class and it's always imported automatically. You could declare static PrintStream print= System.out; as a class variable and then write to console like this: print.println("foo")

but once your out of school, you will hardly every use System.out, even when testing. Once you start coding professionally, you will likely use loggers to log any debug or informational output to log files.

6

u/firsthour Profressional developer since 2006 Dec 15 '18

Been coding for 15+ years and I still write sysouts every day for debugging, some I eventually turn to log, most I just delete.

OP, if you're using Eclipse, you can type "sysout" hit ctrl+space and it will fill it in for you.

2

u/rmslashusr Dec 15 '18

I’d worry that’s an indication of a much larger problem with how your organization is structuring its code if you constantly find yourself in situations where you can’t use the debugger which presumably means you also can’t unit test any of these sections of code you can’t run locally to debug or even connect the debugger to remotely.

2

u/firsthour Profressional developer since 2006 Dec 15 '18

I don't like debugging, I find I'm much faster doing what I do. If anything, my organization has enabled me to skip debugging by paying for Jrebel.

I think it's a weird leap to make that because I like sysout it means I'm unable to debug and our test coverage sucks, basically the opposite.

5

u/rmslashusr Dec 15 '18

I was giving you the benefit of the doubt that you were working with a code base that others had so poorly architected that you couldn’t use the debugger and simply left click once to add a breakpoint and get 100% of the information you need by running debug on the unit test. It’s one left click. Versus countless iterations of writing print lines to examine variable state. You should try making yourself use the debugger more, you might be faster then it now because you’re not used to it yet but there’s absolutely no way you’re more productive on the whole writing print lines constantly than anyone of equivalent skill that is willing to use a tool. It sounds like John Henry, ready to die with print lines in your hands, but there’s an easier way if you’re willing to learn the tech.

1

u/firsthour Profressional developer since 2006 Dec 15 '18

Haha you make a good point. I certainly used the debugger for years, could hit the keys in my sleep. I'll make a go of it in 2019.

1

u/rmslashusr Dec 15 '18

I switched to IntelliJ a year or two back and found the debugger easier but YMMV. Eclipse might have also made similar strides to make the tool better since then. Now if only there was a free profiler that worked well for finding memory leaks =/

1

u/msx Dec 15 '18

Actually syso is enought

1

u/rzwitserloot Dec 16 '18

Even if you debug like this ('sysout debugging' can be the right choice, but if you're doing it 'every day' I suggest you get comfy with the debugger a little more), you should not use sysout for this. Some apps really do write to it for actual non-debug related reasons. Write a little utility class called Debug or whatnot, and give it a print method. This way, you can do a few fancy things (for example, prefix the class name+line number via stack trace analysis so that you can click on it in the output in your IDE to hop straight to it), and most importantly, you can find calls to your debugprint and remove it. Nothing like having tons of stale debug-related sysout crap all over the place.

1

u/firsthour Profressional developer since 2006 Dec 16 '18

I literally said in my post I don't leave sysouts in.

1

u/rzwitserloot Dec 16 '18

You'll forget from time to time. Programmers should be considered human.

3

u/shadowX015 Extreme Brewer Dec 15 '18 edited Dec 15 '18

You can actually static import it but it doesn't shorten it by very much. System.out is a static member.

import static java.lang.System.out;


// used like so:
out.println("foo");

The problem here is not only is it not really much shorter but it is actually obfuscating where you are writing to. There are other situations where you may want to interact with a PrintWriter and it is no longer immediately obvious that you are writing to stdout.

I would personally consider it to be very poor practice because it sacrifices readability but I'm just kind of throwing out that it can be done.

2

u/chronicideas Dec 14 '18

This!

When I started coding I used System.out.println

Now I use loggers like Log4J2 and am able to write log messages to the console at different levels appropriate to the message. For example,

log.info(string), log.debug(string), log.error(string)

3

u/Yithar Intermediate Brewer Dec 15 '18

As others said, once you get into the industry, you don't really use System.out.println that much, compared to logger.info.

That being said, if you're keen on learning Scala, in Scala you can just write println(...) as shorthand for System.out.println().

3

u/thekab Dec 15 '18

It's not popular because nobody writes to it that much. Even for a command line tool there's no good reason to couple it that tightly.

The only time I ever use sout is for occasionally testing a small snippet.

1

u/Wolfhammer69 Nooblet Brewer Dec 15 '18

IntelliJ lets you type sout which brings up the context you can tab complete 😉

-1

u/mblan180131 Java is love. JAVA IS LIFE. Dec 14 '18

Most of the time it's just used for testing (and is therefore removed shortly after), Java isn't exactly notorious for being good at console applications