Programming





Data Types in Java

Data Types in Java


by Administrator | 25-Mar-2020
Java

Tags:  Core Java  Data Types

Share: 


Data types are used to provide important information to the compiler about the data being used in a program. That information helps the compiler in assigning memory space required for the data and the range of the data which can be used. In java we can use following types of data types:

  1. Primitive: A primitive data type specifies the size and type of variable values, and it has no additional methods. If we are allocating the memory by using these data type then the actual data will be stored in the allocated memory space.

Data type

Size required

Default value

Minimum value

Maximum value

boolean

Unspecified/JVM Specific

false

true or false

char

2 (16 bits)

ASCII-0

0

215-1 (32767)

Unicode-\u0000

('\u0000')

('\uFFFF')

byte

1 (8 bits)

0

-27 (–128)

27-1(–127)

short

2 (16 bits)

0

-215 (–32768)

215-1 (32767)

int

4 (32 bits)

0

-231 (–2147483648)

231-1 (2147483647)

long

8 (64 bits)

0L

-263

263-1

float

4 (32 bits)

0.0f

-3.4E+38

+3.4E+38

double

8 (64 bits)

0.0d

-1.7E+308

+1.7E+308

 

  1. Non-Primitive: A non-primitive data type is what the user creates. It cannot be calculated beforehand. These are few examples:
    1. String: A string is a group of characters.
    2. Array: An array is a group of various primitives
    3. Class: A class is a group of objects, methods and variables.

java-data-types

Why Non-primitives don’t have pre-defined size?

Because all the things defined under non-primitives are not constant. They depend on the user. Nobody can know what are the values the user is going to be storing in a String literal or the number of elements in an Array or number of variables, methods inside a class.


Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.