Tim Nicholson wrote:

Hi,

I just was looking at the code given in a previous email to the list.

The code is :-

s4 = new String( charArray, 6, 4 );

Can someone please explain to me why there are 3 parameters (charArray, 6,
4 ) in this constructor instead of the normal 1 ? And what do the second and
third parameters (6 and 4) signify ?

Also; can someone explain how an array of characters can become a String, as
it has in this example ? I know that a String can be thought of as an array
of characters but I'm a bit confused as to just being able to make a
charArray, and String ?

From the Javadoc on JDK1.41. Note this constructor has been in the JDK since 1.1:


String

public *String*(byte[] bytes,
int offset,
int length)

Constructs a new String by decoding the specified subarray of bytes
using the platform's default charset. The length of the new String
is a function of the charset, and hence may not be equal to the
length of the subarray.

The behavior of this constructor when the given bytes are not valid
in the default charset is unspecified. The |CharsetDecoder|
<http://java.sun.com/j2se/1.4.1/docs/api/java/nio/charset/CharsetDecoder.html>
class should be used when more control over the decoding process is
required.

*Parameters:*
|bytes| - the bytes to be decoded into characters
|offset| - the index of the first byte to decode
|length| - the number of bytes to decode *Throws:*
|IndexOutOfBoundsException
<http://java.sun.com/j2se/1.4.1/docs/api/java/lang/IndexOutOfBoundsException.html>|
- if the |offset| and the |length| arguments index characters
outside the bounds of the |bytes| array |NullPointerException
<http://java.sun.com/j2se/1.4.1/docs/api/java/lang/NullPointerException.html>|
- if |bytes| array is |null|
*Since:*
JDK1.1



____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

Be respectful! Clean up your posts before replying
____________________________________________________

Reply via email to