Is There a Java Equivalent or Methodology For the Typedef Keyword in C++

Is there a Java equivalent or methodology for the typedef keyword in C++?

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

Why doesn't java use a keyword like 'Typedef' in Cpp

Besides act as a convenience, typedef provides a way of making code cross-platform. The primitive types in C++ are platform dependent. However, most platforms/compilers will provide definitions of standard headers that define platform-independent types, like uint32_t, and quasi-independent types like size_t. Different machines will have different typedefs to make this work, but the end result is that code which relies on them can be ported much more easily.

Java already has a platform-independent definition for all it's primitive types, so there is no real need for anything equivalent to typedef.

Implement typedef in Java

Since there are no typdefs in Java you cannot use them. The best approximation might be this:

public class Time32 extends UInt {
public Time32(){
super(5);
}
}

Simulating C++-typedefs in Java

The C++ program in the question translates to the following Java code:

public class FooBat {}
public class FooBar {}

public class Foo {
public static Class get_Bar() { return FooBar.class; }
public static Class get_Bat() { return FooBat.class; }
}

public class Introspect {
public static void main(String[] args) {
if( Foo.get_Bar() == FooBar.class &&
Foo.get_Bat() == FooBat.class )
System.out.println( "All is well.\n" );
}
}

This is not as efficient as the C++ code. The types are determined in the C++ version during compilation. In the Java version they are determined at run-time.

A better answer that remedies this issue is most welcome.

Define custom array type in Java

There are no typedefs in java. Arrays are either of one of the primitive types, or some class that will just compile away to object references.

Using a wrapper class for a typedef equivalent in Java; must I expose the class variable?

You can create a package-scoped class like so:

class CardList extends ArrayList<Card> {
// Literally no need for anything here unless you want
// constructors that accept capacity, etc.; the default
// constructor will be provided
}

...and then just use it:

CardList list = new CardList();
list.add(new Card());
// ...

But it's usually better to code to interfaces, in which case:

interface CardList extends List<Card> {
// No need for anything here
}

and

class CardListImpl extends ArrayList<Card> implements CardList {
// No need for anything here unless you want constructors
// that accept capacity, etc.
}

and use it:

CardList list = new CardListImpl();
list.add(new Card());
// ...

...then you can switch to LinkedList<Card> if you like by just changing one file.


Or for that matter, you don't really need the CardList interface — CardList is only two characters shorter than List<Card>. So that would just be:

class CardListImpl extends ArrayList<Card> {
// No need for anything here unless you want constructors
// that accept capacity, etc.
}

and use it:

List<Card> list = new CardListImpl();
list.add(new Card());
// ...

...and again if you decide to switch the type of concrete list class, you only have to do that in one place.



Related Topics



Leave a reply



Submit