Trusted answers to developer questions

Primitive data types in Java

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

Java is a widely used language widely in the industry of computer science. The specialty of Java is that it provides predefined data types (also known as primitive data types). These data types have no additional methods attached to them and are priorly well-defined. The other type of data Java has is known as non-primitive data. This data type is provided by the programmer.

svg viewer

Primitive data types

There are eight primitive data types in Java:

  • byte: This data type can store whole numbers ranging from -128 to 127. The size of Byte in the storage is 1 byte.

  • short: This data type has the storage size of 2 bytes and can store numbers from -32,768 to 32,767.

  • int: This data type has a storage size of 4 bytes. It can store numbers ranging from -2,147,483,648 to 2,147,483,647.

  • long: This data type has the storage size of 8 bytes. It stores numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

  • float: This data type has a storage size of 4 bytes. It is different from all the data types mentioned above because it can store fractional or decimal numbers. If we declare int a = 8.29, it will only store a = 8. This is solved by the float data type which will store the complete 8.29.

  • double: This data type has the storage size of 8 bytes; it stores fractional numbers and is very similar to the float data type. The only difference is that double has double the storage capacity of float.

  • boolean: This data type has a storage size of 1 bit. It stores true or false values (i.e.,1 or 0).

  • char: This data type has the storage size of 2 bytes. It stores a single character/letter or ASCII value with the ' ' quotation marks (e.g., char a ='a';).

RELATED TAGS

primitive
data
types
java
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?