Re: newbie lost in strings......
From: Grant Wagner (gwagner_at_agricoreunited.com)
Date: 07/30/04
- Next message: Stewart Gordon: "Re: GOF"
- Previous message: IOANNS MANOLOUDIS: "My shell can't find Java"
- In reply to: Carl Smotricz: "Re: newbie lost in strings......"
- Next in thread: kaeli: "Re: newbie lost in strings......"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 30 Jul 2004 16:25:45 GMT
The reason you gave is why a byte can contain the values -128 to 127.
The reason I gave is why Byte.MAX_VALUE returns 127. The only reason Byte returns
127 is because someone at Sun wrote:
public final class Byte extends Number implements Comparable {
/**
* A constant holding the maximum value a <code>byte</code> can
* have, 2<sup>7</sup>-1.
*/
public static final byte MAX_VALUE = 127;
If someone suddenly decided a primitive byte in the VM was 12 bits wide, Byte would
have be re-written to be:
public static final int MAX_VALUE = 2047;
(which would require casts anywhere Byte.MAX_VALUE is used)
The important point is there is no "magic" going on here, Byte.MAX_VALUE is just
returning a public static final byte that contains a particular value. By happy
coincidence (actually design) that particular value happens to match the maximum
value a primitive byte can hold, but Byte doesn't *know* that.
Carl Smotricz wrote:
> I'd like to take a crack at answering why byte goes to 127!
>
> A byte (in the general, not the Java sense) is usually an 8 bit
> quantity; the 8 bits can be thought of as representing the positive
> integers from 0 (no bits set) to 255 (all 8 bits set).
>
> Java, however, rules that all integral primitive types (byte, int and
> long) shall hold *signed* values. This means one bit is used as a sign
> bit, with the advantage that those (in the case of byte) 8 bits can now
> hold negative numbers as well, but the magnitude is a bit less. Since
> 2's complement is used for the representation, the values actually go
> from -128 to 127.
>
> Hope that helps.
>
> -Carl-
-- Grant Wagner <gwagner@agricoreunited.com>
- Next message: Stewart Gordon: "Re: GOF"
- Previous message: IOANNS MANOLOUDIS: "My shell can't find Java"
- In reply to: Carl Smotricz: "Re: newbie lost in strings......"
- Next in thread: kaeli: "Re: newbie lost in strings......"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|