Null question help me .....

19 views
Skip to first unread message

Abdul Kader

unread,
Jan 9, 2013, 2:43:51 AM1/9/13
to java...@googlegroups.com
InputStream inputStream = null;


why we are using null in these code.......

Shirish Padalkar

unread,
Jan 9, 2013, 3:02:48 AM1/9/13
to java...@googlegroups.com
Well, it would be good if you can give a context around the code.

In general, null means unknown value or un-initialized value.

With regards,
Shirish Padalkar
http://twitter.com/_Garbage_



On Wed, Jan 9, 2013 at 1:13 PM, Abdul Kader <kadera...@gmail.com> wrote:
InputStream inputStream = null;


why we are using null in these code.......

--
You received this message because you are subscribed to the Google Groups "Java User Group For Pune, India" group.
To view this discussion on the web visit https://groups.google.com/d/msg/javapune/-/rRed4aUIvIAJ.
To post to this group, send email to java...@googlegroups.com.
To unsubscribe from this group, send email to javapune+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/javapune?hl=en.

Toshish Jawale

unread,
Jan 9, 2013, 3:22:46 AM1/9/13
to java...@googlegroups.com
Ideally you should avoid assigning null's to the variables specifically unless there is no other way round. Depends on your code, please share some more to get better idea of your question.
--
Thanks & Regards,
Toshish A. Jawale.
+91-8149115229

AMIT KALE

unread,
Jan 9, 2013, 4:57:24 AM1/9/13
to java...@googlegroups.com
If you wish to use InputStream based on some condition in a particular method and you also need to access this InputStream out of the condition block, then this is the place where you may use such codes.
Just to satisfy the compiler by initializing local variable.
This way your code will be more readable to other Developers.


This is just a guess about your context.
If you can provide more info, it will be helpful and may be learning experience for me as well.

   AMIT KALE

Sandeep Srivastav

unread,
Jan 10, 2013, 1:13:55 AM1/10/13
to java...@googlegroups.com
In java, only class member variables are initialized by null by default.
It means, even if you define any variable without initializing it, it gets initialized by null if it is member variable.
If the variable is defined in method then it is not initialized by java by default because it is local variable.
And if you try to access this variable without assigning any value, java gives compile time error because you should assign some value to the variable before using it.
That's the reason that you need to assign null value to the variable so that it will be initialized but not pointing to any valid memory.
With this, your both criteria gets satisfy and you can proceed.

Thanks and Regards,

Sandeep Srivastav
eQ-Technologic(India) Pvt. Ltd.
Pune.
Mob : +91-9371238688
Reply all
Reply to author
Forward
0 new messages