Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

"rename/redefine" int, double, etc in java?

Thread view: 
Kevin - 16 May 2006 07:00 GMT
Hello,

How can we define a "alias" for a basic java data type, such as int,
double?

Suppose we have the function of:

public my_type do_some_math(my_type varA, my_type varB, my_type varC)
{
 my_type temp_var = 0;
 temp_var = varA + varB;
 .....
 .....
 return temp_var;
}

Here is the question: I want the "my_type" can be "int", "double",
"long" depending on different situations (but only one type at a time).
So instead of writing 3 almost identical code, I can just modify one
line (which defines "my_type").

In C, I heard we can use the type define to define a custom type (a
"alias"). Then in all the code, we just use that custom type (alias).
Later, if necessary, we only need to modify this define line without
modifying everywhere in the code.

In Java, how can we do it?

Thanks a lot. :-)

(did anyone catch what I mean?)
Chris Uppal - 16 May 2006 07:18 GMT
> How can we define a "alias" for a basic java data type, such as int,
> double?

You can't.

Well, you can if you want to push your Java source though a pre-processor of
some kind which globablly replaces "my_type" with "int", but that's your only
option.

   -- chris
Kevin - 16 May 2006 08:14 GMT
hum...... so in this sense, c is "better" than java at this point
(actually, some c coder told me this trick in their code).

This is a very useful property, for example, if I want to target my
code for small data, I modify the code and let "my_type" to be "short",
since for small data (count) short is enough. And recomple the
executable (.jar) to send to end user.

And for a large data set, I modify my code and let my_type to be "long"
to hold very large values. And recomple the executable (.jar) to send
to end user.
This can save a lot of memory especially if I use "my_type[]" a lot in
the code.
Thomas Weidenfeller - 16 May 2006 08:56 GMT
> hum...... so in this sense, c is "better" than java at this point

If you want to do language advocacy, please go to
comp.lang.java.advocacy. F'up set.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Patricia Shanahan - 16 May 2006 14:30 GMT
>> hum...... so in this sense, c is "better" than java at this point
>
> If you want to do language advocacy, please go to
> comp.lang.java.advocacy. F'up set.
>
> /Thomas

I think it was a legitimate question, and, yes, in this area C and C++
have an advantage over Java.

Patricia
Dale King - 17 May 2006 03:57 GMT
>>> hum...... so in this sense, c is "better" than java at this point
>>
[quoted text clipped - 5 lines]
> I think it was a legitimate question, and, yes, in this area C and C++
> have an advantage over Java.

I don't really see this as any real advantage. All it let's you do is
give a new name to a type. How would that be of much use in Java? In C
much of its use for primitives is predicated on the existence of
conditional compilation in the preprocessor. Without a preprocessor
there isn't much it is useful for.

No there are languages which really let you create new types that do
have an advantage. The one I am familiar with is Modula. In it you can
do something like:

TYPE
   Hertz = INTEGER;
   Inches = INTEGER;
VAR
   frequency : Hertz;
   length : Inches;

Even though these two types are both integral they are not assignment
compatible because they are different types. Code like the following
would give a compile error:

    frequency := length;

Which makes sense because they two really are two different types and it
makes little sense to treat one as a value in the other. In order to do
it you would have to explicitly cast the value to tell the compiler that
is really what you meant.

The equivalent code in C happily compiles without complaint:

typedef int Hertz;
typedef int Inches;

Hertz frequency;
Inches length;
...
    frequency = length;

I could see a lot of value for the Modula way of creating types, but of
course it is too late now as it would require a tear-up of the whole
class file format.
Signature

 Dale King

Chris Uppal - 16 May 2006 15:54 GMT
> hum...... so in this sense, c is "better" than java at this point
> (actually, some c coder told me this trick in their code).

In that sense, yes.  I can think of several other ways that C is better than
Java too.

C is a nice language -- I've always enjoyed programming in it.  Sadly, it just
isn't suitable for today's environment where hostile data lurks around every
corner...

   -- chris
Kevin - 16 May 2006 17:48 GMT
###########################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
Hi Guys,

Please do not feel offended.
I do NOT intend to say which language is better.
I just want to confirm the stated point since I can not find a clear
specification about this anywhere.

Thanks.
###########################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
Roedy Green - 21 May 2006 19:18 GMT
>Please do not feel offended.

Nobody is offended. The problem is those sort of statement tend to
trigger endless threads nothing to do with the original topic. We like
to confine them to comp.lang.java.advocacy.

It is the same reason you often avoid the topic of religion or
politics unless you intent to spend a long time discussing them.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 16 May 2006 22:21 GMT
> hum...... so in this sense, c is "better" than java at this point
> (actually, some c coder told me this trick in their code).
[quoted text clipped - 3 lines]
> since for small data (count) short is enough. And recomple the
> executable (.jar) to send to end user.

[The context is having a method work on short, int and long]

With 1.5, you could use autoboxing and generis. Something like:

<pseudoCode strictnessLevel="off the top of my head">
public <T extends Number> T myMethod(T a, T b) {
 return a.longValue() + b.longValue(); /*Not sure if this part will
compile.*/
}
</pseudoCode>

> And for a large data set, I modify my code and let my_type to be "long"
> to hold very large values. And recomple the executable (.jar) to send
> to end user.
> This can save a lot of memory especially if I use "my_type[]" a lot in
> the code.

   Using autoboxing probably won't help if the goal is to minimize memory
usage though.

   - Oliver
Timo Stamm - 17 May 2006 00:34 GMT
Oliver Wong schrieb:
>> hum...... so in this sense, c is "better" than java at this point
>> (actually, some c coder told me this trick in their code).
[quoted text clipped - 14 lines]
> }
> </pseudoCode>

Even if it did compile, it would not behave very well. The call

  myMethod(new Float(0.5), new Float(0.5));

would evaluate to 0.

Timo
Steve Wampler - 16 May 2006 18:00 GMT
> Hello,
>
[quoted text clipped - 16 lines]
> So instead of writing 3 almost identical code, I can just modify one
> line (which defines "my_type").

As you've learned, you can't do this the same way in Java, but you might
look at Java 'generics' - they can [he thinks, without actually trying
it himself] provide a similar, though not identical functionality.
Of course, this will likely have to be done using the Integer, Double, Long,
etc. classes instead of the primitives.  This may or may not be suitable to
your needs.

(In some ways, Java's approach is superior to C's [heh, couldn't resist]
in that you can use the same method definition to work with 'Integer' in one
place in your program while simultaneously working with 'Double' in another.)

Signature

Steve Wampler -- swampler@noao.edu
The gods that smiled on your birth are now laughing out loud.

Timo Stamm - 16 May 2006 18:45 GMT
Steve Wampler schrieb:
> As you've learned, you can't do this the same way in Java, but you might
> look at Java 'generics' - they can [he thinks, without actually trying
> it himself] provide a similar, though not identical functionality.
> Of course, this will likely have to be done using the Integer, Double, Long,
> etc. classes instead of the primitives.  This may or may not be suitable to
> your needs.

I had exactly the same thought.

> (In some ways, Java's approach is superior to C's [heh, couldn't resist]
> in that you can use the same method definition to work with 'Integer' in one
> place in your program while simultaneously working with 'Double' in another.)

Sure, you can create a method with such a signature:

  public <T extends Number> T doSomeMath(T a, T b)

But it is useless because the java.lang.Number only provides a few
methods to retrieve primitive types of the number value, so you can't do
anything with the objects without converting or casting them. If
java.lang.Number had the math methods of java.math.BigDecimal, the idea
might be useful, but I would miss mathematical operators.

Timo
Steve Wampler - 16 May 2006 18:58 GMT
> Sure, you can create a method with such a signature:
>
[quoted text clipped - 5 lines]
> java.lang.Number had the math methods of java.math.BigDecimal, the idea
> might be useful, but I would miss mathematical operators.

Oh right. (D'oh).  Sooner or later you have to get down to the primitives
to do the mathematical operations, unless you define your own set of
numeric-valued classes.  (uh, "nevermind", in my best Gilda Radnor voice).

Signature

Steve Wampler -- swampler@noao.edu
The gods that smiled on your birth are now laughing out loud.

Andrew McDonagh - 16 May 2006 18:28 GMT
> Hello,
>
[quoted text clipped - 27 lines]
>
> (did anyone catch what I mean?)

We did catch your meaning.

Like the others have said - there is no way to do this.

There is no way, because its not something you need nor want to do in a
platform independent language like Java.  Distributing Java code that
used a short, int or long would for most JVMs make no actual difference.

Whilst the JVM Spec talks about Integral Type ranges:

"3.2.1 Integral Types and Values
The values of the integral types of the Java Virtual Machine are the
same as those for the integral types of the Java language (§2.4.1):

    * For byte, from -128 to 127 (-27 to 27-1), inclusive
    * For short, from -32768 to 32767 (-215 to 215-1), inclusive
    * For int, from -2147483648 to 2147483647 (-231 to 231-1), inclusive
    * For long, from -9223372036854775808 to 9223372036854775807 (-263
to 263-1), inclusive
    * For char, from 'u0000' to 'uffff'; char is unsigned, so 'uffff'
represents 65535 when used in expressions, not -1  "

It does NOT talk about their storage.

"3.4 Words
No mention has been made of the storage requirements for values of the
various Java Virtual Machine types, only the ranges those values may
take. The Java Virtual Machine does not mandate the size of its data
types. Instead, the Java Virtual Machine defines an abstract notion of a
word that has a platform-specific size. A word is large enough to hold a
value of type byte, char, short, int, float, reference, or
returnAddress, or to hold a native pointer. Two words are large enough
to hold values of the larger types, long and double. Java's runtime data
areas are all defined in terms of these abstract words.

A word is usually the size of a pointer on the host platform. On a
32-bit platform, a word is 32 bits, pointers are 32 bits, and longs and
doubles naturally take up two words. A naive 64-bit implementation of
the Java Virtual Machine may waste half of a word used to store a 32-bit
datum, but may also be able to store all of a long or a double in one of
the two words allotted to it.

The choice of a specific word size, although platform-specific, is made
at the implementation level, not as part of the Java Virtual Machine's
design. It is not visible outside the implementation or to code compiled
for the Java Virtual Machine."
Steve Wampler - 16 May 2006 18:54 GMT
>> Hello,
>>
>> How can we define a "alias" for a basic java data type, such as int,
>> double?
...
> There is no way, because its not something you need nor want to do in a
> platform independent language like Java.  Distributing Java code that
> used a short, int or long would for most JVMs make no actual difference.

Note that this person is not restricting themselves to just the integral
types but includes floating point types as well.  That does make a
difference to all JVMs (I hope!).

Signature

Steve Wampler -- swampler@noao.edu
The gods that smiled on your birth are now laughing out loud.

Roedy Green - 21 May 2006 19:15 GMT
>How can we define a "alias" for a basic java data type, such as int,
>double?

you can't. The closest you can come is a Wrapper class with an int,
double etc inside.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.