[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[no subject]
- <!--x-content-type: text/plain -->
- <!--x-date: Wed Dec 1 10:06:06 2004 -->
- <!--x-from-r13: wnfbaqnl ng jbeyqarg.ngg.arg (Xnfba Rnl) -->
- <!--x-message-id: [email protected] -->
- <!--x-reference: [email protected] -->
- <!--x-reference: [email protected] --> "http://www.w3.org/TR/html4/loose.dtd">
- <!--x-subject: OT: java quirks Was: Re: [ale] Introduction -->
- <h1>ot: java quirks Was: Re: [ale] Introduction</h1>
- <li><em>date</em>: Wed Dec 1 10:06:06 2004</li>
- <li><em>from</em>: jasonday at worldnet.att.net (Jason Day)</li>
- <li><em>in-reply-to</em>: <[email protected]></li>
- <li><em>references</em>: <[email protected]> <[email protected]></li>
- <li><em>subject</em>: OT: java quirks Was: Re: [ale] Introduction</li>
- <title>ot: java quirks Was: Re: [ale] Introduction</title>
Sure you can. You just have to realize that the JVM can only perform
arithmetic on ints and longs, and consequently whenever you do
arithmetic on a data type that's smaller than an int, the JVM will
automatically promote (and sign extend!!!) it to an int. So,
int diff = 'd' - 'a';
is ok, but
char diff = 'd' - 'a';
is not, because the chars are promoted to ints to do the subtraction,
and the result is an int, and you can't stuff an int into a char without
an explicit cast. So, you could do this:
char diff = (char)('d' - 'a');
if you really wanted to, but you probably want the diff as an int
anyway.
The same goes for bytes as well, but there's an extra hurdle because
bytes are signed, and the JVM sign-extends data types when it promotes
them. So if you have a byte value that's greater than 127, and add it
to another byte, you won't get what you expected. You have to first
explicitly cast it to an int and mask off the sign bit. Fun.
Sorry for the lecture; I've just done way too much bit twiddling in
Java. Don't get me started on the Date and Calendar classes. Or the
StreamTokenizer class *shudder*.
Jason
--
Jason Day jasonday at
<a rel="nofollow" href="http://jasonday.home.att.net">http://jasonday.home.att.net</a> worldnet dot att dot net
"Of course I'm paranoid, everyone is trying to kill me."
-- Weyoun-6, Star Trek: Deep Space 9
</pre>
<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<hr>
<!--X-Follow-Ups-End-->
<!--X-References-->
<!--X-References-End-->
<!--X-BotPNI-->
<ul>
<li>Prev by Date:
<strong><a href="msg00030.html">[ale] Linux PIM</a></strong>
</li>
<li>Next by Date:
<strong><a href="msg00032.html">[ale] OT: solaris on a laptop?</a></strong>
</li>
<li>Previous by thread:
<strong><a href="msg00129.html">[ale] Linux PIM</a></strong>
</li>
<li>Next by thread:
<strong><a href="msg00046.html">[ale] SCSI Tape Drive with SUSE 9.2</a></strong>
</li>
<li>Index(es):
<ul>
<li><a href="maillist.html#00031"><strong>Date</strong></a></li>
<li><a href="threads.html#00031"><strong>Thread</strong></a></li>
</ul>
</li>
</ul>
<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
</body>
</html>