Java Data Types

What is Java Data Types?

Java, data types are used to specify the type of data that a variable can hold. Java has two categories of data types: primitive data types and Non-Primitive data types.

Primitive: There are 8 Primitivity data types

Non-Primitive: Which include Classes, Interfaces, and Arrays.

java-data-types-primitive-non-primitive
  1. byte: It’s the smallest data type in Java. It stores whole numbers from -128 to 127. For example, you might use a byte to store someone’s age.
  2. short: This data type is larger than byte and can store whole numbers from -32,768 to 32,767. Think of it like a byte, but with a wider range.
  3. int: Short for “integer,” this data type can store whole numbers from -2,147,483,648 to 2,147,483,647. It’s commonly used for variables representing counts or indices.
  4. long: For even larger whole numbers, you can use long. It has a much wider range, from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  5. float: If you need to store numbers with decimal points, use float. It’s good for approximations and can handle a wide range of values.
  6. double: Similar to float but with higher precision. If you need more accurate decimal values, go for double. It’s commonly used for financial calculations.
  7. char: Stands for “character.” It can store a single character, like ‘a’ or ‘$’. Useful for handling individual letters or symbols.
  8. boolean: This data type has only two possible values: true or false. It’s perfect for variables that represent conditions or states.

Non-primitive data types are also known as reference data types. Unlike primitive data types that store the actual values, reference data types store references (memory addresses) to objects. These data types represent objects, and they include classes, interfaces, arrays.