Powered by Blogger.

Default value of primitive data types in Java

>> Saturday, August 8, 2015

In Real life to store any type of element/material, it should have suitable containers. Lets take water bottle can hold little water, like wise egg tray can hold eggs e.t.c. So to hold any object, there should have their suitable containers. In the same way, any form of the data can be stored by using the primitive data types based on their properties or attributes of the objects.
Default values of primitive data types_javabynataraj
A Real time example on Space Consumption: Let's Assume we have 5,2,1 liter bottles. If you want to pour one liter of water, in which bottle do we pour it? Probably in 1 liter bottle. Because it can hold correctly. So you don't want to use 5,2 liters bottles. This will waste your space.

In the same way java designers by choosing this kind of representation as i believe this reduces the memory consumption and also reduces the creation of newer objects which is crucial to maintain the application performance.
there are eight primitive data types in java:
  boolean
  byte
  short
  char
  int
  float
  long
  double
Here is a table with the sizes and default values.
Type Size in Bytes Default Value
byte 1 byte 0
short 2 bytes 0
char 2 byte '\u0000'
int 4 bytes 0
float 4 bytes 0.0f
long 8 bytes 0
double 8 bytes 0.0d
boolean not precisely defined* false
Every data type has some memory size defined. This enables that when ever a variable is declared, the memory size defined will be blocked irrespective of the value that is going to get assigned.

Every primitive data type has default values defined. When the programmer does not declare to assign any values to the variables, these default values will be assigned by the Virtual machine during the object instantiation.

Limitations of datatypes:
https://www.ima.umn.edu/~arnold/disasters/ariane.html

Program to find the default values of primitive data types:
public class DefaultValues {
// author: javabynataraj.blogspot.com
  boolean bo;
  byte b;
  char c;
  short s;
  int i;
  long l;
  float f;
  double d;
  Object o;
 public static void main(String[] args) {
    DefaultValues app = new DefaultValues();
    app.values();
  }
 public void values() {
    System.out.println("boolean: " + bo);
    System.out.println("byte: " + b);
    System.out.println("char: " + c);
    System.out.println("short: " + s);
    System.out.println("int: " + i);
    System.out.println("long: " + l);
    System.out.println("float: " + f);
    System.out.println("double: " + d);
    System.out.println("Object: " + o);
  }
}

Output:
boolean: false
byte: 0
char:
short: 0
int: 0
long: 0
float: 0.0
double: 0.0
Object: null


Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.