Primitive Data Types in Java

javaData types define and restrict the type of values that can be stored in the variables. Java has the following 8 primitive data types:

  1. Int – The Int or the Integer data type is a 32-bit signed two’s complement integer. Int data type can only hold whole numbers ranging from -2,147,483,648 to 2,147,483,647.
    Default value for an int variable is 0
    We can declare and initialize Int in java as following:

     int number;
    number = 2395;
    
  2. Double – Double is used to hold 64-bit signed floating point values. This is the largest primitive data type available in Java that can hold decimal values.
    Default value for a double variable is 0.0d
    We can declare and initialize double in java as following:

     double bigNumber;
    bigNumber = 2395.23; 
  3. Float – float is a single-precision 32-bit IEEE 754 floating point.
    Default value is 0.0f
    We can declare and initialize float in java as following:

     float fl;
    fl = 2395.2; 
  4. Long – long is an 64-bit signed two’s complement integer. It can hold numbers ranging from -2^63 to +2^63-1
    Default value for a long variable is 0L
    We can declare and initialize long in java as following:

     float br;
    br = 6758798; 
  5. Short – short is an 16-bit signed two’s complement integer. It can hold numbers ranging from -32,768 to +32,767
    Default value for a short variable is 0
    We can declare and initialize short in java as following:

     short k;
    k = 7632; 
  6. Char – char data type is used to store any single character. Size of char is 16 bit. Minimum value is 0 and maximum is 65,535
    We can declare and initialize char in java as following:

     char alphabet;
    alphabet = 'A'; 
  7. Byte – Byte is an 8-bit signed two’s complement integer. It can hold numbers ranging from -128 to +127
    Default value for a byte variable is 0
    We can declare and initialize byte in java as following:

     byte betty;
    betty = 44; 
  8. Boolean – This data type can have two possible values: true and false. Default value of boolean type is false and the size is 1 bit.
    We can declare and initialize boolean in java as following:

     boolean hasCar;
    hasCar = true; 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: