Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Does java have a macro definition facility like #define in C

Thread view: 
Google Man - 13 Dec 2007 19:05 GMT
I am just beginning to learn java.  I hate having to type long stuff
like system.out.println("Hello World")  when I could do something
like  cout<<"Hello World" in C++.  Any way I can do something like :

#define cout  system.out.println

or somehow not have to type system.out. each time?
Arne Vajhøj - 13 Dec 2007 19:20 GMT
> I am just beginning to learn java.  I hate having to type long stuff
> like system.out.println("Hello World")  when I could do something
[quoted text clipped - 3 lines]
>
> or somehow not have to type system.out. each time?

No.

Sometimes you can use a static import to save a class name, but
that is about it.

And you should prioritize readability over typing savings.

If you really insist I believe there are a couple of
Macro preprocessors for Java.

Arne
Richard Reynolds - 13 Dec 2007 19:24 GMT
>I am just beginning to learn java.  I hate having to type long stuff
> like system.out.println("Hello World")  when I could do something
[quoted text clipped - 3 lines]
>
> or somehow not have to type system.out. each time?

That's hardly a long line!
How about just using an IDE with auto completion?

You could always write your own class called something short and wrap the
System.out.println call with a method with a short name, hardly seems worth
it though!
public class O
{
   public static p(Object o)
   {
       System.out.println(o);
   }
}
then you can just do:
O.p("I wouldn't recommend this, but if you really feel the need ...");
Knute Johnson - 13 Dec 2007 19:48 GMT
> I am just beginning to learn java.  I hate having to type long stuff
> like system.out.println("Hello World")  when I could do something
[quoted text clipped - 3 lines]
>
> or somehow not have to type system.out. each time?

You can do this but the real question is why.  Speaking of why, why
don't you just create a macro in your editor and make ALT-P (or
something) give you System.out.println()?

import java.io.*;

public class test2 {
    public static void main(String[] args) {
        PrintStream x = System.out;
        x.println("hello world");
    }
}

Signature

Knute Johnson
email s/nospam/knute/

Roedy Green - 13 Dec 2007 21:18 GMT
On Thu, 13 Dec 2007 11:05:59 -0800 (PST), Google Man
<googleman04@yahoo.com> wrote, quoted or indirectly quoted someone who
said :

>I am just beginning to learn java.  I hate having to type long stuff
>like system.out.println("Hello World")  when I could do something
[quoted text clipped - 3 lines]
>
>or somehow not have to type system.out. each time?

You can use C's macro processor on Java source.

You can use the import static to cut down the verbiage. See
http://mindprod.com/jgloss/import.html

Smart IDEs such as Intellij Idea have completion.  See
http://mindprod.com/jgloss/ide.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 13 Dec 2007 21:18 GMT
On Thu, 13 Dec 2007 11:05:59 -0800 (PST), Google Man
<googleman04@yahoo.com> wrote, quoted or indirectly quoted someone who
said :

>I am just beginning to learn java.  I hate having to type long stuff
>like system.out.println("Hello World")  when I could do something
[quoted text clipped - 3 lines]
>
>or somehow not have to type system.out. each time?

see http://mindprod.com/jgloss/macro.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

RedGrittyBrick - 14 Dec 2007 11:32 GMT
> I am just beginning to learn java.  I hate having to type long stuff
> like system.out.println("Hello World")  when I could do something
[quoted text clipped - 3 lines]
>
> or somehow not have to type system.out. each time?

Most IDEs will let you expand abbreviations. For example you could
configure Eclipse so that when you type 'println' and press Ctrl+Space
it inserts 'System.out.println("");' and positions the cursor between
the quotes.

You could do this for 'cout' but I'd recommend you get into the habit of
thinking in Java rather than thinking in C then translating your
thoughts into Java.

When I type 'main' and press Ctrl+Space, Eclipse offers me a choice of
several named boilerplate chunks of code for Swing or non-Swing apps.
Lionel van den Berg - 14 Dec 2007 11:52 GMT
> I am just beginning to learn java.  I hate having to type long stuff
> like system.out.println("Hello World")  when I could do something
[quoted text clipped - 3 lines]
>
> or somehow not have to type system.out. each time?

I'm agreeing with all the other posts. I believe you can do something like:

import system.out;

and the you just write:

println("foo");

Test it if you wish.
Google Man - 16 Dec 2007 15:47 GMT
> I am just beginning to learnjava.  I hate having to type long stuff
> like system.out.println("Hello World")  when I could do something
[quoted text clipped - 3 lines]
>
> or somehow not have to type system.out. each time?

Thanks for all the answers.  All make sense.
I guess the easiest is to just define a short wrapper as suggested. I
was just getting ticked off when I was doing a sample test with lots
of system.out.println.  Anyway .. as I am learning, I realize that is
going to be the least of my worries.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.