Default Array Values in Java (original) (raw)
Last Updated : 24 Jan, 2022
If we don’t assign values to array elements and try to access them, the compiler does not produce an error as in the case of simple variables. Instead, it assigns values that aren’t garbage.
Below are the default assigned values.
S. No. | Datatype | Default Value |
---|---|---|
1 | boolean | false |
2 | int | 0 |
3 | double | 0.0 |
4 | String | null |
5 | User-Defined Type | null |
Example:
Java
class
ArrayDemo {
`` public
static
void
main(String[] args)
`` {
`` System.out.println(
"String array default values:"
);
`` String str[] =
new
String[
5
];
`` for
(String s : str)
`` System.out.print(s +
" "
);
`` System.out.println(
`` "\n\nInteger array default values:"
);
`` int
num[] =
new
int
[
5
];
`` for
(
int
val : num)
`` System.out.print(val +
" "
);
`` System.out.println(
`` "\n\nDouble array default values:"
);
`` double
dnum[] =
new
double
[
5
];
`` for
(
double
val : dnum)
`` System.out.print(val +
" "
);
`` System.out.println(
`` "\n\nBoolean array default values:"
);
`` boolean
bnum[] =
new
boolean
[
5
];
`` for
(
boolean
val : bnum)
`` System.out.print(val +
" "
);
`` System.out.println(
`` "\n\nReference Array default values:"
);
`` ArrayDemo ademo[] =
new
ArrayDemo[
5
];
`` for
(ArrayDemo val : ademo)
`` System.out.print(val +
" "
);
`` }
}
Output
String array default values: null null null null null
Integer array default values: 0 0 0 0 0
Double array default values: 0.0 0.0 0.0 0.0 0.0
Boolean array default values: false false false false false
Reference Array default values: null null null null null
Similar Reads
- Default Methods In Java 8 Before Java 8, interfaces could only have abstract methods. The implementation of these methods has to be provided in a separate class. So, if a new method is to be added in an interface, then its implementation code has to be provided in the class implementing the same interface. To overcome this i 3 min read
- Default constructor in Java Like C++, Java automatically creates default constructor if there is no default or parameterized constructor written by user, and (like C++) the default constructor automatically calls parent default constructor. But unlike C++, default constructor in Java initializes member data variable to default 1 min read
- Can We Override Default Method in Java? Default method in Java is a method in java which are defined inside the interface with the keyword default is known as the default method. It is a type of non-abstract method. This method is capable of adding backward capability so that the old interface can grasp the lambda expression capability. J 3 min read
- HashMap values() Method in Java The java.util.HashMap.values() method of HashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the HashMap. Syntax: Hash_Map.values() Parameters: The method does not accept any parameters. Return Value: The method i 2 min read
- Environment Variables in Java In Java, Environment variables are widely used by operating systems to deliver configuration data to applications. Environment variables are key/value pairs with both the key and the value being strings, similar to properties in the Java platform. What are Environment Variables in Java?In Java, Envi 5 min read
- EnumMap values() Method in Java The Java.util.EnumMap.values() method in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the EnumMap. Syntax: EnumMap.values() Parameters: The method does not take any argument. Return Values: The method returns the collection 2 min read
- Final Local Variables in Java In Java, a local variable is a variable, which is declared inside a method. Local variables are only accessible within the method in which they are declared, other methods in the class do not know anything about that variable. When we declare a local variable, we need to initialize it first before u 3 min read
- Java.Lang.Double Class in Java Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa. An object of the Double class can hold a single double value. Double class is a wrapper class for the pr 4 min read
- AbstractMap values() Method in Java with Examples The AbstractMap.values() method of AbstractMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the AbstractMap. Syntax: AbstractMap.values() Parameters: The method does not accept any parameters. Return Value: The met 2 min read
- BigDecimal valueOf() Method in Java java.math.BigDecimal.valueOf(long val) is an inbuilt method in Java that translates a long value into a BigDecimal value with a scale of zero. It allows us, the reuse of frequently used BigDecimal values and hence this "static factory method" is provided in preference to a (long) constructor. Packag 3 min read