Sat. Nov 16th, 2024

The datatypes represents type of data that we store into memory. The datatypes in java language are classified into three types

1) Primitive datatypes

The primitive datatypes are predefined and designed to store a single value.

Ex: int, char etc.

2) Derived datatypes

The derived data types are predefined and designed to store multiple values.

Ex: array

3) User defined datatypes

If a datatype is created by the user or the programmer, then it is called as user defined datatype. Using the user defined datatype we can store any number of values and any type of values, according to the application requirement.

Ex: class

Primitive datatypes

The primitive datatypes are predefined and designed to store a single value and they are used to store the basic inputs required for a program. The primitive data type are also called as fundamental data types.

The java language provides 8 primitive data types and they are classified into 4 categories.

  1. Integer
  2. Floating-point
  3. Character
  4. Boolean

1) Integer Category

This category can be used to storing number, either positive or negative without a decimal point. Under the integer we have 4 primitive data types and they are

a) byte

b) short

c) int

d) long

All the 4 primitive data types are used for storing same kind of data, but their sizes and ranges are different so that the memory can be utilized efficiently without any wastage.

Data TypesSizeRange
Byte1-128 to 127
Short2032768 to 32767
Int4-2147483648 to 214743647
Long8-9223372036854775808 to 9223372036854775807
MSB 
         
Sign Bit 0 +ve 1 -veValue Bits

2) Floating- Point Category

The category used for storing numbers either positive or negative with decimal point. Under the floating category we have two primitive data types and they are

a) float

b) double

Both the primitive data types under floating-point category are used for storing same kind of data, but their sizes and ranges are different to that the memory can be utilized efficiency without wastage.

Data TypesSizeRangeNumber of decimal digits
Float41.4E-46 to 3.4E387
double84.9E-324 to 1.79E30815

3) Character Category:

This category can be used for storing a single character. A character can be represented by either one alphabet one special symbol. Under the character there is only on primitive data type and it is char.

Data TypesSizeRange
Char20 to 65535

Qns: Why is the size of char 1 byte in C language and 2 bytes in Java?

Ans) When an application is developed in C language, it uses the characters of only english language. To store the English language characters 1 byte of memory is sufficient. The C language uses ASCII character set (0-255).

When an application is developed in java language, it uses the character of all the foreign languages. To store the characters of all languages 1 byte of memory is not sufficient, therefore the size is increased to 2 bytes of Java language. The Java language use UNICODE character set (0-65535).

4) Boolean Category

This category is used for storing either true or false. Under the boolean category we have only primitive type i.e. boolean

Data TypesSizeRange
BooleanJVM DependentTrue/false

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *