Home arrow static arrow Java Programming [Archive] - data types
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - data types
4 Duke Stars available
This topic has 12 replies on 1 page.

Posts:18
Registered: 7/17/04
data types  
Jul 23, 2004 3:57 PM



 
Does anyone know if there is a class, interface, or property file somewhere (as part of the standard java environment) that holds a descrete listing of all the standard data types one could use to define a variable (eg. int, String, booean, Double, etc.)?

I want to build a quick and dirty tool/utility to quickly create basic data class files with appropriate getter and setter methods (it will have a GUI front end).. I would like to find a class, interface, etc. with the supported data types defined so I always have a correct listing of them in my application.

Yes I can type in the types on my own, but I think it is better to get the information from the environment itself... kind of what OOP and reusability is all about! :-)

Anyone know where to look?

Thanks,

BillR
 

Posts:18,384
Registered: 21.03.00
Re: data types  
Jul 23, 2004 4:18 PM (reply 1 of 12)



 
Hi,

I'm not sure that I understand what you mean by standard. A variable can be declared to be of any class, so all classes in J2SE are standard?
Another definition would be all classes in the package java.lang, but that would imply that System is a standard data type.

So in short; All classes can be used as "data types", so it doesn't make sense to have a list within J2SE (and you will probably not find one).

That is my 5 cents
/Kaj
 

Posts:6,750
Registered: 1/25/04
Re: data types  
Jul 23, 2004 4:21 PM (reply 2 of 12)



 
The data types are boolean, byte, short, int, long, float, double, char, and Object. There ya go.

If you want to break out Object, you will have to define what you mean by "standard." Anything defined in java.lang? Anything that comes with the SDK? I think either answer would be available via reflection. However, neither is probably what you want. If you want the user to specify an object type, then make them type it in. You could help them out by trying to resolve the package for them based on the class name, but there could be multiple classes with the same name in different packages.

Perhaps you see now that there really could be no such facility as you ask, because there is no useful definition of "standard data type" other than the list I gave you (hope I didn't forget any).
 

Posts:6,750
Registered: 1/25/04
Re: data types  
Jul 23, 2004 4:22 PM (reply 3 of 12)



 
I guess kajbj either thinks or types faster than I do. :-)
 

Posts:18,384
Registered: 21.03.00
Re: data types  
Jul 23, 2004 4:26 PM (reply 4 of 12)



 
nasch_,
Who knows... But now I have to go to sleep. The time is 01.25 AM in Sweden. So now you have your chanse to answer first :)

/Kaj
 

Posts:18
Registered: 7/17/04
Re: data types  
Jul 23, 2004 9:05 PM (reply 5 of 12)



 
OK, how about this: how does Java figure out a valid type? For example:

I define four variables:

int myInt;
Double myDouble;
float myFloat;
String myString;
Potatoe floyd;

I have not defined a Potatoe class anywhere, so Java will complain. However, I have also not defined 'int', 'Double', 'float', or 'String'. So how does Java 'know' that they are valid types? A very lame and useless answer would be: because they are built in to Java. What I am asking is, where are they defined in Java? Or is there any place publicly available within the Java environment to find a list of these commonly used types?

As another example, if someone were to give you the task of writing a short program that lists these 'base' or built in data types, where would you get this information?

I am pretty confident that I could write a program in 'C' that could at least parse the header files stored on any system with a 'C' compiler to find a listing of the datatypes available to it. I have to believe Java has some way to do similar.

I call them standard types because they are built in to Java, and we can start using them as variables right away. In fact, we use them to build other types (classes) which we can of course in turn instantiate as variables. Maybe a better term would be 'base' types.

I hope this is clearer.

 

Posts:18
Registered: 7/17/04
Re: data types  
Jul 23, 2004 9:11 PM (reply 6 of 12)



 
other than the list I gave you (hope I didn't forget any).

This is the reason I am asking this. :-) If I can get Java itself to tell me, I won't get the chance to forget. Or if they were to add a new type, I would not have to go and add it myself.

If they are not easily found, I will type them in myself, but don't think this is a good solution. :-/

Thanks,

BillR
 

Posts:2,830
Registered: 9/1/03
Re: data types  
Jul 23, 2004 9:23 PM (reply 7 of 12)



 
other than the list I gave you (hope I didn't
forget any).


This is the reason I am asking this. :-) If I can
get Java itself to tell me, I won't get the chance to
forget. Or if they were to add a new type, I would
not have to go and add it myself.

Don't worry, I can't see sun ever adding any new primative types :)
 

Posts:37,103
Registered: 3/30/99
Re: data types  
Jul 23, 2004 9:55 PM (reply 8 of 12)



 
OK, how about this: how does Java figure out a valid
type? For example:

I define four variables:

int myInt;
Double myDouble;
float myFloat;
String myString;
Potatoe floyd;

I have not defined a Potatoe class anywhere, so Java
will complain. However, I have also not defined
'int', 'Double', 'float', or 'String'. So how does
Java 'know' that they are valid types?

The 8 primitive types that Java just "knows" about are byte, char, short, int, long, float, double, boolean. Those are "built in".

Object is probably also "built in" but may not be. I'm not totally sure. Other than that--String, Double, Potato--those it finds using classloaders, the most famous of which is the system classloader that searches the classpath.

A very lame

and useless answer would be: because they are built
in to Java. What I am asking is, where are they
defined in Java? Or is there any place publicly
available within the Java environment to find a list
of these commonly used types?

As another example, if someone were to give you the
task of writing a short program that lists these
'base' or built in data types, where would you get
this information?

I am pretty confident that I could write a program in
'C' that could at least parse the header files stored
on any system with a 'C' compiler to find a listing of
the datatypes available to it. I have to believe Java
has some way to do similar.

I call them standard types because they are built in
to Java, and we can start using them as variables
right away. In fact, we use them to build other types
(classes) which we can of course in turn instantiate
as variables. Maybe a better term would be 'base'
types.

I hope this is clearer.

 

Posts:18,384
Registered: 21.03.00
Re: data types  
Jul 24, 2004 4:07 AM (reply 9 of 12)



 
OK, how about this: how does Java figure out a valid
type? For example:

I define four variables:

int myInt;
Double myDouble;
float myFloat;
String myString;
Potatoe floyd;

I have not defined a Potatoe class anywhere, so Java
will complain. However, I have also not defined
'int', 'Double', 'float', or 'String'. So how does
Java 'know' that they are valid types?

This is basically what happens...
When you build a compiler you define reserved keywords, and a grammar for the language. The reserved keywords are 'words' that has a special meaning to the grammar. The grammar specifies in which order they can occure. int and float mentioned above are reserved keywoards that the grammar knows of. Double and String is not known to the grammar, so they have to be resolved, but since they are in the package java.lang you don't have to import them, but they still have to be on the classpath. You would not be able to compile the code above if you removed java.lang.String and java.lang.Double from the classpat (they are in rt.jar I think). Java will only complain if you don't have Potatoe on the classpath, it will compile fine when you adds it to the classpath, se there is no difference between String, Double and Potatoe.

So what you are talking about is different things.

Reserved keywords for types that can be used in calculations:
byte, char, short, int, long, float, double

Reserved keywords that a logical expression evaluates to:
boolean

And classes in java.lang that wrap the reserved keywords + the class String.

A compiler doesn't need a listing of the types since they are in the grammar for the compiler, or need to be resolved by the classloader.

/Kaj

Ps. What stated is above is just a sketch of how things work. If you want to know exactly how it works I would recommend you to take a class in compiler techniques.

 

Posts:6,750
Registered: 1/25/04
Re: data types  
Jul 27, 2004 8:59 AM (reply 10 of 12)



 
The 8 primitive types that Java just "knows" about are
byte, char, short, int, long, float, double, boolean.
Those are "built in".

These are documented in section 4.2 of the Java Language Specification. I'm sure they didn't forget any.

"The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing Unicode characters.

The floating-point types are float, whose values include the 32-bit IEEE 754 floating-point numbers, and double, whose values include the 64-bit IEEE 754 floating-point numbers.

The boolean type has exactly two values: true and false."
 

Posts:18
Registered: 7/17/04
Re: data types  
Jul 27, 2004 9:57 PM (reply 11 of 12)



 
Yeah... I was just hoping that there was some sort of reflection-ish way to list them. I already knew what the datatypes were. I guess I was just trying to be too fancy. :-)

BTW, I since looked into it, and there is no simple way to do it in C either...
 

Posts:5,965
Registered: 5/17/03
Re: data types  
Jul 27, 2004 11:11 PM (reply 12 of 12)



 
Does anyone know if there is a class, interface, or
property file somewhere (as part of the standard java
environment) that holds a descrete listing of all the
standard data types one could use to define a
variable (eg. int, String, booean, Double, etc.)?

Why not write a program which scans the documentation to the Java API for all available "standard" classes and interface types and builds the list. The primitive types are so few and never changing so if you build a default list by hand you're not likely to have to change it in a long time. You'll also have to add array and enum to that default list.
 
This topic has 12 replies on 1 page.