The Scanner close java class () method lead story

The Scanner close java class () method - a method of magic

  In learning to learn Java Scanner class is more commonly used classes. As everyone knows, which hides a big secret. If not careful to use its use Close (), method, will be a big mistake. Such as:. Classes in java.util NoSuchElementException . When this is input to the general procedure,

Scanner sc = new Scanner (System.in) ;
  declare a Scanner object. new Scanner (xxxx) -> Object brackets when IuputStream incoming class. The following is a class constructor used Scanner

 

 

  This shows that when system.in get IuputStream object class. This is true, in the System class, in a InputStream type, and is the Final , that is constant, after being initialized can not be changed.

 

Scanner sc = new Scanner(System.in);

 

  Thoroughly understand the principles of object creation Scanner bar code on it

1   Scanner sc = new Scanner(System.in);
2   String next = sc.next();
3   //sc.close();
4   Scanner aa = new Scanner(System.in);
5   String next1 = aa.next();

 

  Run the code above to enter characters, carriage return, eh! It seems to be no problem. But you probably forgot one important thing, IuputStream is a stream, need to be closed when not in use, if not close the stream, it is very resource-consuming of.

  May learn when not a big deal, when you need to really develop a program, in order to better optimize the program , which is very necessary. Thus the opening of the third line of code, it is closed. Happy again enter, enter, surprises happen.

 

 

  Solution : finally closed in use Scanner stream, so that neither error nor a waste of resources. Of course, preferably the open and close the flow stream in the same method, the so increases the readability of the code, but also easy to increase the error code.

  And do not target multiple new Scanner, virtual machine heap of garbage will be reduced . In case you want to use other methods in objects Scanner The scanner is preferably passed into the parameter when an object.

  Code Scene: generally not possible to use multiple Scanner attached to objects, but it is possible in the same method call and then multiple methods and these methods may be used Scanner objects.

  

 

  The next main event debut.

  The reason being given : When creating Scanner objects above parameters are in brackets in System.in and then a final type, it can not be changed once initialized. So, although the number of Scanner class object is created behind you, in fact, they are the same return InputStream stream. In other words, each time a new object is assigned the address is the same .

Once the stream is closed, the back flow is used by the object closed state. It will have a surprise! If not pay attention you may write a lot of code after the error, so it is difficult to find the real point of error. Of course, you can also decompile view the call situation .

  close () should be used with caution!

Guess you like

Origin www.cnblogs.com/caoshuiping/p/11286265.html