views:

4510

answers:

8

Coming from a C and C++ background, I found judicious use of typedef to be incredibly helpful. Do you know of a way to achieve similar functionality in Java, whether that be a Java mechanism, pattern, or some other effective way you have used?

Thanks,

-bn

+6  A: 

Java has primitive types, objects and arrays and that's it. No typedefs.

cletus
A: 

There is no typedef in java as of 1.6, what you can do is make a wrapper class for what you want since you can't subclass final classes (Integer, Double, etc)

yx
A: 

Typedef allows items to be implicitly assigned to types they are not. Some people try to get around this with extensions; read here at IBM for an explanation of why this is a bad idea.

Edit: While strong type inference is a useful thing, I don't think (and hope we won't) see typedef rearing it's ugly head in managed languages (ever?).

Edit 2: In C#, you can use a using statement like this at the top of a source file. It's used so you don't have to do the second item shown. The only time you see the name change is when a scope introduces a name collision between two types. The renaming is limited to one file, outside of which every variable/parameter type which used it is known by its full name.

using Path = System.IO.Path;
using System.IO;
280Z28
"Typedef allows items to be implicitly assigned to types they are not" What? Typedef simply allows you to create another name as an alias for the type. The type is still the exact same, you just get a shorter name for it. It has nothing to do with type inference. -1
jalf
@jalf: Actually, type inference is used as the solution to exactly what you're talking about, but I've given another example where you can use a "typedef" to get around a name collision.
280Z28
A: 

There is no need for typedef in Java. Everything is an Object except for the primitives. There are no pointers, only references. The scenarios where you normally would use typedefs are instances in which you create objects instead.

Markus Koivisto
No, the need for typedef still exists if you want a shorter name for a type. Or just if you want to be able to replace the use of one type with another by changing one place in the source code.
jalf
The latter hinders readability, since a new programmer will have to look up the definitions of all the types.The OO way is to use polymorphism instead. The source code uses the superclass or the abstract class/interface and the specific type is only changed where the object is created.
Markus Koivisto
The most important part of code is making your code readable. Renaming (Duplicating!) a type for shortness has to be the absolutely worst coding concept I've come across in a while!
Bill K
@Bill K: meh, say that after you've had to type something like std::map<int, std::map<std::string, boost::shared_ptr<std::string> > >::const_iterator a few times. In that case, a name-shortening typedef enhances readability, not hinders it.
Joel
Many uses of typedef deal with portability. There is less need for typedef in Java (although I would not agree that there is no need for it) because - for example - int is 32 bits on the one and only platform that Java runs on, the JVM.
Joel
A: 

You could use an Enum, although that's semantically a bit different than a typedef in that it only allows a restricted set of values. Another possible solution is a named wrapper class, e.g.

public class Apple { public Apple(Integer i){this.i=i; } }

but that seems way more clunky, especially given that it's not clear from the code that the class has no other function than as an alias.

Steve B.
A: 

If this is what you mean, you can simply extend the class you would like to typedef, e.g.:

public class MyMap extends HashMap<String, String> {}
Zed
anti-pattern :-P (http://www.ibm.com/developerworks/library/j-jtp02216.html)
Andreas_D
I'd argue whether this is any more anti-pattern than using typedef in C.
Zed
It definitely is - `typedef` has none of the problems that article describes for those fake classes (and they are very real).
Pavel Minaev
A: 

Just another Java limitation! In the good old times I was writing in PASCAL :-P

Xpucmo
I was just asking the question for informational reasons; I would not call it a limitation exactly, or pass positive or negative judgment. It is something the language does not do which is not good or bad -- it is the language.
bn
A: 

I am missing typdefs as well in Java. However also in C++ I'd like to see an improved typedef handling. Suppose I have 2 int typedefs:

typedef int id1_t; typedef int id2_t;

and I have a definition: void f(id1_t first, id2_t second) {

}

If I accidentally swap the parameters, I get no warning. I would like one. So:

id1_t one; id2_t two;

f( two, one); // is perfectly OK (to the compiler)

also:

int a, b; f(a,b);

I would like seeing them treated as genuine types.

lolke