Please advice on where can I find the lib in order to use the shorter expression of System.out.println()
and where should I place that lib.
-
2You want to use this only because the expression is shorter?– Erick RobertsonJul 23, 2010 at 17:29
-
61If you're going to add a library to your project just to shorten an expression, you've got your priorities out of order.– Erick RobertsonJul 23, 2010 at 17:35
-
7Than you are doing it wrong, trust me.– PeterKJul 23, 2010 at 18:07
-
6I feel like this needs the code-golf tag.– Mark PetersJul 23, 2010 at 18:51
-
3Using System.out is generally bad in a well designed application, so the answers don't really matter...– BananeweizenMay 24, 2012 at 20:41
12 Answers
Logging libraries
You could use logging libraries instead of re-inventing the wheel. Log4j for instance will provide methods for different messages like info()
, warn()
and error()
.
Homemade methods
or simply make a println
method of your own and call it:
void println(Object line) {
System.out.println(line);
}
println("Hello World");
IDE keyboard shortcuts
IntelliJ IDEA and NetBeans:
you type sout
then press TAB, and it types System.out.println()
for you, with the cursor in the right place.
Eclipse:
Type syso
then press CTRL + SPACE.
Other
Find a "snippets" plugin for your favorite text editor/IDE
Static Import
import static java.lang.System.out;
out.println("Hello World");
Explore JVM languages
Scala
println("Hello, World!")
Groovy
println "Hello, World!"
Jython
print "Hello, World!"
JRuby
puts "Hello, World!"
Clojure
(println "Hello, World!")
Rhino
print('Hello, World!');
-
18
import static System.out;
is a rare practice, and quite unnecessary in my opinion, and thus seeingout.println
can cause confusion at least initially. Static import has more idiomatic usage, but this particular case isn't it. Jul 23, 2010 at 18:05 -
11+1 for Eclspe & Netbeans shortcuts. I'm undecided about the static import. Jul 23, 2010 at 18:21
-
4In Eclipse you can change the
sysout
template in Java > Editor > Templates. You can even make it more shorter by replacingsysout
byso
or so :)– BalusCJul 23, 2010 at 18:36 -
3@BalusC Better idea --
so
CTRL+SPACE opens Stack Overflow in a browser window and you stop coding Jul 23, 2010 at 19:08 -
1
sout
also works in IntelliJ IDEA (NetBeans probably copied it from them :) Anyway, good to mention that – the length ofSystem.out.println()
becomes irrelevant with it IMHO.– JonikJul 24, 2010 at 16:10
void p(String l){
System.out.println(l);
}
Shortest. Go for it.
-
8+1, just because it's amusing to see Java win a code golf for a change. ;-) Jul 23, 2010 at 20:45
-
Actually it's straight out of "Beginning Java Programming: The Object-Oriented Approach" here wrox.com/WileyCDA/WroxTitle/… May 2, 2017 at 6:09
Java is a verbose language.
If you are only 3 days in, and this is already bothering you, maybe you'd be better off learning a different language like Scala:
scala> println("Hello World")
Hello World
In a loose sense, this would qualify as using a "library" to enable shorter expressions ;)
Some interesting alternatives:
OPTION 1
PrintStream p = System.out;
p.println("hello");
OPTION 2
PrintWriter p = new PrintWriter(System.out, true);
p.println("Hello");
For Intellij IDEA type sout
and press Tab.
For Eclipse type syso
and press Ctrl+Space.
-
2Your answer probably depends on an IDE. You might want to explain where exactly this shortcut works. Otherwise your answer doesn't really help anybody. Sep 26, 2016 at 10:36
-
1@defaultlocale Thanks. Yes, it's true. I added some specific info. Sep 26, 2016 at 18:55
Use log4j or JDK logging so you can just create a static logger in the class and call it like this:
LOG.info("foo")
In Java 8 :
List<String> players = new ArrayList<>();
players.forEach(System.out::println);
As Bakkal explained, for the keyboard shortcuts, in netbeans
you can go to tools->options->editor->code templates and add or edit your own shortcuts.
In Eclipse
it's on templates.
My solution for BlueJ is to edit the New Class template "stdclass.tmpl" in Program Files (x86)\BlueJ\lib\english\templates\newclass and add this method:
public static <T> void p(T s)
{
System.out.println(s);
}
Or this other version:
public static void p(Object s)
{
System.out.println(s);
}
As for Eclipse I'm using the suggested shortcut syso + <Ctrl> + <Space>
:)
A minor point perhaps, but:
import static System.out;
public class Tester
{
public static void main(String[] args)
{
out.println("Hello!");
}
}
...generated a compile time error. I corrected the error by editing the first line to read:
import static java.lang.System.out;
package some.useful.methods; public class B { public static void p(Object s){ System.out.println(s); } }
package first.java.lesson; import static some.useful.methods.B.*; public class A { public static void main(String[] args) { p("Hello!"); } }
Using System.out.println() is bad practice (better use logging framework) -> you should not have many occurences in your code base. Using another method to simply shorten it does not seem a good option.
-
I'd disagree. If you have normal logging in place already, then using Sysout is a better way to distinguish between what you need to see at the moment. Also, using sysout is much faster, and if you do a lot of debugging every second counts. Sep 6, 2016 at 2:10