This week's book giveaway is in the Agile forum.
We're giving away four copies of Fixing your Scrum and have Ryan Ripley & Todd Miller on-line!
See this thread for details.
Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

java i/o stream close() how it works?

 
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there

I have a question that

for every stream that we create for java i/o we write at the end in finally block



it works

my question is as per syntax in if (is!=null) this means that if is means stream contain some value then close it(!=null)

then how it in my opnion it should be like (in logical way)


if stream contain nothing then close it

if(is==null)
{
is.close();
}

I am confuse about working this line

can any one explain how it works

thanks
 
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your example, you have assigned the input stream to the variable "is". If the variable "is" is NULL, that means it does not point to an input stream object. Therefor if you try to call a method on a variable that does not point to an object, you will get a NullPointerException.

This is the reason to test if the variable is NULL before trying to call a method on it. An exception may have been thrown while the input stream was being created so the input stream object was never assigned to the "is" variable.



Hope this helps.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Scott

Great Answars clearly understand
 
Marshal
Posts: 73732
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I told you in one of your other threads, you are better off learning about try with resources. Then you don't need to write all that boilerplate code.
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic