A seemingly simple, but full of routine Java basic interview questions

I. Introduction

I was reviewing javase knowledge recently, and found a seemingly simple but pitting interview question, organized into a technical blog to share with everyone, this blog is based on the Javase video summary of Shang Silicon Valley

2. Topic

Find the output of the following code:

public class Test{
    
    
	public static void main(String[] args){
    
    
		int[] arr = new int[]{
    
    1,2,3};
		System.out.println(arr);

		char[] arr1 = new char[]{
    
    'a','b','c'};
		System.out.println(arr1);
	}
}

Three, answer analysis

The result is:
Insert picture description here

We all understand that the storage result of a variable of a reference data type is either an address value or a null .
What we get by outputting arr is indeed an address value. So why does the output of arr1 traverse the array?

Let's dive into the original code and analyze the reasons:

Insert picture description here

We can find by analyzing the original code that the println method is actually an overloaded method . The variable name that we output int type array actually calls the above figure:

println(Object):void

However, we want to output the name of the char type array variable, in fact, the method called is the above figure:

println(char[]):void

These are two overloaded methods. When we output a char type array, the array will be traversed.
This is why it is also a reference data type variable, the first output statement outputs the address value, and the second output result is the traversal of the array

Guess you like

Origin blog.csdn.net/weixin_46594796/article/details/109689014