what are the benefits of BufferedReader over Scanner

前端 未结 3 484
清酒与你
清酒与你 2021-01-14 21:20

here\'s a code about depth first search in graphs. who knows why bufferedReader class were used in this code? and why nextInt function not used instead? what is its privileg

3条回答
  •  醉酒成梦
    2021-01-14 21:45

    BufferedReader is simpler (which makes it slightly more efficient) but it is also a clearer choice showing you intend to do is to use the functionality BufferdReader provides. i.e readLine() is the main one.

    In short, if you have BufferedReader you know it is just going to read lines. If you use Scanner it implies you may or many not be reading something more complicated.

    BTW:

      Integer.parseInt(br.readLine())
    

    and

      scanner.nextInt();
    

    are not the same although the distinction is usually lost on noob developers. For this reason I prefer the first example. The difference is how new lines are handled.

提交回复
热议问题