简体   繁体   中英

I am creating an object of bufferedReader but i don't know what is the meaning of the second new in this statement :

我不知道该声明中第二个新词的含义是什么,请有人帮我解释一下?

BufferedReader br = new BufferedReader(new InputStreamReader(System.in);

You are creating a BufferedReader . This particular class has a specific function, and that is providing a buffer on its input, so that you can retrieve it in the chunks you desire (lines, ...). However, it can't take a file, or anything else directly as input for a simple reason: Avoiding repetition.

Imagine you wanted a different kind of reader. If BufferedReader also handled the direct reading from files and such, you'd have to duplicate all that functionality in your new class. So, instead, you say that BufferedReader takes an object that, by contract of its interface, feeds it data in a specific format. That way BufferedReader doesn't need to care where it reads from, it just does its job.

A side effect of this is that you thus need to employ a different object, in this instance the instace of InputStreamReader that handles actually retrieving data from System.in .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM