• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's the difference in this case? (so basic)  RSS feed

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there !

I'm in the middle of an internal battle here, because I'm in the middle of a dilemma.

When converting a byte array to String, why is it different to use the toString() method instead of doing a new String(byte[]); the latter works, but the first case doesn't.



What's the difference between these two implementations?, and Why does the one that -works- is able to convert effectively a byte array into a String? While the other (toString()) does not?


Thanks in advance,

Sincerely,

Jose


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The byte array, in fact, all arrays, do not override the toString() method. So, it uses the toString() method of the Object class.

Henry
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, as it stands, none of that should work, as the array of byte objects is never initilialized.

If you do this, it all works, doesn't it?


-Cameron McKenzie

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron,

Sorry If I didn't clarify this.... Like I said, It was to be assumed that the array of bytes was initialized to something.

Anyways, assuming you have this array that has some characters in it... Why is it that I can't get the String back when I use toString() on it? while new String() does ?

What's the difference conceptually?

Jose

 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you should get something!

When I run the following code, I get B@56845684 as the result printed to the console.


But what is @56845684? That's the memory address of the array instance. Remember, b1 isn't a byte, b1 is an ARRAY, which is an object in Java. You can invoke the toString method of an array, as you can any Object in Java. But, you're really not going to get anything meaningful back, because there isn't really anything about the array that the JVM might return.

Remember, an array is one thing. An array can hold other things. When you toString an array, you toString the array, not the things inside of it.

Tell me, can you get the funny memory pointer location to print out when you run it? I'm running on a Java 5 JDK I think.

-Cameron McKenzie
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron Wallace McKenzie wrote:But what is @56845684? That's the memory address of the array instance.

Actually no. It's the hexidecimal representation of the hash code. That may or may not be related to the memory address, depending on the JVM implementation.

Remember, b1 isn't a byte, b1 is an ARRAY, which is an object in Java. You can invoke the toString method of an array, as you can any Object in Java. But, you're really not going to get anything meaningful back, because there isn't really anything about the array that the JVM might return.

Technically, char[]'s toString() could return all chars appended, and byte[]'s toString() could return a HEX string of the bytes.

The thing with arrays is, their meaning depends on the context. Jose wants byte[]'s toString() to return the same as new String(b), but in my earlier example it could return a HEX string of the bytes. So which one is best? Both, neither, either one, there is no single answer.

That's why the toString() method of arrays returns something that looks like gibberish to most, but basically it's Object's toString() implementation: the class name (which is [B for byte[]), followed by @ and the hexidecimal representation of the hash code. If you need anything else, just call an existing method or constructor, or create something new.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Rob !

Thanks for the extended explanation. It's always good to be reminded about the nuances of the Language; I kinda had the basic idea, but just had forgotten about how both ways differ conceptually.
Apparently and according to a Senior Programmer for IBM, I made an unforgivable mistake by even assuming I could get the String of characters out of a byte array by using toString().
What's your opinion on this ? Do I deserve forgiveness given that I'm certified? I was just messing around with codes and incorrectly assumed these array objects (especially byte) had toString() implemented. But he flamed me... Couldn't even believe I did that...

I'm kinda depressed for that reason, What do you guys think ?

Best Regards,

Jose
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Type 25 Hello World's and all will be forgiven, my son

Seriously, everybody makes small mistakes now and then (if you think you don't, you're lying to yourself). The thing is to catch these mistakes early on, so you'll have time to fix the big mistakes when you need it.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Rob. The experience of being a coder is constantly making mistakes and trying to catch as many as possible. This is the only way to become any good.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, thanks for that...

I might just write those Hello-Worlds to redeem myself. But I needed to read those words, sometimes I just use the tools and pick the first method that returns a String that shows up in the help-box, and I know that it's probably not the best way to program, it's like being only half-aware of what one's doing; I'm not trying to indulge myself by saying what I'm about to say, But I lost focus given that sometimes I find it over-whelming to write Java for WebSphere Message Broker...

Thanks for forgiving me Master

I'll be back !

Jose
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose Campana wrote:I might just write those Hello-Worlds to redeem myself.

I'll be back !

Jose
You will only be redeemed if you use more informative thread titles ; then we shall be only too pleased to see you back
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!