How to make System.out.println() shorter
void p(String l){
System.out.println(l);
}
Shortest. Go for it.
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!');
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 ;)