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. Parameter bytes represents the bytes to
be decoded into characters, offset represents the index of the first
byte to decode and length represents the number of bytes to decode.
class StringConstructor4{ public static void main(String args[]){ byte data[] = new byte[26]; String s1; int count = 0; for(byte b = 97; b < 123; b++){ data[count] = b; count++; } s1 = new String(data, 5, 10); System.out.println("s1 = " +s1); } }
Output
s1 = fghijklmno
1. Throws
IndexOutOfBoundsException If the offset and the length arguments
index characters outside the bounds of the bytes array.
class StringConstructor4IndexOut{ public static void main(String args[]){ byte data[] = new byte[26]; String s1; int count = 0; for(byte b = 97; b < 123; b++){ data[count] = b; count++; } s1 = new String(data, 18, 9); System.out.println("s1 = " +s1); } }
Output
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind ex out of range: 27 at java.lang.String.checkBounds(String.java:373) at java.lang.String.<init>(String.java:533) at StringConstructor4IndexOut.main(StringConstructor4IndexOut.java:12)
No comments:
Post a Comment