Java Data Types - The Coding Shala

Home >> Learn Java >> Java Data Types

Data Types

Java is a strongly typed language that means every variable has a type, every expression has a type and every type is strictly defined. The Java compiler checks all the expressions and parameters to ensure that the types are compatible and throws errors if found any mismatches.



There are two data types available in Java-

  1. Primitive data types
  2. Object data(user-created types)
Java Data Types - The Coding Shala

Primitive Data types in Java

Java has eight primitive types of data - 
  1. byte
  2. short
  3. int
  4. long
  5. char
  6. float
  7. double
  8. boolean
In Java byte, short, int and long are four integer types. Java does not support unsigned integers so all these integers are signed, positive and negative values. Float and double are floating-point numbers also known as real numbers. Data type char is used to store characters in Java. In Java for logical values, we use boolean data types.



Java byte data type

In Java, the smallest integer type is a byte. The range of the byte variable is from -128 to 127 and is a signed 8-bit type. To declare Byte variables we use byte keyword. for example-
byte a, b, c;
here variables a, b and c are byte types. 

Java short data type

Data type short is a 16-bit wide and has a range from -32768 to 32767. To declare short type variables we use a short keyword. for example-
short a;
short b;

Java int data type

Data type int is a 32-bit type and has a range from -2,147,483,648 to 2,147,483,647. To declare int type variables we use int keyword. for example
int a;
int number;

Java long data type

Data type long is a 64-bit type and has a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Data type long is used when int type is not large enough to hold the value. To declare a long type of long keyword is used. for example-
long distance;
long speed;


Java float data type

Data type float has 32 bits of storage and specifies a single-precision. When we need a fractional component type float is useful. To declare float type float keyword is used. For example- 
float temperature;
float pi = 3.14;

Java double data type

Data type double has 64 bits of storage and specifies double precision. To declare a double data type double keyword is used. for example 
double var;

Java char data type

In Java, char is used to store characters. Java uses Unicode to represent characters so Java char is a 16-bit type not the same as char in C/C++(8-bit).  The range of char is 0 to 65,536. To declare a char data type in Java char keyword is used. for example 
char a;
char b = 's;

Java boolean data type

Data type boolean can have an only true or false value. To declare boolean data types in Java boolean keyword is used. For example
boolean a;
boolean b = true;

Below Java Program will explain the Java Data Types- 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class DataTypeExample{
    public static void main(String args[]){
    int it = 45;
    System.out.println("This is an int it: " + it);
    
    byte bt = 125;
    //byte bt1 = 129; this will give overflow error out of byte range.
    System.out.println("This is byte variable bt : " + bt);
    
    short st = 54;
    System.out.println("This is short type st : " + st);
    
    long distance = 27452937356723l; //to declare long we have to append 'l' or 'L'
    System.out.println("This is long type distance : " + distance);
    
    float ft = 3.14f;
    //in Java floating point default to double precision
    //to specify float we have to append 'f' or 'F'.
    System.out.println("This is float type ft: " + ft);
    
    double dt = 3.14;
    System.out.println("This is double type dt: " + dt);
    
    char ch1 = 'X';
    char ch2 = 66;
    System.out.println("This is char ch1 : " + ch1);
    System.out.println("This is char ch2 : " + ch2);
    
    boolean b1 = true;
    if(b1){
        System.out.println("This is boolean b1 with having value " + b1);
    }
 }
}


Output>>>>
This is an int it: 45
This is byte variable bt : 125
This is short type st : 54
This is long type distance : 27452937356723
This is float type ft: 3.14
This is double type dt: 3.14
This is char ch1 : X
This is char ch2 : B
This is boolean b1 with having value true



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

New Year Chaos Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala