Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-28-2008, 09:30 AM
mew mew is offline
Member
 
Join Date: Nov 2007
Posts: 70
mew is on a distinguished road
using Byte arrays
I was exploring byte arrays. Review the code below:

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

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-28-2008, 11:44 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-30-2008, 04:54 AM
Member
 
Join Date: Jan 2008
Posts: 1
cowgod is on a distinguished road
Your code is all wrong.

First, you don't need:

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

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:

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Byte Array sandor New To Java 8 08-29-2008 01:28 PM
Byte Values javaplus New To Java 1 06-23-2008 01:08 AM
BufferedImage to Byte Java Tip Java Tips 0 01-22-2008 09:17 PM
int to byte ravian New To Java 1 01-13-2008 08:22 PM
Byte Streams JavaForums Java Blogs 0 12-26-2007 03:01 PM


All times are GMT +3. The time now is 10:20 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org