Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I am learning GoF Java Design Patterns and I want to see some real life examples of them. Can you guys point to some good usage of these Design Patterns.(preferably in Java's core libraries). Thank you
I want to make a growable array of bytes. I.e a list. In c# would usally do the following syntax List<byte> mylist = new List<byte>(); where as in java this syntax does not work and I have googled around and found the below code List myList = new ArrayList(); but that is not what I want. Any idea's where I am going wrong?
I am curious. Why do I have to type String myStr with a capital letter whereas I type int aNumba with a lower-case letter?
I have a string that looks like this: ["1011000", "1000010", "1001101", "1000011"]. My argument is coming from elsewhere so it needs to be this way. I need to typecast this to a real byte array. Here's my method: public void send(String[] payloadarr) throws IOException { byte [] payload = {}; for (int i = 0; i < payloadarr.length; i++) { byte x = (byte) payloadarr[i]; ...
I am coming across a strange thing. I have a number in binary in the form of string particularly "01001100". But I am getting the exception mentioned above by executing the following code. String s = "01001100"; byte b = Byte.parseByte(s); But why is it happening? Whereas in a byte we can store max no. upto 127 and min. upto -128. And the decimal equivalent of the above number is 76 which is ...
For a project, I have to convert a binary string into (an array of) bytes and write it out to a file in binary. Say that I have a sentence converted into a code string using a huffman encoding. For example, if the sentence was: "hello" h = 00 e = 01, l = 10, o = 11 Then the string representation would be 0001101011. How would I convert that into a byte? <-- If that question doesn't make s...
Java 6 API primitive type wrappers have pairs of static methods decode(String s) and valueOf(String s). Both of them return a new object of wrapper class type and none of them are annotated as deprecated. Does someone know the difference between them? For example: Byte b1 = Byte.decode("10"); and Byte b2 = Byte.valueOf("10");
I'm just beginning to learn about file compression and I've run into a bit of a roadblock. I have an application that will encode a string such as "program" as a compressed binary representation "010100111111011000"(note this is still stored as a String). Encoding g 111 r 10 a 110 p 010 o 011 m 00 Now I need to write this to the file system using a FileOut...
Hello Can anyone help me to know how can I have a "Set" of Bytes in Java? Thank you
I am reading 8 bytes of data in from a hardware device. I need to convert them into a numeric value. I think I want to convert them to a long as that should fit 8 bytes. I am not very familiar with Java and low level data type operations. I seem to have two problems (apart from the fact there is almost no documentation for the hardware in question), The bytes are expecting to be unsigned, so...
I got an exception while parsing a string to byte String Str ="9B7D2C34A366BF890C730641E6CECF6F"; String [] st=Str.split("(?<=\\G.{2})"); byte[]bytes = new byte[st.length]; for (int i = 0; i <st.length; i++) { bytes[i] = Byte.parseByte(st[i],16); }
what is equivalent to negative infinity in java?−∞
Let's say I have the string "1e". In Java, how can I convert it to the byte 0x1e? I got both of the methods to work. However, then I found out that this wasn't what I needed to do anyway...
public static String asHex (byte buf[]) { StringBuffer strbuf = new StringBuffer(buf.length * 2); int i; for (i = 0; i < buf.length; i++) { if (((int) buf[i] & 0xff) < 0x10) strbuf.append("0"); strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); } return strbuf.toString(); }
what is the difference between this two Byte i1=new Byte(1);//complier error byte b=1;//ok my question is about assigning the value 1 to byte where 1 is int literal. but when passing 1 to the Byte class constructor it gives error
Context - GraniteDS + JPA DataNucleus + MySQL The scenario is to store / retrieve an image in db. The issue is with the retrieval FROM db. The book cover is fetched from the db but at the flex front it shows as null. The translation from java "Byte[]" to flex "ByteArray" is returning null. Below are the 2 class files. AcrionScript class [RemoteClass(alias="com.app.model.Book")] public cla...
I'm having a small error in my code that I can not for the life of me figure out. I have an array of strings that are representations of binary data (after converting them from hex) for example: one index is 1011 and another is 11100. I go through the array and pad each index with 0's so that each index is eight bytes. When I try to convert these representations into actual bytes I get an erro...
I am doing a assignment about this. For Eaxmple: I have a message (String), and the length is 302 in dec, 12e in hex. String message = "THE MESSAGE BODY"; int lengthOfMessage = number.length(); // 302 String lengthOfMessageInHex = Integer.toHexString(lengthOfMessage); // 12e Now, I need to change the lengthOfMessageInHex from "12e" to "0000012e". lengthOfMessageInHex = ("00000000" + length...
  /*
   * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code 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
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.lang;

The Byte class wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte.

In addition, this class provides several methods for converting a byte to a String and a String to a byte, as well as other constants and methods useful when dealing with a byte.

Author(s):
Nakul Saraiya
Joseph D. Darcy
Since:
JDK1.1
See also:
Number
 
 public final class Byte extends Number implements Comparable<Byte> {

    
A constant holding the minimum value a byte can have, -27.
 
     public static final byte   MIN_VALUE = -128;

    
A constant holding the maximum value a byte can have, 27-1.
 
     public static final byte   MAX_VALUE = 127;

    
The Class instance representing the primitive type byte.
 
     public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");

    
Returns a new String object representing the specified byte. The radix is assumed to be 10.

Parameters:
b the byte to be converted
Returns:
the string representation of the specified byte
See also:
Integer.toString(int)
 
     public static String toString(byte b) {
         return Integer.toString((int)b, 10);
     }
 
     private static class ByteCache {
         private ByteCache(){}
 
         static final Byte cache[] = new Byte[-(-128) + 127 + 1];
 
         static {
             for(int i = 0; i < .i++)
                 [i] = new Byte((byte)(i - 128));
         }
     }

    
Returns a Byte instance representing the specified byte value. If a new Byte instance is not required, this method should generally be used in preference to the constructor Byte(byte), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

Parameters:
b a byte value.
Returns:
a Byte instance representing b.
Since:
1.5
 
    public static Byte valueOf(byte b) {
        final int offset = 128;
        return .[(int)b + offset];
    }

    
Parses the string argument as a signed byte in the radix specified by the second argument. The characters in the string must all be digits, of the specified radix (as determined by whether Character.digit(char,int) returns a nonnegative value) except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting byte value is returned.

An exception of type NumberFormatException is thrown if any of the following situations occurs:

  • The first argument is null or is a string of length zero.
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
  • Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1.
  • The value represented by the string is not a value of type byte.

Parameters:
s the String containing the byte representation to be parsed
radix the radix to be used while parsing s
Returns:
the byte value represented by the string argument in the specified radix
Throws:
NumberFormatException If the string does not contain a parsable byte.
    public static byte parseByte(String sint radix)
        throws NumberFormatException {
        int i = Integer.parseInt(sradix);
        if (i <  || i > )
            throw new NumberFormatException(
                "Value out of range. Value:\"" + s + "\" Radix:" + radix);
        return (byte)i;
    }

    
Parses the string argument as a signed decimal byte. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting byte value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseByte(java.lang.String,int) method.

Parameters:
s a String containing the byte representation to be parsed
Returns:
the byte value represented by the argument in decimal
Throws:
NumberFormatException if the string does not contain a parsable byte.
    public static byte parseByte(String sthrows NumberFormatException {
        return parseByte(s, 10);
    }

    
Returns a Byte object holding the value extracted from the specified String when parsed with the radix given by the second argument. The first argument is interpreted as representing a signed byte in the radix specified by the second argument, exactly as if the argument were given to the parseByte(java.lang.String,int) method. The result is a Byte object that represents the byte value specified by the string.

In other words, this method returns a Byte object equal to the value of:

new Byte(Byte.parseByte(s, radix))

Parameters:
s the string to be parsed
radix the radix to be used in interpreting s
Returns:
a Byte object holding the value represented by the string argument in the specified radix.
Throws:
NumberFormatException If the String does not contain a parsable byte.
    public static Byte valueOf(String sint radix)
        throws NumberFormatException {
        return new Byte(parseByte(sradix));
    }

    
Returns a Byte object holding the value given by the specified String. The argument is interpreted as representing a signed decimal byte, exactly as if the argument were given to the parseByte(java.lang.String) method. The result is a Byte object that represents the byte value specified by the string.

In other words, this method returns a Byte object equal to the value of:

new Byte(Byte.parseByte(s))

Parameters:
s the string to be parsed
Returns:
a Byte object holding the value represented by the string argument
Throws:
NumberFormatException If the String does not contain a parsable byte.
    public static Byte valueOf(String sthrows NumberFormatException {
        return valueOf(s, 10);
    }

    
Decodes a String into a Byte. Accepts decimal, hexadecimal, and octal numbers given by the following grammar:
DecodableString:
Signopt DecimalNumeral
Signopt 0x HexDigits
Signopt 0X HexDigits
Signopt # HexDigits
Signopt 0 OctalDigits

Sign:
-

DecimalNumeral, HexDigits, and OctalDigits are defined in §3.10.1 of the Java Language Specification.

The sequence of characters following an (optional) negative sign and/or radix specifier ("0x", "0X", "#", or leading zero) is parsed as by the Byte.parseByte method with the indicated radix (10, 16, or 8). This sequence of characters must represent a positive value or a NumberFormatException will be thrown. The result is negated if first character of the specified String is the minus sign. No whitespace characters are permitted in the String.

Parameters:
nm the String to decode.
Returns:
a Byte object holding the byte value represented by nm
Throws:
NumberFormatException if the String does not contain a parsable byte.
See also:
parseByte(java.lang.String,int)
    public static Byte decode(String nmthrows NumberFormatException {
        int i = Integer.decode(nm);
        if (i <  || i > )
            throw new NumberFormatException(
                    "Value " + i + " out of range from input " + nm);
        return (byte)i;
    }

    
The value of the Byte.

Serial:
    private final byte value;

    
Constructs a newly allocated Byte object that represents the specified byte value.

Parameters:
value the value to be represented by the Byte.
    public Byte(byte value) {
        this. = value;
    }

    
Constructs a newly allocated Byte object that represents the byte value indicated by the String parameter. The string is converted to a byte value in exactly the manner used by the parseByte method for radix 10.

Parameters:
s the String to be converted to a Byte
Throws:
NumberFormatException If the String does not contain a parsable byte.
See also:
parseByte(java.lang.String,int)
    public Byte(String sthrows NumberFormatException {
        this. = parseByte(s, 10);
    }

    
Returns the value of this Byte as a byte.
    public byte byteValue() {
        return ;
    }

    
Returns the value of this Byte as a short.
    public short shortValue() {
        return (short);
    }

    
Returns the value of this Byte as an int.
    public int intValue() {
        return (int);
    }

    
Returns the value of this Byte as a long.
    public long longValue() {
        return (long);
    }

    
Returns the value of this Byte as a float.
    public float floatValue() {
        return (float);
    }

    
Returns the value of this Byte as a double.
    public double doubleValue() {
        return (double);
    }

    
Returns a String object representing this Byte's value. The value is converted to signed decimal representation and returned as a string, exactly as if the byte value were given as an argument to the toString(byte) method.

Returns:
a string representation of the value of this object in base 10.
    public String toString() {
        return String.valueOf((int));
    }

    
Returns a hash code for this Byte.
    public int hashCode() {
        return (int);
    }

    
Compares this object to the specified object. The result is true if and only if the argument is not null and is a Byte object that contains the same byte value as this object.

Parameters:
obj the object to compare with
Returns:
true if the objects are the same; false otherwise.
    public boolean equals(Object obj) {
        if (obj instanceof Byte) {
            return  == ((Byte)obj).byteValue();
        }
        return false;
    }

    
Compares two Byte objects numerically.

Parameters:
anotherByte the Byte to be compared.
Returns:
the value 0 if this Byte is equal to the argument Byte; a value less than 0 if this Byte is numerically less than the argument Byte; and a value greater than 0 if this Byte is numerically greater than the argument Byte (signed comparison).
Since:
1.2
    public int compareTo(Byte anotherByte) {
        return this. - anotherByte.value;
    }

    
The number of bits used to represent a byte value in two's complement binary form.

Since:
1.5
    public static final int SIZE = 8;

    
use serialVersionUID from JDK 1.1. for interoperability
    private static final long serialVersionUID = -7183698231559129828L;
New to GrepCode? Check out our FAQ X