English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Mostrar el valor inicial por defecto de un tipo de datos en Java

Para mostrar el valor inicial predeterminado de un tipo de datos, simplemente declare una variable del mismo tipo de datos y muéstrala.

A continuación, se muestra un programa en Java que muestra los valores iniciales de los DataTypes.

Ejemplo

public class Demo {
   boolean t;
   byte b;
   short s;
   int i;
   long l;
   float f;
   double d;
   void Display() {
      System.out.println("boolean (Valor Inicial) = ") + t);
      System.out.println("byte (Valor Inicial) = ") + b);
      System.out.println("short (Valor Inicial) = ") + s);
      System.out.println("int (Valor Inicial) = ") + i);
      System.out.println("long (Valor Inicial) = ") + l);
      System.out.println("float (Valor Inicial) = ") + f);
      System.out.println("double (Valor Inicial) = ") + d);
   }
   public static void main(String[] args) {
      Demo d = new Demo();
      System.out.println("Mostrando valores iniciales...")}
      d.Display();
   }
}

Resultados de salida

Mostrando valores iniciales...
boolean (Valor Inicial) = false
byte (Valor Inicial) = 0
short (Valor Inicial) = 0
int (Valor Inicial) = 0
long (Valor Inicial) = 0
float (Valor Inicial) = 0.0
double (Valor Inicial) = 0.0

En el programa anterior, declaramos una variable con diferentes tipos de datos.

boolean t;
byte b;
short s;
int i;
long l;
float f;
double d;

Para obtener el valor inicial predeterminado, simplemente imprima la variable declarada anteriormente.

System.out.println("boolean (Valor Inicial) = ") + t);
System.out.println("byte (Valor Inicial) = ") + b);
System.out.println("short (Valor Inicial) = ") + s);
System.out.println("int (Valor Inicial) = ") + i);
System.out.println("long (Valor Inicial) = ") + l);
System.out.println("float (Valor Inicial) = ") + f);
System.out.println("double (Valor Inicial) = ") + d);

Se muestra el valor predeterminado.