Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

char and int array difference

Tags:

java

When I try to print the uninitialized static char array it gives run time error (Null pointer exception) whereas the uninitialized static int array gives null value. Why?

public class abc {
    static int arr[];
    static char ch[];

    public static void main(String[] args) {
        System.out.println(ch); //it gives null pointer exception at run time
        System.out.println(arr); //it gives output as "null".
        }
    }
like image 991
Shashank Agarwal Avatar asked Jul 18 '14 20:07

Shashank Agarwal


People also ask

What is the difference between int array and char array?

A character array contains characters; an integer array contains integers. And assuming you know the difference between a character and an integer, that's all the explanation you need… that's about it.

What is difference between int and character?

char: The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers. int: As the name suggests, an int variable is used to store an integer. float: It is used to store decimal numbers (numbers with floating point value) with single precision.

What is the difference between character and array?

Character Arrays String refers to a sequence of characters represented as a single data type. Character Array is a sequential collection of data type char. Strings are immutable.

Is char array the same as char array?

char *array = "One good thing about music"; declares a pointer array and make it point to a constant array of 31 characters. char array[] = "One, good, thing, about, music"; declares an array of characters, containing 31 characters.


2 Answers

System.out is an instance of PrintStream and this class has a few overloaded println methods. In your case:

  1. System.out.println(ch); is using public void println(char x[])
  2. System.out.println(arr); is using public void println(Object x) (there is no public void println(int[] x) method so the closest available type for int[] which println can use is Object).

The second method is using

String.valueOf(x);

to get a string representation of the object we want to print and the code of the valueOf method looks like

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

so it is null-safe (will return the string "null" if the reference holds null).

The first method is at some level using

public void write(char cbuf[]) throws IOException {
    write(cbuf, 0, cbuf.length);
                 //    ^^^^^^^ this throws NPE
}

and because cbuf is null cbuf.length will throw NullPointerException because null doesn't have length (or any other) field.

like image 149
Pshemo Avatar answered Oct 01 '22 08:10

Pshemo


The answer exists in the PrintWriter source code (of which System.out is an instance).

Start with the fact that the uninitialized arrays, as reference variables, are given the default of null.

The println(char[]) (eventually) attempts to call .length on the passed in array. It's null, resulting in the NullPointerException. println(char[]) (eventually) calls write(char[]):

public void write(char buf[]) {
    write(buf, 0, buf.length);
}

There is no overload of println matching int[], but there is a println(Object). There it (eventually) attempts String.valueOf, passing the null reference, so String.valueOf takes the null and returns the String "null". println(Object) calls print(Object):

public void print(Object obj) {
    write(String.valueOf(obj));
}
like image 15
rgettman Avatar answered Oct 01 '22 08:10

rgettman