1) this code definately going to throw nullpointerexception
you prediction is absolutely right.
2) when ever you are going to declare any local or automatic variable create a separate scope there if it could be happened a number of times.
like
for(int i=0;i<10;i++)
String s = "govinda";
the above will definately going to give compilation error stating invalid declaration. do inspite of above as this,
for(int i=0;i<10;i++)
{String s = "govinda";}
putting curley braces will not cause thi error because by this every time program execution flow leave that curley braces String s goes out of scope and hence cease and in the next time
it again get created and after completing that block again ceases. this cycle will goes on up to iteration continues by your for loop.
Originally posted by Mamta Jha:
hi everybody,
My doubt is
1.String s=null;
System.out.println(s.length());
should throw nullpointer exception,but it shows 0.why?
2.for(int i=0; i<N; i++)>
String tmp = "test"; //why this shows compilation error as variable redeclaration
I'll be thankful,if anybody could answer these ques
Thanks,
Mamta