Pages

Tuesday, 26 April 2022

Java Data Types

There is a basic java data type for each variable in Java.

The data type of a variable determines the type of data the variable can contain, and what operations we can execute on it.

Every bit of processed data is divided into types. There are various kinds of data types in Java, the data types are mainly of two categories:
a. Primitive Data Types in Java
Primitive data types are fundamental data types offered by Java. These are the basic data values. These data types are hard coded into the Java compiler so that it can recognize them during the execution of the program.

There are 8 types of primitive data types in Java:
Data Type   Default size
byte          1 byte
short          2 byte
int             4 byte
long             8 byte
double     8 byte
float             4 byte
boolean         1 bit
char             2 byte
Note:
  • byte, short, int and long data types are used for storing whole numbers.
  • float and double are used for fractional numbers.
  • char is used for storing characters(letters).
  • boolean data type is used for variables that holds either true or false.
byte:
This can hold whole number between -128 and 127. Mostly used to save memory and when you are certain that the numbers would be in the limit specified by byte data type.
Default size of this data type: 1 byte.
Default value: 0
Example:
class JavaExample 
{
    public static void main(String[] args)
    {
    byte num;
    num = 113;
    System.out.println(num);
    }
}
Output:
113

Note:
Try the same program by assigning value assigning 150 value to variable num, you would get type mismatch error because the value 150 is out of the range of byte data type. The range of byte as I mentioned above is -128 to 127.

short:
This is greater than byte in terms of size and less than integer. Its range is -32,768 to 32767.
Default size of this data type: 2 byte

short num = 45678;

int: 
Used when short is not large enough to hold the number, it has a wider range: -2,147,483,648 to 2,147,483,647
Default size: 4 byte
Default value: 0
Example:
class JavaExample
 {
    public static void main(String[] args)
   {
       short num;
       num = 150;
    System.out.println(num);
    }
}
Output:
150
The byte data type couldn’t hold the value 150 but a short data type can because it has a wider range.

long:
Used when int is not large enough to hold the value, it has wider range than int data type, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
size: 8 bytes
Default value: 0
Example:
class JavaExample 
{
    public static void main(String[] args) 
    {
      long num = -12332252626L;
    System.out.println(num);
    }
}
Output:
-12332252626

double:
Sufficient for holding 15 decimal digits
size: 8 bytes
Example:
class JavaExample 
{
    public static void main(String[] args)
   {
      double num = -42937737.9d;
    System.out.println(num);
    }
}
Output:
-4.29377379E7

float: 
Sufficient for holding 6 to 7 decimal digits
size: 4 bytes
Example:
class JavaExample 
{
    public static void main(String[] args) 
    {
      float num = 19.98f;
    System.out.println(num);
    }
}
Output:
19.98

boolean:
holds either true of false.
Example:
class JavaExample 
{
    public static void main(String[] args) 
   {
     boolean b = false;
    System.out.println(b);
    }
}
Output:
false

char: 
It holds characters.
size: 2 bytes
Example:
class JavaExample 
{
    public static void main(String[] args) 
    {
        char ch = 'Z';
    System.out.println(ch);
    }
}
Output:
Z

b. Non-Primitive Data Types in Java
Non-Primitive data types are the reference data types. These are the special data types that are user-defined. The program already contains their definition. Some examples of non-primitive or reference data types are classes, interfaces, String, arrays, etc.

Type casting:
Type casting is nothing but assigning a value of one primitive data type to another. When you assign the value of one data type to another, you should be aware of the compatibility of the data type. If they are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion and if not, then they need to be casted or converted explicitly.

There are two types of casting in Java as follows:
Widening Casting(automatically): This involves the conversion of a smaller data type to the larger type size.
byte -> short -> char -> int -> long -> float -> double

Narrowing Casting(manually): This involves converting a larger data type to a smaller size type. 
double -> float -> long -> int -> char -> short -> byte

Widening Casting(Implicit)
This type of casting takes place when two data types are automatically converted. It is also known as Implicit Conversion. This happens when the two data types are compatible and also when we assign the value of a smaller data type to a larger data type.

Narrowing Casting(Explicit)
In this case, if you want to assign a value of larger data type to a smaller data type, you can perform Explicit type casting or narrowing. This is useful for incompatible data types where automatic conversion cannot be done.

Ex:
public class Narrowing
{
public static void main(String[] args)
{
double d = 200.06;

//explicit type casting 
long l = (long)d;
 
//explicit type casting
 int i =(int) l;
System.out.println("Double Data type value "+d);

//fractional part lost
System.out.println("Long Data type value "+l);

//fractional part lost
System.out.println("Int Data type value "+i);
}
}

Output:
Double Data type value 200.06
Long Data type value 200
Int Data type value 200


No comments:

Post a Comment

Servlet - JSP Programs

JSP(Java Server Pages) Programs : JavaServer Pages (JSP) is a Java standard technology that enables you to write dynamic data-driven pages f...