I am a beginner in java. I found this example and cant understand that how can we pass a "new" operator , to create an object inside the parameter of a constructor. Furthermore, its nested and there is another statement(as parameter) in the parenthesis.
For ex: inside the new 'Bufferedreader' we pass an new 'Inputstream' object , then again call a method inside the constructor: url.openStream(). I have seen this many times and i am confused as to why dont we create a new object reference to "InputStream i = null;' and then pass that to the constructor?
Also, in general, what does it mean to return a value from a method and assign it ta Class instance? Test t = x.getname();', where the value return should be a class type? I dont understand this, please help!
StringBuilder json = new StringBuilder();
try {
URL url = new URL(sMyUrl);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
try {
String str;
while ((str = in.readLine()) != null) {
json.append(str).append("\n");
}
} finally {
in.close();
}
} catch (Exception e) {
throw new RuntimeException("Failed to read JSON from stream", e);
}