String, text, numbers, I/O II part
manipulation to extract the pieces of information encoded in the bytes. There are
other situations where bitwise manipulation is used, as well.
by Greger Ohlson
 Why cannot I cast from double to java.lang.Object?
Q: I m trying to build a vector, however, one of the objects that I m passing to the
vector is of type double. How do I cast the double as an object so that I may insert
the value into a vector? Does this make sense? Here is the following snippet of code
I was trying to use:
myVector.add (1, (Object)myDouble);
Of course when I try to compile I get the following message:
Invalid cast from double to java.lang.Object
Could someone please explain why? I realize that Object is the mother of all objects
and therefore ANY reference data type "is an" Object. So therefore I shouldn t have
to cast the double, right? Help, I m a beginner!
Answer: A double is not a reference type, but a primitive one. Hence, it doesn t
inherit from Object (or anything else, for that matter). To put primitives (byte, short,
int, long, float, double, boolean, char) into something that requires an Object, use
Java s wrapper classes.
The wrapper classes are Double, Integer, Long, Boolean, etc., and are basically an
object "wrapped" around a primitive type. You make a Double object by:
Double d = new Double (myDouble);
and to get the actual value back,
double z = d.doubleValue();
It works the same way for all the rest of the primitive/wrapper pairs.
by Trevor Hill
Q: is there a mod (x, y) function that returns the remainder when x is divided by
y? Something equivalent to fmod(x,y) in C?
Answer: a = x%y;
I m having trouble figuring out how to convert characters to their ASCII value in
java
Q: I m having trouble figuring out how to convert characters to their ASCII
value in java. Is there a class like NumberFormat that will do it?
Answer: I can t see any problem here:
char ch =  A ; // character  A 
int i = (int)ch; // ASCII value for  A  (=>65)
Yes. And just be aware that ASCII only runs from 0 through 127. Anything
higher needs to be addressed differently, since Java is using Unicode values.
 Q: I m writing a program that makes I/O on a (huge) file.
file:///F|/a_jsite/350_tips/stings_text__date_numbers_io II.htm (6 of 7) [2001 07 08 11:25:00]






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java web hosting

 

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved