Java written test face question--int The default value of the array

Source: Internet
Author: User
Tags float double object object

1. In the interview today encountered a problem, the code is as follows:

public class Test2
{public

	static void Main (string[] args)
	{
		int[] arr=new int[5];
		Assigning value to an array member
		arr[0]=2;
		arr[3]=5;
		for (int i = 0; i < arr.length; i++)
		{
			//output The value of each array member sequentially
			System.out.println ("Show:" +arr[i]);
		}
}
The output results are as follows:

Show:2
show:0
show:0
show:5
show:0
You will find that no assigned member, the output default value is: 0;

Reason: Because int is the basic data type of Java, its default value is 0;

Extended:

Default values for basic data types in 8:

①byte Short int Long these four basic data types array default value is 0

②float double the default value of the two arrays is 0.0

③char The default value of this type of array is a space

④boolean type array default value is False

2. When the defined array type is integer, what is the output result?

The code is as follows:

public class Test2
{public

	static void Main (string[] args)
	{
		//int[] arr=new int[5];
		Integer[] Arr=new integer[5];
		Assigning value to an array member
		arr[0]=2;
		arr[3]=5;
		for (int i = 0; i < arr.length; i++)
		{
			//output The value of each array member sequentially
			System.out.println ("Show:" +arr[i]);
		}
}
Output results:
Show:2
show:null
show:null
show:5
show:null
The output that is not assigned here is NULL, and this is the bit.

Reason: Because the integer class is not a basic data type, the default value is not 0, and is null;

Extensions: The default value for Java base data types is 0, the default value for reference data type is null, and reference data types include three kinds: class classes, interface interface, arrays;

Classes Class Reference
Can be we created, here I do not say much, mainly to explain a few Java libraries in the class

Object:object is a very important class, object is the root class of the class hierarchy, each class uses object as the superclass, and all objects (including the number

Group) implements the method of this class. Use object to define all classes

Such as:

Object object= New Integer (1); To define a Interger class

Integer i= (integer) object; To cast this object into the Interger class.

The String:string class represents a string, and all string literals in the Java program (such as "ABC") are implemented as instances of this class. Check the list of sequences

Characters, comparison strings, search strings, extract substrings, create a copy of a string, in that copy, all characters are converted to uppercase or lowercase.

Date:date represents a specific moment, accurate to milliseconds. Date classes are now generally replaced by calendar and GregorianCalendar.

The Void:void class is a placeholder class that is not instantiated and maintains a reference to a class object that represents the Java keyword Void.

There is also a corresponding class such as: Integer Long Boolean Byte Character Double Float Short These basic data types of encapsulation class;

The. 3.String type of array output.

The code is as follows:

public class Test2
{public

	static void Main (string[] args)
	{
		
		string[] arr=new string[5];
		Assigning values to an array member
		arr[0]= "haha";
		Arr[3]= "Java";
		for (int i = 0; i < arr.length; i++)
		{
			//output The value of each array member sequentially
			System.out.println ("Show:" +arr[i]);
		}
}

Output results:

Show:haha
show:null
show:null
show:java
show:null


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.