DEV Community

Cover image for Data Types In Java
Samuel K.M
Samuel K.M

Posted on • Edited on

Data Types In Java

Before taking a deep dive into any programming knowledge, its very important to understand the basics. One of the most important thing to understand is data types. As long as you are a programmer you will always work with data types.

In Java there are two types of data types:

  1. Primitive
  2. Reference

Primitive Data Types

Integer Type

Under this type we have four other types,namely:

  • byte has a size of 1 byte and holds integer -128 to 127
  • short has a size of 2 bytes and holds integer --32,768 to 32,767
  • int has a size of 4 bytes and holds integer -2 billion to 2 billion
  • long has a size of 8 bytes and holds integer -2 quatrillion to 2 quatrillion Note it ends with an "L" 3999999999467898997777776088768759L

Decimal Type

Under this type we have two types,namely:

  • float has a size of 4 bytes and holds fractional number up to 7 digits. Note it ends with an "f" 3.467899f
  • double has a size of 4 bytes and holds and holds fractional number up to 15 digits. ****

Char Type

Single character / letter / ASCII value e.g 'h'. Has a size of 2 bytes

Boolean

Has a size of 1 bit and its either True Or False

Reference Data Types

String Type

A sequence of characters e.g 'hello world'. It has varying size.

Difference between Primitive and Reference Data Types

Primitive Data Types

  1. 8 types
  2. Stores data
  3. Can only hold one value
  4. Less memory
  5. Fast

Reference Type

  1. Unlimited(user defined)
  2. Stores an address
  3. Could hold more than one value
  4. More memory
  5. Slower

Top comments (2)

Collapse
 
sanket2021 profile image
Sanket Barapatre

Hey I think short can hold more values than -128 to 127.
Refer: docs.oracle.com/javase/tutorial/ja...

Collapse
 
bazeng profile image
Samuel K.M

Hello Thanks, that was an editing error. Glad you pointed it out