-3

But i dont understand why its a? why read() method is executing twice?

 import java.io.*;
  public class asrts{
    public static void main(String argv[])throws Exception{
      StringReader sr=new StringReader("Kavp");
      sr.read();
      char c=(char)sr.read();
      System.out.println(c);
    }
  }

Output is "a"

please help

user2985842
  • 437
  • 9
  • 24

2 Answers2

2
  sr.read();                  <--- call #1, returns "K" and loses it
  char c=(char)sr.read();
               ^^^^^^^^^--- call #2, returns "a"
Marc B
  • 356,200
  • 43
  • 426
  • 500
1

why read() method is executing twice?

Because you called it twice.

user207421
  • 305,947
  • 44
  • 307
  • 483