Data types in Java

Posted on

Data types:

Data types are used to declare variables in Java programs are

  • For Integers:byte: -128 to 127
    short: -32768 to 32767
    int: -2,147,483,648 to 2,147,483,648
    long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • For floating point:float:
    double:
  • Characters:char:
  • Boolean:boolean (i.e., true/false)

 

/*Demo on assigning the variables to integer data type.*/

class App1{

public static void main(String yck[])
{

int x;
x=100;
System.out.println(“The value of x is : “+x);
}

}

 

output:
\>javac App1.java
\>java App1
The value of x is : 100

 

/*demo on assigning the variable to double data type*/

class App2{

public static void main(String yck[])
{

double x;
x=100.2323;
System.out.println(“The value of x is : “+x);
}

}

 

output:
\>javac App2.java
\>java App2
The value of x is : 100.2323

 

/*demo on assigning the variable to float data type*/

class App3{

public static void main(String yck[])
{

float x;
x=100.2323f;       //x=(float)100.2323
System.out.println(“The value of x is : “+x);
}

}

 

Note: In Java we cannot directly assign the values to the float variables. So, for declaring the variables we need the help of type casting. There are two types of syntax’s to convert the given value into the float data type. They are

  1. Type ‘f’ at the end of the value.
    Example: x = 100.2323f;
  2. Use the type casting method same as in C and in C++.
    Example: x = (float) 100.2323;

 

output:
\>javac App3.java
\>java App3
The value of x is : 100.2323

Leave a Reply

Your email address will not be published. Required fields are marked *