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

G

Google Man

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?
 
A

Arne Vajhøj

Google 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
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?

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
 
R

Richard Reynolds

Google Man 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
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?
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 ...");
 
K

Knute Johnson

Google 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
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?

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");
}
}
 
R

Roedy Green

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?

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
 
R

RedGrittyBrick

Google 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
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?

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.
 
L

Lionel van den Berg

Google 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
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?

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.
 
G

Google Man

I am just beginning to learnjava. I hate having to type long stuff
like system.out.println("Hello World") when I could do something
like cout<<"Hello World" inC++. Any way I can do something like :

#define cout system.out.println

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top