Intialisation of default vals

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
saregg
Posts: 20
Joined: Fri May 01, 2020 11:27 am
Contact:

Intialisation of default vals

Post by saregg »

Code: Select all

public class Test 
	{ 
		public static void main(String[] args) 
			{
				 Boolean [] arr = new Boolean[ 2 ]; 
				 List<Boolean> list = new ArrayList<>(); 
				 list.add(arr[ 0 ]); 
				 list.add(arr[ 1 ]);
				 }
		}
The above code adds null to list as default value for Boolean is null and as its a reference and so the def vals are assigned, but the local variables shouldnot be initialised with default vals ? how it happens here ?

admin
Site Admin
Posts: 10403
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Intialisation of default vals

Post by admin »

This code is not initializing any local variable with default values. The variable arr is being explicitly assigned an array object.

Whenever you create an array object, its elements are always initialized with default values. Just like fields of an instance of any class.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 16 guests