- 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 array values in 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
- Default array values in Java
- What are the default initialization values of elements in an array using Java?
- What are Default Constructors in Java?
- What are the default values of instance variables whether primitive or reference type in Java?
- What are Default Methods in Java 8?
- Display default initial values of DataTypes in Java
- Do local variables in Java have default values?
- What are the default values used by DB2 for various data types?
- Mapping an array to a new array with default values in JavaScript
- What are the differences between protected and default access specifiers in Java?
- What are the differences between default constructor and parameterized constructor in Java?
- Replace null values with default value in Java Map
- Why are default values shared between objects in Python?
- What are private, public, default and protected access Java modifiers?
- Does Java support default parameter values for a method?

Advertisements