- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the default initialization values of elements in an array using Java?
In Java arrays are the reference types which stores multiple elements of the same datatype. You can create an array just like an object using the new keyword −
type[] reference = new type[10];
or, directly using the flower brackets ({}).
int [] myArray = {10, 20, 30, 40, 50}
When you create instance variables in Java you need to initialize them, else the compiler will initialize on your behalf with default values.
Similarly, if you create an array as instance variable, you need to initialize it else the compiler initializes with default values which are −
- Integer: 0
- Byte: 0
- Float:0.0
- Boolean: false
- String/Object: null
Example
In the following Java program prints the default values of the arrays of type integer, float, byte, boolean and, String.
import java.util.Arrays; import java.util.Scanner; public class ArrayDefaultValues { int intArray[] = new int[3]; float floatArray[] = new float[3]; byte byteArray[] = new byte[3]; boolean boolArray[] = new boolean[3]; String stringArray[] = new String[3]; public static void main(String args[]){ ArrayDefaultValues obj = new ArrayDefaultValues(); System.out.println(Arrays.toString(obj.intArray)); System.out.println(Arrays.toString(obj.floatArray)); System.out.println(Arrays.toString(obj.byteArray)); System.out.println(Arrays.toString(obj.boolArray)); System.out.println(Arrays.toString(obj.stringArray)); } }
Output
[0, 0, 0] [0.0, 0.0, 0.0] [0, 0, 0] [false, false, false] [null, null, null]
- Related Articles
- What are the default array values in Java?
- Default array values in Java
- Initialization of a normal array with one default value in C++
- What are the default values of instance variables whether primitive or reference type in Java?
- How to reverse the elements of an array using stack in java?
- Mapping an array to a new array with default values in JavaScript
- What are all the possible C# array initialization syntaxes?\n
- What are Default Constructors in Java?
- How to return an array whose elements are the enumerable property values of an object in JavaScript?
- Find max and min values in an array of primitives using Java
- Can you assign an Array of 100 elements to an array of 10 elements in Java?
- Display default initial values of DataTypes in Java
- What is the difference between initialization and assignment of values in C#?
- What are Default Methods in Java 8?
- How to create an array with non-default repeated values in C#?

Advertisements