Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there a way to shorten System.out.println

 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please let me know if there is a shortcut to System.out.println? In certain programs there are lots of these print statements and it would be convenient if I could make it short like println().

 
Aneesh Vijendran
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use System.out.println, use some Logging framework (Log4j/commons logging)

Cheers
Aneesh
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can shorten until out.println

Use a static import like the following:

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use static import as Nicola said, I think, there is no other way.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64835
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aneesh Vijendran wrote:Don't use System.out.println, use some Logging framework (Log4j/commons logging)

This!
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are just trying to save typing time you could write your own print method using System.out.println().



Hunter
 
Aneesh Vijendran
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case the best option would be: (Assuming log4j/Commons logging and no other frameworks are never written ;) )



 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct, that should have been Object, my mistake.

Hunter
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks.
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish there was a macro feature something like

#define PRINT(String x) { System.out.println(x) };

Then I would be able to use PRINT() in my programs.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is exactly what we just did. You just put yours on one-line.




Hunter
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup you are right. I was thinking using Macros. Maybe Java 7 allows this.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64835
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Eipe wrote: Maybe Java 7 allows this.

Good grief, I hope not! Having survived C I can tell you that nothing would open the door for abuse and the creation of completely obtuse and unreadable code more.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really see much of a difference between macro definitions and defining your own methods? are they not the same?

Hunter
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Programmable editors (like Vim and Emacs) let you define abbreviations, so you type "sop(", and it would automatically be expanded to "System.out.println(". If your editor/IDE doesn't have that function, then a desktop automation tool like TypeIt4Me or QuicKeys might work.
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Macros, replacement happens wherever the macro function is used. When we use functions each would result in a call. Which add more overhead? I don't know!
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, but that is a function of the editor right? John is suggesting that Java will have macros.


Hunter
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64835
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Eipe wrote:Which add more overhead? I don't know!

Overhead is generally irrelevant -- what's at issue is code clarity.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:I don't really see much of a difference between macro definitions and defining your own methods? are they not the same?

Yes, they are not the same, particularly C-style macros, which are notoriously easy to abuse and get 100% wrong.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic