Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default value for character array

Tags:

default value for character array is giving a null pointer exception...can any tell why is this exception just for character array ...even though the default value for other dataype arrays is null. eg

class  Test{       char[] x;     public static void main(String[] args) {         Test t=new Test();         System.out.println(t.x);         } } 

throwing null pointer exception

class  Test{     int[] x;     public static void main(String[] args) {         Test t=new Test();         System.out.println(t.x);     } } 

output:null

like image 283
Naved Madabhavi Avatar asked Aug 12 '15 05:08

Naved Madabhavi


People also ask

What is default value of character array in C?

For char arrays, the default value is '\0' .

What is the default value of a character data type elements of an array in Java?

Well assuming you have not added anything into the array by assigning the first position (0) a value, it will be null.


2 Answers

Printing a char[] behaves differently than other arrays, since PrintStream (which is the type of the System.out instance) has a specific method for printing char arrays - public void println(char x[]) - while for other arrays the general method for Objects is used - public void println(Object x).

println(Object x) prints the String "null" when passing to it a null reference.

/**  * Prints an Object and then terminate the line.  This method calls  * at first String.valueOf(x) to get the printed object's string value,  * then behaves as  * though it invokes <code>{@link #print(String)}</code> and then  * <code>{@link #println()}</code>.  *  * @param x  The <code>Object</code> to be printed.  */ public void println(Object x) {     String s = String.valueOf(x);     synchronized (this) {         print(s);         newLine();     } }  /**  * Returns the string representation of the <code>Object</code> argument.  *  * @param   obj   an <code>Object</code>.  * @return  if the argument is <code>null</code>, then a string equal to  *          <code>"null"</code>; otherwise, the value of  *          <code>obj.toString()</code> is returned.  * @see     java.lang.Object#toString()  */ public static String valueOf(Object obj) {     return (obj == null) ? "null" : obj.toString(); } 

println(char x[]) throws a NullPointerException when passing to it a null reference.

public void println(char x[]) calls public void print(char s[]) which calls public void write(char buf[]), which throws the NullPointerException when buf.length is evaluated:

/*  * The following private methods on the text- and character-output streams  * always flush the stream buffers, so that writes to the underlying byte  * stream occur as promptly as with the original PrintStream.  */  private void write(char buf[]) {   try {     synchronized (this) {       ensureOpen();       textOut.write(buf);       textOut.flushBuffer();       charOut.flushBuffer();       if (autoFlush) {         for (int i = 0; i < buf.length; i++)         if (buf[i] == '\n')             out.flush();       }     }   }   catch (InterruptedIOException x) {     Thread.currentThread().interrupt();   }   catch (IOException x) {     trouble = true;   } } 

BTW, the Javadoc of print(char s[]), which is the first method called by public void println(char x[]), mentions the NullPointerException exception :

void java.io.PrintStream.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

like image 92
Eran Avatar answered Mar 21 '23 18:03

Eran


First one is using println(char[] x) and the second one is using println(Object obj) which is internally using String.valueOf(x) and print null instead of NullPointerException

Check String.valueOf()

 public static String valueOf(Object obj) {         return (obj == null) ? "null" : obj.toString();  } 

and according to java doc println(char[] x) will throw NullPointerException if char[] arr is null.

like image 24
akash Avatar answered Mar 21 '23 16:03

akash