Data Types in Java

Data Types in Java Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java: 1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double. 2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, String and Arrays. Java is a statically-typed programming language. It means, all variables must be declared before its use. That is why we need to declare variable's type and name. We have to explicitly declare variable before it use. _ Primitive Data Types:_ These are predefined by the language and represent basic values. There are eight primitive data types in Java: byte: 8-bit signed integer. Range: -128 to 127. short: 16-bit signed integer. Range: -32,768 to 32,767. int: 32-bit signed integer. Range: -2,147,483,648 to 2,147,483,647. long: 64-bit signed integer. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. float: 32-bit single-precision floating-point number. double: 64-bit double-precision floating-point number. boolean: Represents true or false values. char:16 bit unicode character _ Non-Primitive (Reference) Data Types:_ These types are created by the programmer and store references to objects in memory. Examples of non-primitive data types include: Classes: Templates for creating objects. Interfaces: Contracts that define methods for classes. Arrays: Collections of elements of the same data type. String: Represents a sequence of characters. Primitive vs Non-Primitive Data Types Primitive data types: Memory: stored in stack Speed: Primitive data types are faster Example: int a=10; Non-Primitive Data Types: Memory: stored in heap Speed: Non-Primitive data types are slower Example: String name="aaa";

Jun 3, 2025 - 03:50
 0
Data Types in Java

Data Types in Java
Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, String and Arrays.
Java is a statically-typed programming language. It means, all variables must be declared before its use. That is why we need to declare variable's type and name. We have to explicitly declare variable before it use.

_
Primitive Data Types:_
These are predefined by the language and represent basic values. There are eight primitive data types in Java:

  1. byte: 8-bit signed integer. Range: -128 to 127.
  2. short: 16-bit signed integer. Range: -32,768 to 32,767.
  3. int: 32-bit signed integer. Range: -2,147,483,648 to 2,147,483,647.
  4. long: 64-bit signed integer. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  5. float: 32-bit single-precision floating-point number.
  6. double: 64-bit double-precision floating-point number.
  7. boolean: Represents true or false values.
  8. char:16 bit unicode character

_
Non-Primitive (Reference) Data Types:_
These types are created by the programmer and store references to objects in memory. Examples of non-primitive data types include:

Classes: Templates for creating objects.
Interfaces: Contracts that define methods for classes.
Arrays: Collections of elements of the same data type.
String: Represents a sequence of characters.

Primitive vs Non-Primitive Data Types
Primitive data types:

  • Memory: stored in stack
  • Speed: Primitive data types are faster
  • Example: int a=10;

Non-Primitive Data Types:

  • Memory: stored in heap
  • Speed: Non-Primitive data types are slower
  • Example: String name="aaa";