Java Data Types - Data Types in Java, Primitive data types
Java Data Types Examples
Java Data Types
What are Data Types in Java ?
Java Data type specifies the size and type of values that can be stored in an identifier . In other words Data type defines the values that a variable can take. There are two types of data types in Java :
- Primitive Data Types
- Non-Primitive Data Types
In this Java Data Type Tutorial, you can learn about different data types that Java programming language supports for creating variables including Primitive Data Types and Non-Primitive Data Types .
What are Java Primitive data types ?
The Java primitive data types include boolean, char, byte, short, int, long, float and double . A primitive data type is pre-defined by the programming language. The size and type of variable values are specified. The Java programming language is statically-typed, which means that all variables must first be declared before they can be used.
What are Java Non-primitive data types ?
The Java Non-primitive data types are not actually defined by the programming language but are created by the programmer. They are also called Reference Variables or Object References since they reference a memory location which stores the data. The Java non-primitive data types include Classes, Interfaces, and Arrays .
What are the 8 Primitive Data Types in Java ?
There are 8 Primitive data types in Java . The following list of Java Primitive Data Types showing their Range , Memory Size in Bytes and Default Value of each Data Type. The eight primitive data types in Java are :
Java Primitive Data Types and Sizes Chart | |||
---|---|---|---|
Data Type | Range | Memory (in bytes) |
Default Value |
boolean | true or false | 1 bit | false |
byte | -128 to 127 | 1 | 0 |
char | any character or 0 to 65535 | 2 | '\u0000' |
short | -32768 to 32767 | 2 | 0 |
int | -2147483648 to 2147483647 | 4 | 0 |
float | 1.40129846432481707e-45 to 3.40282346638528860e+38 | 4 | 0.0f |
long | -9223372036854775808 to 9223372036854775807 | 8 | 0L |
double | 4.94065645841246544e-324d to 1.79769313486231570e+308d | 8 | 0.0d |
A primitive data type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language. Following explanation shows the min and max values of respected data type in Java.
-
byte : The
byte
data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). Thebyte
data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place ofint
where their limits help to clarify your code; the fact that a variable's range is limited. -
short : The
short
data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As withbyte
, the same guidelines apply: you can use ashort
to save memory in large arrays, in situations where the memory savings actually matters. -
int : By default, the
int
data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use theint
data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to useint
data type as an unsigned integer. Static methods likecompareUnsigned
,divideUnsigned
etc have been added to theJava Integer class
to support the arithmetic operations for unsigned integers. -
long : The
long
data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use thelong
data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided byint
. TheJava Long class
also contains methods likecompareUnsigned
,divideUnsigned
etc to support arithmetic operations for unsigned long. -
float : The
float
data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations forbyte
andshort
, use afloat
(instead ofdouble
) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings coversBigDecimal
and other useful classes provided by the Java platform. -
double : The
double
data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency. -
boolean : The
boolean
data type has only two possible values:true
andfalse
. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined. -
char : The
char
data type is 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).
Default Values of Primitive Data Types in Java
It's not always necessary to assign a value when a field is declared in Java. Fields that are declared but not initialized will be set to default values by Java compiler. This default value will be zero or null, depending on the data type in Java programming.
The following chart summarizes the default values for the Java data types.
Default values for primitive types | |
---|---|
Data Type | Default Value (for fields) |
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | '\u0000' |
String (or any object) | null |
boolean | false |
Local variables are slightly different, the compiler never assigns a default value to an uninitialized local variable in Java. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it in Java. Accessing an uninitialized local variable will result in a compile time error in Java.
What is the difference between primitive and non primitive data types in Java ?
The difference between primitive and non-primitive data types are as follows :
Primitive Data Type | Non-Primitive Data Type |
---|---|
Primitive data types are predefined data types in Java. | Non-primitive data types are created by the user and is not defined by Java. |
Primitive types cannot be used to call methods to perform certain operations. | Non-primitive types can be used to call methods to perform certain operations. |
The primitive data type has always a value. | Non-primitive types can be null. |
The primitive type starts with a lowercase letter as they are keywords in Java. | The non-primitive types starts with an uppercase letter as they are objects in Java. |
The size of a primitive type depends on the data type. | The non-primitive types have all the same size. Since they reference a memory location which stores the data. |
What is a boolean Data Type in Java ?
boolean is a primitive data type in Java . boolean means true or false i.e. logical true or false . It occupies 1 bit in memory . These values do not correspond to 1 or 0 as in C or C++ . The following example shows how to use boolean data type in Java .
The boolean Primitive Data Type in Java Programming Language
- The boolean data type represents only 1 bit in size.
- The boolean is a primitive data type in Java .
- The boolean data type is used for logical values .
- The boolean data type can have two possible values true or false .
- The boolean is the type returned by all relational operators
- The boolean is the type required by the conditional expressions used in control statements such as if, for and while so on .
- Boolean is wrapper class for boolean primitive data type .
- The boolean primitive data type is also used in creating boolean array .
How many ways to use boolean value in Java ?
- The boolean value is used to check whether condition is true or false .
- You can use boolean true value in while loop condition for infinite loop .
- No need to do == to check equality
Java boolean Data Type Example
/* Java boolean Data Type Example Save with file name BooleanDataType.java */ public class BooleanDataType { public static void main(String args[]) { // TYPE DECLARATION DEFAULT VALUE OF boolean IS false boolean b; System.out.println("Java boolean Data Type Example"); // ASSIGN A VALUE b = true; System.out.println("Assigned boolean Value : " + b); } }
What is a byte Data Type in Java ?
byte is a primitive data type in Java . byte data type holds numeric value between -128 and 127 . Java Byte data type is used to save space in arrays . The following example shows how to use byte data type in Java .
Java byte Data Type Example
/* Java byte Data Type Example Save with file name ByteDataType.java */ public class ByteDataType { public static void main(String args[]) { // TYPE DECLARATION DEFAULT VALUE OF byte IS 0 byte b; System.out.println("Java byte Data Type Example"); // ASSIGN A VALUE b = 77; System.out.println("Assigned byte Value : " + b); } }
What is a char Data Type in Java ?
char is a primitive data type in Java . char data type holds any character or 0 to 65535 in it . char is short for character in Java . It is 16 bits in size . The actual data stored in the char data type does not take up more than 8 bits . The following example shows how to use char data type in Java .
Java char Data Type Example
/* Java char Data Type Example Save with file name CharDataType.java */ public class CharDataType { public static void main(String args[]) { // TYPE DECLARATION DEFAULT VALUE OF char IS '\u0000' char c; System.out.println("char Example"); // ASSIGN A VALUE c = 'A'; System.out.println("Assigned char Value : " + c); // ASSIGN small a ascii value c = 97; System.out.println("Assigned char ASCII Value : " + c); } }
What is a short Data Type in Java ?
short is a primitive data type in Java . short data type holds numeric value between -32768 and 32767 . The short data type is a 16-bit signed two's complement integer. You can use a Java short to save memory in large arrays, in situations where the memory savings actually matters.
In Java, the short data type is the smallest type compared to int and long . The following example shows how to use short data type in Java .
Java short Data Type Example
/* Java short Data Type Example Save with file name ShortDataType.java */ public class ShortDataType { public static void main(String args[]) { // TYPE DECLARATION DEFAULT VALUE OF short IS 0 short s; System.out.println("Java short Data Type Example"); // ASSIGN A VALUE s = 123; System.out.println("Assigned short Value : " + s); } }
What is a int Data Type in Java ?
int is a primitive data type in Java . int data type holds numeric value between -2147483648 and 2147483647 . The Java int data type is a 32-bit signed two's complement integer . Java int data type stores whole numbers, positive or negative such as 786 or -1234, without decimals. The following example shows how to use int data type in Java .
Java int Data Type Example
/* Java int Data Type Example Save with file name IntDataType.java */ public class IntDataType { public static void main(String args[]) { // TYPE DECLARATION DEFAULT VALUE OF int IS 0 int i; System.out.println("Java int Data Type Example"); // ASSIGN A VALUE i = 5; System.out.println("Assigned int Value : " + i); } }
What is a float Data Type in Java ?
float is a primitive data type in Java . float data type holds numeric value between 1.40129846432481707e-45 and 3.40282346638528860e+38 . The following example shows how to use float data type in Java .
Java float Data Type Example
/* Java float Data Type Example Save with file name FloatDataType.java */ public class FloatDataType { public static void main(String args[]) { // TYPE DECLARATION DEFAULT VALUE OF float IS 0f float a; System.out.println("Java float Data Type Example"); // ASSIGN A VALUE LIKE THIS COMPILER SHOW ERROR // POSSIBLE LOSS OF PRECISION FOUND DOUBLE REQUIRED FLOAT //a = 5.25; // GET COMPILATION ERROR COMPILER ASSUMES double a = 5.25f; // ASSIGN A float VALUE System.out.println("Assigned float Value : " + a); } }
What is a long Data Type in Java ?
long is a primitive data type in Java . long data type holds numeric value between -9223372036854775808 and 9223372036854775807 . The following example shows how to use long data type in Java .
Java long Data Type Example
/* Java long Data Type Example Save with file name LongDataType.java */ public class LongDataType { public static void main(String args[]) { // TYPE DECLARATION DEFAULT VALUE OF long IS 0L long a; System.out.println("long Example"); // ASSIGN A VALUE a = 45000L; System.out.println("Assigned long Value : " + a); } }
What is a double Data Type in Java ?
double is a primitive data type in Java . double data type holds numeric value between 4.94065645841246544e-324d and 1.79769313486231570e+308d . The following example shows how to use double data type in Java .
Java double Data Type Example
/* Java double Data Type Example Save with file name DoubleDataType.java */ public class DoubleDataType { public static void main(String args[]) { // TYPE DECLARATION DEFAULT VALUE OF double IS 0d double a; System.out.println("double Example"); // ASSIGN A double VALUE a = 5.25d; System.out.println("Assigned double Value : " + a); } }