File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes difference between the two Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between the two" Watch "difference between the two" New topic

difference between the two

Mahesh Bamane
Ranch Hand

Joined: Mar 12, 2008
Posts: 68
I would like to know the difference between new String(byte[],encoding) and new String().getBytes(encoding).
Please explain.

There is nothing like trying, either you do it or you don't.
SCJP 1.5
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

The first one produces a String as the result. The second one produces a byte array as the result.

There are more differences, but you only asked for one.
Mahesh Bamane
Ranch Hand

Joined: Mar 12, 2008
Posts: 68
Please, you can go ahead and tell them all ;)
marc weber

Joined: Aug 31, 2004
Posts: 11343

Java™ Platform, Standard Edition 6 - API Specification.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

The first takes a byte[] and creates a String with its contents. The second creates a new empty String, then retrieves its bytes in a specific encoding, then throws away this empty String. The byte[] should be empty.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: difference between the two
It's not a secret anymore!