File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Is there a way to shorten System.out.println Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is there a way to shorten System.out.println" Watch "Is there a way to shorten System.out.println" New topic
Author

Is there a way to shorten System.out.println

John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
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().


www.cs-repository.info
Aneesh Vijendran
Ranch Hand

Joined: Jun 29, 2008
Posts: 125
Don't use System.out.println, use some Logging framework (Log4j/commons logging)

Cheers
Aneesh


Cheers
Aneesh
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
You can shorten until out.println

Use a static import like the following:



Bye,
Nicola
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Use static import as Nicola said, I think, there is no other way.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Aneesh Vijendran wrote:Don't use System.out.println, use some Logging framework (Log4j/commons logging)

This!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

If you are just trying to save typing time you could write your own print method using System.out.println().



Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Aneesh Vijendran
Ranch Hand

Joined: Jun 29, 2008
Posts: 125
In that case the best option would be: (Assuming log4j/Commons logging and no other frameworks are never written ;) )



Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

You are correct, that should have been Object, my mistake.

Hunter
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Ok thanks.
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
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

Joined: Mar 13, 2009
Posts: 492

That is exactly what we just did. You just put yours on one-line.




Hunter
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Yup you are right. I was thinking using Macros. Maybe Java 7 allows this.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

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

Joined: Mar 13, 2009
Posts: 492

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

Joined: Oct 14, 2008
Posts: 1337
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

Joined: May 23, 2008
Posts: 215
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

Joined: Mar 13, 2009
Posts: 492

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

Joined: Jan 10, 2002
Posts: 61010
    
  65

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

Joined: Sep 29, 2008
Posts: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there a way to shorten System.out.println