When an array of "x" data type is initialized in java, what value does it default to? [closed]

-1

I'm starting in this of the programming, the truth is difficult for me to learn the same if they could give me advice, the truth I would like to work as a developer.

    
asked by Demon 18.09.2017 в 02:48
source

1 answer

2

The question seems interesting to me. I hope you can improve it, so that it adapts to the format of the site.

If you review the documentation you will see the default values of the primitive types:

Tipo                      Valor por defecto
byte                       0
short                      0
int                        0
long                       0L
float                      0.0f
double                     0.0d
char                       '\u0000'
String (u objeto)          null
boolean                    false

When you create arrays of any of those types and verify them without assigning them values, you will see that the arrays adopt the same values according to the type.

Let's see a code test:

VIEW DEMO

import java.util.*;
import java.lang.*;

class Rextester
{  
    public static void main(String args[])
    {

        System.out.println("Byte:");
        byte[] bytes = new byte[7];          
        for (Byte by : bytes)
            System.out.print(by + " ");

        System.out.println("\n\nShort:");
        short[] shorts = new short[7];          
        for (Short sh : shorts)
            System.out.print(sh + " ");

        System.out.println("\n\nInt:");
        int[] ints = new int[7];          
        for (Integer inte : ints)
            System.out.print(inte + " ");

        System.out.println("\n\nLong:");
        long[] longs = new long[7];          
        for (long lng : longs)
            System.out.print(lng + " ");

        System.out.println("\n\nFloat:");
        float[] floats = new float[7];          
        for (float flt : floats)
            System.out.print(flt + " ");

        System.out.println("\n\nDouble:");
        double[] doubles = new double[7];          
        for (Double dbl : doubles)
            System.out.print(dbl+ " ");

        System.out.println("\n\nChar:");
        char[] arrChars = new char[7];          
        for (char chs : arrChars)
            System.out.print(chs+ " ");


        System.out.println("\n\nString:");
        String str[] = new String[7];
        for (String s : str)
            System.out.print(s + " ");

        System.out.println("\n\nBoolean:");
        boolean bols[] = new boolean[7];
        for (boolean bol : bols)
            System.out.print(bol + " ");

    }
}

Result:

Byte:
0 0 0 0 0 0 0 

Short:
0 0 0 0 0 0 0 

Int:
0 0 0 0 0 0 0 

Long:
0 0 0 0 0 0 0 

Float:
0.0 0.0 0.0 0.0 0.0 0.0 0.0 

Double:
0.0 0.0 0.0 0.0 0.0 0.0 0.0 

Char:


String:
null null null null null null null 

Boolean:
false false false false false false false 
    
answered by 18.09.2017 в 03:31