473,394 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

A shorter way of output?

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
5 2543

"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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
2
by: Zulik | last post by:
Hello, I have a problem with MessageDigest MD5 in Java. I want to calculate the digest from a file, encode it in base64 and display it. Now, according to MD5 spec, digest shall be 128 bits so,...
0
by: jy2003 | last post by:
The query below is very long, and it takes about 5 seconds to get the result. Of course I am not happy with the 5 seconds, and I am wondering if I can get a better speed if I can break it into...
1
by: Treetop | last post by:
I used the following script for a site to stop displaying after a date / time, but it seems huge. Any ideas on how to make it shorter? There are many events in this list. function events() {...
8
by: Servé Lau | last post by:
Sometimes I code like this: char *p = strchr(buf,'\n'); if (p) *p = 0; But I've also seen the shorter expression: buf = 0;
2
by: junky_fellow | last post by:
Hi, Consider following piece of code: int i = 0x12345678; char c; c = i; printf("0x%x\n",c); What value will be printed ?
15
by: Mik0b0 | last post by:
Hallo everybody, my problem is: there are two single-dimension arrays, longer and shorter, every array is organized in ascending order. We need to build a new array out of two. This is what I...
1
by: Daikide | last post by:
I need average for my program. And I'm using it multiple time in my program. I'm doing this in PVSS: So say average float is 2.0013005 (FOR EXAMPLE) but the number in the end can be higher......
3
by: skanemupp | last post by:
is there anyway to make this shorter? i hate having these big blocks of similar-looking code, very unaesthetic. maybe doesnt matter good-code-wise? anyway can i make some function that makes this...
4
by: raylopez99 | last post by:
Please consider the below and how to make name references shorter-- there has to be a way. RL using System; namespace MyNamesSpace1 { class ManagerClass
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.