Results 1 to 3 of 3
  1. #1
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

    Default using Byte arrays

    I was exploring byte arrays. Review the code below:

    Java Code:
    		String str = "Java World for 1.4";
    		byte bArray[] = new byte[str.length()];
    		
    		bArray = str.getBytes();
    		
    		System.out.println(bArray);
    Output:

    Java Code:
    [B@3e25a5
    So, byte arrays are not to simply keep and data in byte form and to display it. Why we use byte/byte arrays?

    Thanks in advance.

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    One of the best example would be encoding/decoding or encryption where you need to go over the each bytes and bits .. not just storing and displaying .. Hope it helps ..
    dont worry newbie, we got you covered.

  3. #3
    cowgod is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Default

    Your code is all wrong.

    First, you don't need:

    Java Code:
    byte bArray[] = new byte[str.length()];
    		
    bArray = str.getBytes();
    You just need:

    Java Code:
    byte bArray[] = str.getBytes();
    The method getBytes() creates the array and returns it. You don't need to create it yourself. What you did was create an empty array and then throw it away and replace it with the array created by getBytes.

    Second, you aren't printing the array. You're printing the address in memory that the data is stored at. To print the array, you need to do the following:

    Java Code:
    for(int iByte = 0; iByte < bArray.length; iByte++)
      System.print((char)bArray[iByte]);
    
    System.println();
    (Note: I didn't actually test this code, but it should work.)

    It will print the actual characters instead of the address of the array. The system.print methods don't take arrays as arguments. They take Strings. Other objects are converted with the toString() method, but it turns out that with arrays this just outputs the address of the array.

    You also have to convert the byte back into a char to avoid printing out a number instead of a character.

    If you use byte arrays properly, they actually work. In that case, you'll know when you need them by the time you get to writing a real program. Roots' example is a good one.

Similar Threads

  1. Byte Array
    By sandor in forum New To Java
    Replies: 12
    Last Post: 01-15-2009, 04:31 AM
  2. Byte Values
    By javaplus in forum New To Java
    Replies: 1
    Last Post: 06-23-2008, 01:08 AM
  3. BufferedImage to Byte
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-22-2008, 09:17 PM
  4. int to byte
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •