By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

A shorter way of output?

P: n/a
In Java, as we all know, you have to type System.out.println to just
print out one thing. Before Java, I used C++, and it was simply cout.
So, out of curiosity, is there any way to print stuff in Java without
having to type System.out.?
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"PlasmaDragon" <Pl**********@lycos.co.uk> wrote in message
news:15**************************@posting.google.c om...
In Java, as we all know, you have to type System.out.println to just
print out one thing. Before Java, I used C++, and it was simply cout.
So, out of curiosity, is there any way to print stuff in Java without
having to type System.out.?


No. But you could make a helper class ... eg. Printer.println("something");
where println() makes the call to System.out.println. Not much of a saving
but slightly more compact code if that's what you're after.
Jul 17 '05 #2

P: n/a
"PlasmaDragon" <Pl**********@lycos.co.uk> wrote in message
news:15**************************@posting.google.c om...
In Java, as we all know, you have to type System.out.println to just
print out one thing. Before Java, I used C++, and it was simply cout.
So, out of curiosity, is there any way to print stuff in Java without
having to type System.out.?


If you're not worried about primitives:
public class SOPHelper {
public static void p(Object o) {
System.out.println(o);
}

public static void main(String[] args) {
p("Hello world");
}
}

For primitives, you have to overload that method (for now). Alternately:
public class SOPHelper {
public static void main(String[] args) {
java.io.PrintStream o = System.out;
o.println("Hello world");
}
}
Jul 17 '05 #3

P: n/a
I've figured it out. All you need to do is add a very short print (or
whatever you want to call it) function at the end of your program,
like this:

import java.io.*;
import java.lang.*;
public class ShortPrint
{
public static void main(String[] args)
{
printf("Hello There!");
printf("5+5 is "+(5+5));
printf("5/9 is "+(5.0/9.0));
printf("16/32 is "+(16.0/32.0));
}

public static void printf(String arg)
{
System.out.println(arg);
}
}
Jul 17 '05 #4

P: n/a
cout is just a stream object, what you are really asking is there a way
to overload operators in java and the answer is no.

this was a deliberate design decision because it was felt that
overloading operators created more problems than they solved....

thats from the creators of java and can be found in the java archives....

- perry
PlasmaDragon wrote:
In Java, as we all know, you have to type System.out.println to just
print out one thing. Before Java, I used C++, and it was simply cout.
So, out of curiosity, is there any way to print stuff in Java without
having to type System.out.?


Jul 17 '05 #5

P: n/a
hi
first of all u either need to make a class or u need to write this
method in evry class you wish to use
eitherways theres not much of a saving
oth the downside this can just print string args ...you need to
overload for all types
another thing is consider
System.out.println(5+5);

so its best you stick to System.out ;) wot u say :D

regards
amey


Pl**********@lycos.co.uk (PlasmaDragon) wrote in message news:<15*************************@posting.google.c om>...
I've figured it out. All you need to do is add a very short print (or
whatever you want to call it) function at the end of your program,
like this:

import java.io.*;
import java.lang.*;
public class ShortPrint
{
public static void main(String[] args)
{
printf("Hello There!");
printf("5+5 is "+(5+5));
printf("5/9 is "+(5.0/9.0));
printf("16/32 is "+(16.0/32.0));
}

public static void printf(String arg)
{
System.out.println(arg);
}
}

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.