I came across this program and its not behaving in expected way.
public class StringTest
{
public static void main(String[] args)
{
String s = "Hello world";
for(int i = 0 ; i < s.length() ; i++)
{
System.out.write(s.charAt(i));
}
}
}
If we think it should print Hello world but it prints nothing. What is going on? Any ideas? Thanks in advance.
You want:
System.out.print(s.charAt(i));As per the API of
write:As noted in a comment to your question, if you really wish to use
write()you need toflush().The reason why
write(int)doesn’t print anything is because it only flushes the stream on\nand whenautoFlushis true.