If the new String() constructor inside the try block throws an exception, no value will be assigned to s4. You should either intialize s4 to "" before the try block or in the catch statement.

Lambok Sianturi wrote:

Hi all...

I have piece of code like this..

char charArray[] = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a','y'};
byte byteArray[] = { (byte) 'n', (byte) 'e', (byte) 'w',
(byte) ' ', (byte) 'y', (byte) 'e',
(byte) 'a', (byte) 'r' };
StringBuffer buffer;
String s, s1, s2, s3, s4, s5, s6, s7, output;

s = new String( "hello" );
buffer =
new StringBuffer( "Welcome to Java Programming" );
// use the String constructor
s1 = new String();
s2 = new String( s );
s3 = new String( charArray );
try {
s4 = new String( charArray, 6, 4 );
}
catch( StringIndexOutOfBoundsException ex ) {
System.out.println( "out of exception" );
}
s5 = new String( byteArray, 4, 4 );
s6 = new String( byteArray );
s7 = new String( buffer );


output = "s1 = " + s1 +
"\ns2 = " + s2 +
"\ns3 = " + s3 +
"\ns4 = " + s4 +
"\ns5 = " + s5 +
"\ns6 = " + s6 +
"\ns7 = " + s7;

When i try to compile this, an error "variable s4 might not have been
initialized" occurs.
Would someone like to explain me why?

Thankyou in advance.

::Lambok Sianturi::
PT. Multi Garmenjaya
Jl. Krawang No. 1
Bandung - Jawa Barat
Indonesia
Phone : 62 22 7272222
____________________________________________________


____________________________________________________
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