/* Byte.java -- object wrapper for byte
Copyright (C) 1998, 2001 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package java.lang;
/**
* Instances of class Byte
represent primitive byte
* values.
*
* Additionally, this class provides various helper functions and variables
* useful to bytes.
*
* @author Paul Fisher
* @author John Keiser
* @author Per Bothner
* @since JDK 1.0
*/
public final class Byte extends Number implements Comparable
{
static final long serialVersionUID = -7183698231559129828L;
/**
* The minimum value a byte
can represent is -128.
*/
public static final byte MIN_VALUE = -128;
/**
* The maximum value a byte
can represent is 127.
*/
public static final byte MAX_VALUE = 127;
/**
* The primitive type byte
is represented by this
* Class
object.
*/
public static final Class TYPE = VMClassLoader.getPrimitiveClass('B');
/**
* The immutable value of this Byte.
*/
private final byte value;
/**
* Create a Byte
object representing the value of the
* byte
argument.
*
* @param value the value to use
*/
public Byte(byte value)
{
this.value = value;
}
/**
* Create a Byte
object representing the value specified
* by the String
argument.
*
* @param s the string to convert.
*/
public Byte(String s) throws NumberFormatException
{
value = parseByte(s, 10);
}
/**
* Return a hashcode representing this Object.
*
* Byte
's hash code is calculated by simply returning its
* value.
*
* @return this Object's hash code.
*/
public int hashCode()
{
return value;
}
/**
* Returns true
if obj
is an instance of
* Byte
and represents the same byte value.
* @return whether these Objects are semantically equal.
*/
public boolean equals(Object obj)
{
return ((obj instanceof Byte) && (value == ((Byte)obj).byteValue()));
}
/**
* Converts the byte
to a String
and assumes
* a radix of 10.
* @param i the byte
to convert to String
* @return the String
representation of the argument.
*/
public static String toString(byte i)
{
return Integer.toString ((int) i);
}
/**
* Converts the Byte
value to a String
and
* assumes a radix of 10.
* @return the String
representation of this Byte
.
*/
public String toString()
{
return Integer.toString ((int) value);
}
/**
* Creates a new Byte
object using the String
,
* assuming a radix of 10.
* @param s the String
to convert.
* @return the new Byte
.
* @see #Byte(java.lang.String)
* @see #parseByte(java.lang.String)
* @exception NumberFormatException thrown if the String
* cannot be parsed as a byte
.
*/
public static Byte valueOf(String s) throws NumberFormatException
{
return new Byte(parseByte(s));
}
/**
* Creates a new Byte
object using the String
* and specified radix (base).
* @param s the String
to convert.
* @param radix the radix (base) to convert with.
* @return the new Byte
.
* @see #parseByte(java.lang.String,int)
* @exception NumberFormatException thrown if the String
* cannot be parsed as a byte
.
*/
public static Byte valueOf(String s, int radix)
throws NumberFormatException
{
return new Byte(parseByte(s, radix));
}
/**
* Converts the specified String
into a byte
.
* This function assumes a radix of 10.
*
* @param s the String
to convert
* @return the byte
value of the String
* argument.
* @exception NumberFormatException thrown if the String
* cannot be parsed as a byte
.
*/
public static byte parseByte(String s) throws NumberFormatException
{
return parseByte(s, 10);
}
/**
* Converts the specified String
into a byte
* using the specified radix (base).
*
* @param str the String
to convert
* @param radix the radix (base) to use in the conversion
* @return the String
argument converted to byte.
* @exception NumberFormatException thrown if the String
* cannot be parsed as a byte
.
*/
public static byte parseByte(String str, int radix)
throws NumberFormatException
{
int i = Integer.parseInt(str, radix);
if (i < MIN_VALUE || i > MAX_VALUE)
throw new NumberFormatException();
return (byte) i;
}
/**
* Convert the specified String
into a Byte
.
* The String
may represent decimal, hexadecimal, or
* octal numbers.
*
* The String
argument is interpreted based on the leading
* characters. Depending on what the String begins with, the base will be
* interpreted differently:
*
*
Leading Characters | Base |
---|---|
# | 16 |
0x | 16 |
0X | 16 |
0 | 8 |
Anything Else | 10 |
String
to interpret.
* @return the value of the String as a Byte
.
* @exception NumberFormatException thrown if the String
* cannot be parsed as a byte
.
*/
public static Byte decode(String str) throws NumberFormatException
{
int i = (Integer.decode(str)).intValue();
if (i < MIN_VALUE || i > MAX_VALUE)
throw new NumberFormatException();
return new Byte((byte) i);
}
/** Return the value of this Byte
as an short
.
** @return the value of this Byte
as an short
.
**/
public byte byteValue()
{
return value;
}
/** Return the value of this Byte
as an short
.
** @return the value of this Byte
as an short
.
**/
public short shortValue()
{
return value;
}
/** Return the value of this Byte
as an int
.
** @return the value of this Byte
as an int
.
**/
public int intValue()
{
return value;
}
/** Return the value of this Byte
as a long
.
** @return the value of this Byte
as a long
.
**/
public long longValue()
{
return value;
}
/** Return the value of this Byte
as a float
.
** @return the value of this Byte
as a float
.
**/
public float floatValue()
{
return value;
}
/** Return the value of this Byte
as a double
.
** @return the value of this Byte
as a double
.
**/
public double doubleValue()
{
return value;
}
/**
* Compare two Bytes numerically by comparing their
* byte
values.
* @return a positive value if this Byte
is greater
* in value than the argument Byte
; a negative value
* if this Byte
is smaller in value than the argument
* Byte
; and 0
, zero, if this
* Byte
is equal in value to the argument
* Byte
.
*/
public int compareTo(Byte b)
{
return (int)(value - b.byteValue());
}
/**
* Behaves like compareTo(java.lang.Byte)
unless the Object
* is not a Byte
. Then it throws a
* ClassCastException
.
* @exception ClassCastException if the argument is not a
* Byte
.
*/
public int compareTo(Object o)
{
return compareTo((Byte)o);
}
}