File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Typedef

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it does, but I thought I would ask. Does Java have any concept of Typedef? And if not, why not?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
brad balmer
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. ;-)
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. No. No. I teach C, C++ and Java, typedef's do not create new datatypes- there are just alias for existing types!!!
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic