Data types
As we discussed in Java Variables when a variable is declared some part of memory is reserved. However, how much memory will be reserved depends upon the data type of the variable. It means how much memory will be reserved and which type of data can be stored in reserved memory depending upon the data type of the variable.
Data Types in Java
- Primitive data types: include boolean, char, byte, short, int, long, float, and double.
- Non-Primitive data types: includes Classes, Interfaces, and Arrays.
Primitive data types
Primitive data types refer to the most basic data types which include the below eight categories. These are considered building blocks of data manipulation.
- boolean
- byte
- char
- short
- int
- long
- float
- double
Boolean Java
The boolean data type is used to capture flag values for true/false conditions. It has only two possible values true/false.
The default value of the boolean is false.
Java Example to declare and use boolean data type.
package com.w3schools; public class BooleanTest { public static void main(String args[]){ //Declare boolean type variables. boolean var1 = true; boolean var2 = false; //Use ternary operator to initialize. boolean var3 = (10 > 12) ? true : false; //Print variables value. System.out.println("var1 Value :" + var1); System.out.println("var2 Value :" + var2); System.out.println("var3 Value :" + var3); } }
Output:
var1 Value :true var2 Value :false var3 Value :false
Double Java
The double data type represents a double-precision 64-bit IEEE 754 floating point.
Range: 1.7e−308 to 1.7e+308
The default value of the double is 0.0d.
Java Example to declare and use double data type.
package com.w3schools; public class DoubleTest { public static void main(String args[]){ //Declare double type variables. double var1 = 32.454; double var2 = 178.44; //Print variables value. System.out.println("var1 Value :" + var1); System.out.println("var2 Value :" + var2); //Print Sum of var1 and var2. System.out.print("Sum: "); System.out.println(var1 + var2); } }
Output:
var1 Value :32.454 var2 Value :178.44 Sum: 210.894
Float Java
The float data type is a single-precision 32-bit IEEE 754 floating point. Its Range is 3.4e−038 to 3.4e+038.
The default value of the float is 0.0f.
Java Example to declare and use float data type.
package com.w3schools; public class FloatTest { public static void main(String args[]){ //Declare float type variables. float var1 = 32.454f; float var2 = 178.44f; //Print variables value. System.out.println("var1 Value :" + var1); System.out.println("var2 Value :" + var2); //Print Sum of var1 and var2. System.out.print("Sum: "); System.out.println(var1 + var2); } }
Output:
var1 Value :32.454 var2 Value :178.44 Sum: 210.894
Long Java
The long data type refers to a 64-bit two’s complement integer. The minimum and maximum values of signed long are -263 and 263-1 respectively. In Java SE 8 and subsequent versions, long represents an unsigned 64-bit long value, which has a minimum value of 0 and a maximum value of 264-1.
Range: -9223372036854775808 to 9223372036854775807.
The default value of the long is 0.
Java Example to declare and use long data type.
package com.w3schools; import java.util.Date; public class LongTest { public static void main(String args[]){ //Declare long type variable. long dateTime = new Date().getTime(); //Print variable value. System.out.println("Date Time Value: " + dateTime); } }
Output:
Date Time Value: 1695721528005
Short Java
The short data type is a 16-bit signed two’s complement integer. The minimum and maximum values of signed long are -32,768 and 32,767 (inclusive) respectively.
The default value of the long is 0.
Java Example to declare and use short data type.
package com.w3schools; public class ShortTest { public static void main(String args[]){ //Declare short type variables. short var1 = 10; short var2 = 20; //Print variables value. System.out.println("var1 Value: " + var1); System.out.println("var2 Value: " + var2); //Print Sum var1 and var2. System.out.print("Sum: "); System.out.println(var1 + var2); } }
Output:
var1 Value: 10 var2 Value: 20 Sum: 30
Byte Java
The byte data type refers to an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).
The default value of the long is 0.
Java Example to declare and use byte data type.
package com.w3schools; public class ByteTest { public static void main(String args[]){ //Declare byte type variables. byte var1 = 100; byte var2 = 20; //Print variables value. System.out.println("var1 Value: " + var1); System.out.println("var2 Value: " + var2); } }
Output:
var1 Value: 100 var2 Value: 20
Char Java
The char data type is used to store characters and represents a single 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive).
The default value of the long is u0000.
Java Example to declare and use char data type.
package com.w3schools; public class CharTest { public static void main(String args[]){ //Declare char type variables. char ch1 = 'J'; char ch2 = 74; //Print variables value. System.out.println("ch1 Value :" + ch1); System.out.println("ch2 Value :" + ch2); } }
Output:
ch1 Value :J ch2 Value :J
Int Java
The int data type is a 32-bit signed two’s complement integer. It has a minimum value of -231 and a maximum value of 231-1.
Range: -2147483648 to 2147483647
The default value of the long is 0.
Java Example to declare and use int data type.
package com.w3schools; public class IntTest { public static void main(String args[]){ //Declare int type variables. int num1 = 10; int num2 = 20; //Print variables value. System.out.println("Number 1 Value: " + num1); System.out.println("Number 2 Value: " + num2); //Print Sum num1 and num2. System.out.print("Sum: "); System.out.println(num1 + num2); } }
Output
Number 1 Value: 10 Number 2 Value: 20 Sum: 30