Java default values

Default Value of Data Types in Java :

Data TypeDefault Value (for fields)
byte0
short0
int0
long0L
float0.0f
double0.0d
char‘u0000’
String (or any object)null
booleanfalse

Live Example : Default value of Data Type

Sample Program that will illustrate Default Value Stored in Each Primitive Data Type Variable

public class DefaultValue {
  static boolean bool;
  static byte by;
  static char ch;
  static double d;
  static float f;
  static int i;
  static long l;
  static short sh;
  static String str;
  public static void main(String[] args) {
    System.out.println("Bool :" + bool);
    System.out.println("Byte :" + by);
    System.out.println("Character:" + ch);
    System.out.println("Double :" + d);
    System.out.println("Float :" + f);
    System.out.println("Integer :" + i);
    System.out.println("Long :" + l);
    System.out.println("Short :" + sh);
    System.out.println("String :" + str);
  }
}

Output :

[468×60]

Bool     :false
Byte     :0
Character:
Double   :0.0
Float    :0.0
Integer  :0
Long     :0
Short    :0
String   :null
Boolean data type in java programming

Java boolean data type

boolean Data Type : Primitive Data Type in Java Programming

  1. Boolean is primitive data type in Java.
  2. Boolean data type is used for logical values.
  3. Boolean data type can have two possible values : true or false.
  4. Boolean is the type returned by all relational operators
  5. Boolean is the type required by the conditional expressions used in control statements such as if and for.
  6. “Boolean” is wrapper class for “boolean” primitive data type.

How to Declare & Display Boolean Variable ?

[468×60]

public static void main(String args[]) {
 boolean b1,b2,b3;
 b1 = true;  // Assigning Value
 b2 = false; // Assigning Value
 b3 = b2;    // Assigning Variable
 System.out.println(b1); // Printing Value
 System.out.println(b2); // Printing Value
 System.out.println(b3); // Printing Value
}

Output :

true
false
false

Boolean data type in java programming

Live Example :

// Demonstrate boolean values.
class BoolTest {
  public static void main(String args[]) {
    boolean b1;
    b1 = false;
    System.out.println("b1 is " + b1);
    b1 = true;
    System.out.println("b1 is " + b1);
  // a boolean value can control the if statement
    if(b1) System.out.println("This is executed.");
    b = false;
    if(b)
      System.out.println("Not executed.");
 // outcome of relational operator is a boolean
    System.out.println("100 > 90 is " + (100 > 90));
  }
}

Output :

b1 is false
b1 is true
This is executed.
100 > 90 is true

[468×60]

Different Ways of Using Boolean Value :

Way 1 : Inside If Statement

public static void main(String args[])
{
 boolean b;
 b = true;
 if(b)
   {
   System.out.println("I am True");
   }
}
  • Boolean Value is used to check whether condition is true or not.
  • No need to do == to check equality
public static void main(String args[])
{
 boolean b;
 b = true;
 if(b == true)
   {
   System.out.println("I am True");
   }
}

Way 2 : Comparing Two Numbers

class Demo {
public static void main(String args[]) {
 boolean b;
 b = (10 > 6);
 if(b)
   {
   System.out.println("10 > 6");
   }
 }
}

we can use boolean value to hold the result Comparison operators. here 10 > 6 therefor true will be stored in boolean variable

Character Data Type in Java

Java character data type

Character data Type : Java Primitive Data Types

  1. In Java, the data type used to store characters is char.
  2. Character is 16 bits wide in Java.
  3. Java uses Unicode to represent characters.
  4. Java support lot of Unicode symbols from many more human languages for this purpose, it requires 16 bits.
  5. The range of a char is 0 to 65,536.
  6. There are no negative chars.

What is Unicode ?

Unicode defines a fully international character set that can represent all of the characters found in all human languages.

Live Example 1 : Integer Value Assigned to Character Data Type

class CharDemo {
  public static void main(String args[]) {
    char ch;
    ch = 'M';
    System.out.println("Character is : " + ch);
  }
}

Output :

Character is : M

Live Example 2 : Integer Value Assigned to Character Data Type

// Demonstrate char data type.
class CharDemo {
  public static void main(String args[]) {
    char ch1, ch2;
    ch1 = 88; // code for X
    ch2 = 'Y';
    System.out.print("ch1 and ch2: ");
    System.out.println(ch1 + "" + ch2);
  }
}

Output :

ch1 and ch2: X Y

Live Example 3 : Incrementing Character Variable

class CharDemo2 {
  public static void main(String args[]) {
    char ch1;
    ch1 = 'P';
    System.out.println("ch1 contains " + ch1);
    ch1++; // increment ch1
    System.out.println("ch1 is now " + ch1);
  }
}

Output :

ch1 contains P
ch1 is now Q

Other Data Types in Java : Integer | Float | Character | Boolean

Java float / double data type

Float Data Type :

  1. Floating Data Type is used to store float value.
  2. Floating Data Type is Primitive Data Type in Java Programming Language.
  3. Floating Data Type have respective Wrapper Class - “Float or Double“.

float Data Type Can store 2 types of Values these are listed below -

  1. Float
  2. Double

[468×60]

floatVariables of this type can have values from -3.4E38 (-3.4 * 1038) to +3.4E38 (+3.4 * 1038) and occupy 4 bytes in memory. Values are represented with approximately 7 decimal digits accuracy
doubleVariables of this type can have values from -1.7E308 (-1.7 * 10308) to +1.7E308 (+1.7 * 10308) and occupy 8 bytes in memory. Values are represented with approximately 17 decimal digits accuracy. The smallest non-zero value that you can have is roughly (4.9 * 10–324).

Live Example : Declaring Integer Variable in Java Programming

Class FloatDemo
{
public static void main(String args[])
  {
  float fval = 10.0f;
  System.out.println("Total Number : " + fval);
  }
}

Float Type : Some Notes

  1. In Java any value declared with decimal point is by default of type double.
  2. Suppose we have to assign float value then we must use ‘f’ or ‘F’ literal to specify that current value is “Float”.
  3. Specify “E” or “e” for values which contain exponent.

Way 1 : How to Declare Double Variable

Class FloatDemo
{
public static void main(String args[])
  {
  double d1 = 10;
  System.out.println("Total Number : " + fval);
  }
}
  • We have assigned integer value to the Double . (i.e we are assigning lower value of inside bigger variable , no need to typecast )
  • double keyword is used to declare double variable.

Way 2 : How to Declare float Variable

float fval = 10.4F;

Way 3 : Using Exponent In Double Value

float electronMass = 9E-28F;

another example

double lightSpeed = 3.8E8;

Way 4 : Declaring Fix Value

final int meter_in_cm = 100;
Integer Data TYpe in Java Programming Language

Java integer data type

Integer Data Type :

  1. Integer Data Type is used to store integer value.
  2. Integer Data Type is Primitive Data Type in Java Programming Language.
  3. Integer Data Type have respective Wrapper Class - “Integer“.
  4. Integer Data Type is able to store both unsigned ans signed integer values so Java opted signed,unsigned concept of C/C++.

Integer Data Type Can have 4 types of Values these are listed below table -

Name Width Range
long 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
int 32 –2,147,483,648 to 2,147,483,647
short 16 –32,768 to 32,767
byte 8 –128 to 127

Live Example : Declaring Integer Variable in Java Programming

Class IntDemo
{
public static void main(String args[])
  {
  int number=0;
  System.out.println("Total Number : " + number);
  }
}

Explanation :

  1. Primitive Variable can be declared using “int” keyword.
  2. Though Integer contain default Initial Value as 0 , still we have assign 0 to show assignment in Java.
  3. “+” operator is used to concatenate 2 strings.
  4. Integer is converted into String internally and then two strings are concatenated.

Integer Literal in Java :

  1. Value Stored in the Integer Variable is called as Literal.
  2. In the above example 0 is integer literal.
Type of Value to be storedConvention
IntegerAny Valid Integer Value
Long10L (L at the end)
Hexadecimal Number0×25
Octal Number025 (Preceding 0)
Data Types in Java Programming Language

Java primitive data types

Primitive Data Types in Java Programming Language :

Data type is nothing but the type of the data. Each Variable in Java must have certain type associated with it which tells us what kind of data a variable can store.

Data Types in Java Programming Language are classified into two main groups - Primitive and Reference Data Types.

A primitive Data Types are :

  1. Data types are predefined by the Java language.
  2. Predefined data types are Reserved keyword so we cannot use them as variable name inside program/application
  3. Primitive values do not share state with other primitive values.
  4. Total Number of Primitive Data Types in Java Programming is 8
  5. All Primitive Data Types have respective Wrapper Classes i.e Integer is wrapper class for primitive type int

Primitive Data Types are -

Primitive Data Types in Java :
booleancharbyteint
shortlongfloatdouble

Classification of Data Types in Java Programming Language

Data Types in Java Programming Language

Data Type and Other Details

TypeContainsDefaultSize
booleantrue or falsefalse1 bit
charUnicode Characteru000016 bits
byteSigned Integer08 bits
shortSigned Integer016 bits
intSigned Integer032 bits
longSigned Integer064 bits
floatFloating Number0.032 bit
doubleFloating Number0.064 bit

*to view range of individual data type click here.