aspose file tools*
The moose likes Beginning Java and the fly likes Typedef Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Typedef" Watch "Typedef" New topic
Author

Typedef

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I don't think it does, but I thought I would ask. Does Java have any concept of Typedef? And if not, why not?


GenRocket - Experts at Building Test Data
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Well, java has classes and classes are used to define data types.
I'm not entirely familiar with typedef. Is there something that you do with typedef that you wish you could do in Java but you can't?
[ March 10, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
brad balmer
Ranch Hand

Joined: Mar 08, 2004
Posts: 57
No, java does not have the concept of typdef that c++ does.
As for why not, who knows. Maybe it will in future releases but definitly not now.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

typedef int INTEGER;
Bad example, but that is the gist of it.
I don't know if I would ever use it, I was just curious.
[ March 10, 2004: Message edited by: Gregg Bolinger ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
If you say
typedef int INTEGER;
INTEGER myNumber = 2;
I might say

Or I might say
Why do you want to make a new name for the int data type?

[ March 10, 2004: Message edited by: Dirk Schreckmann ]
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
I believe that the "typedef" keyword is used to define a new type, something like:
typedef struct {float realpart, imaginary;} complex;
You could then use "complex" as a type when declaring variables. This would be very similar to:
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

typedef in C/C++ was used for a few reasons...
1) Convience. you could use typedef to change a "struct tnode" to myNode. saves some typing. and, in a large source library, could save disk space (much more important 25 years ago.
2) It helped with portability. in C, not all systems used the same number of bytes for the various primitives. an int may be 4, 6, 12... bytes, depending on the underlying OS and hardware. If you want to port to a different system, you only need change the typedef, not every reference. This is not a problem in Java.
3) It helps with maintainability. the line
treePtr rootptr;
is much more readable than
struct node * rootptr;
[forgive me if i've made any coding blunders... it's been a few years since i've done any C, and i tend to forget this kind of stuff if i don't keep in current in my brain]


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
In C, it is necessary to use the keyword "struct" when declaring a user-defined type. C++ did away with this requirement. Java doesn't require it, either. Also, Java primitive data types are the same size no matter where it is run. This removes the portability issues that C solves with the typedef.
Layne
p.s. Fred, you're C/C++ looks fine. ;-)


Java API Documentation
The Java Tutorial
James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
No. No. No. I teach C, C++ and Java, typedef's do not create new datatypes- there are just alias for existing types!!!


Author and Instructor, my book
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
yes, typedef just references an existing type.
Therefore technically

first creates an nameless struct type with 2 floats and only then aliasses it to the name complex .
Of course in this instance you probably don't care
If I remember correctly some C or C++ compilers would refuse to compile the following:

but I could be mistaken and thinking of another language...


42
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Jeroen Wenting:

If I remember correctly some C or C++ compilers would refuse to compile the following:

but I could be mistaken and thinking of another language...

AFAIK, this should compile just fine. The alias doesn't remove or hide the original name for the type.
Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Typedef