Intersting Question
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
class j2
{
public static void main(String arg[])
{
j2 jo=new j2();
jo.me(null);
}
public void me(String s)
{
System.out.println(s);
}
}
why does the following question doesnt give the NullPointerException.....is null a String? i dont think so..then why is this working fine and printing the out put as null.
please help also lemme know under what circumstances can we get a null pointer exception...example pls...
{
public static void main(String arg[])
{
j2 jo=new j2();
jo.me(null);
}
public void me(String s)
{
System.out.println(s);
}
}
why does the following question doesnt give the NullPointerException.....is null a String? i dont think so..then why is this working fine and printing the out put as null.
please help also lemme know under what circumstances can we get a null pointer exception...example pls...
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
<html><body>
i)strings are objects.
Any class level object is automatically initialised to the null. So, when you call for them using println they always print null. Let us take an example.
<code>
public class int1{
Integer i;
public static void main(String args[]){
int1 it=new int1();
it.me();
}
public void me(){
System.out.println(i);// this will print null
}
}
</code></body></html>
i)strings are objects.
Any class level object is automatically initialised to the null. So, when you call for them using println they always print null. Let us take an example.
<code>
public class int1{
Integer i;
public static void main(String args[]){
int1 it=new int1();
it.me();
}
public void me(){
System.out.println(i);// this will print null
}
}
</code></body></html>
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Anand,
as Asim told you, this is an assignment to an Object with value null. So there is nothing special about...
You can do a without any problems....
BUT you will get a NullPointerException, if you invoke a method on that string, for example if you want to print out the length...
Hope that helps, cheers
Oliver
as Asim told you, this is an assignment to an Object with value null. So there is nothing special about...
You can do a without any problems....
BUT you will get a NullPointerException, if you invoke a method on that string, for example if you want to print out the length...
Hope that helps, cheers
Oliver
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
But what about this.
class j2
{ static char[] c;
public static void main(String arg[])
{
j2 jo=new j2();
jo.me(c);
}
public void me(char[] s)
{
System.out.println(s);
}
}
why does it give a nullpointerException
class j2
{ static char[] c;
public static void main(String arg[])
{
j2 jo=new j2();
jo.me(c);
}
public void me(char[] s)
{
System.out.println(s);
}
}
why does it give a nullpointerException
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi,
Here is the explanation.
The statement , static char[] c, initializes the char array to null.
In the main() method, this char[] c of nulls is passed as an argument to public void me(char[] s).
The statement, System.out.println(s), tries to print a char[] array of nulls and hence NullPointerException is thrown.
Java API on java.io.PrintStream:
public void println(char[] x)
Prints an array of characters and then terminates the line. This method behaves as though it invokes print(char[])and then println().
public void print(char[] s)
Prints an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
Parameters:
s - The array of chars to be printed
Throws:
NullPointerException - If s is null
Hope its clear.
FYI: You may try this interesting code.
class j3
{
public static void main(String arg[])
{
j3 jo=new j3();
jo.me(null);
System.out.println(jo);
j3 jo1=null;
System.out.println(jo1);
// System.out.println(null); - will give Compile time error
}
public void me(String s)
{
System.out.println(s);
}
}
The output of the above program is:
null
j3@xxxxxxx ==> hashcode value
null
Note:
The following code when executed will give compile-time error.
class NullTest1
{
public static void main(String arg[])
{
System.out.println(null);
}
}
- Suresh Selvaraj
Here is the explanation.
The statement , static char[] c, initializes the char array to null.
In the main() method, this char[] c of nulls is passed as an argument to public void me(char[] s).
The statement, System.out.println(s), tries to print a char[] array of nulls and hence NullPointerException is thrown.
Java API on java.io.PrintStream:
public void println(char[] x)
Prints an array of characters and then terminates the line. This method behaves as though it invokes print(char[])and then println().
public void print(char[] s)
Prints an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
Parameters:
s - The array of chars to be printed
Throws:
NullPointerException - If s is null
Hope its clear.
FYI: You may try this interesting code.
class j3
{
public static void main(String arg[])
{
j3 jo=new j3();
jo.me(null);
System.out.println(jo);
j3 jo1=null;
System.out.println(jo1);
// System.out.println(null); - will give Compile time error
}
public void me(String s)
{
System.out.println(s);
}
}
The output of the above program is:
null
j3@xxxxxxx ==> hashcode value
null
Note:
The following code when executed will give compile-time error.
class NullTest1
{
public static void main(String arg[])
{
System.out.println(null);
}
}
- Suresh Selvaraj
Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="http://www.decontconsulting.com" target="_blank" rel="nofollow">www.decontconsulting.com</a>
| You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |






