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 / July 2006

Tip: Looking for answers? Try searching our database.

2  questions

Thread view: 
gk - 14 Jul 2006 19:14 GMT
i have 2 questions..

Q1:Why do you create interfaces, and when MUST you use one?

Q2: can a double value be casted into byte ?how ?  wont there be loss
of precison ?

double=8 bytes ? is not it ?

please respond

thanks
Paul Davis - 14 Jul 2006 19:50 GMT
Q1 has too many different answers to get a simple reply. Pick up a copy
of "Effective Java" and use that as your guide.

Re: Q2:
yes a double can be cast to a byte. Yes, there will absolutely be a
loss of precission. in fact, you may not even recognize your value.
double dbl = 9876.341;
byte b = (byte)dbl;
// b now equals -127

When you cast a double to a byte, the compiler will generate two
different instructions having the mnemonics d2i and i2b (double to
integer and integer to byte).
d2i basically drops any fraction and if your double is really large,
you are going to lose even more
i2b is the equivalent of copying over the last 8 bits, in other words
intVal & 0xFF

So, going double to a byte is like trying to pour eight gallons into a
one gallon bucket.
Oliver Wong - 14 Jul 2006 19:57 GMT
>i have 2 questions..
>
> Q1:Why do you create interfaces, and when MUST you use one?

   This one question alone is actually composed of two sub-questions. The
answer to the first sub-question is long and abstract. Read
http://java.sun.com/docs/books/tutorial/java/concepts/interface.html

   For the second sub-question, it's never the case that you *MUST* use an
interface, in the Turing Complete sense; that is, for any Java program which
uses interfaces, it should always be possible to write a different program
which doesn't use interfaces, but which is indistinguishable from the first
according to the end-user.

   That said, interfaces are useful in that you can implement multiple
interfaces, but only extend one class.

   This next question is actually 5 questions combined together.

> Q2: can a double value be casted into byte ?

   Yes.

> how ?

   double foo = 1.0;
   byte bar = (byte) foo;

>  wont there be loss
> of precison ?

   Most of the time, yes.

> double=8 bytes ?

   On most (all?) implementations, probably yes. However, IMHO it's better
to think of the range of values than the actual bit-width. Double ranges
between negative Double.MAX_VALUE and positive Double.MAX_VALUE.
Double.MAX_VALUE is 1.7976931348623157e+308. The actual bit-width isn't that
important. The JVM for a 128 bit processor might allocate 16 bytes for
double, for example, and just ignore the top 8 bytes.

> is not it ?

   That's not it. See above.

   - Oliver
gk - 20 Jul 2006 15:02 GMT
thanks you

thas a beautiful way to answer.
i appreciate it.
thank you

> >i have 2 questions..
> >
[quoted text clipped - 43 lines]
>
>     - Oliver


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.