Ticker

6/recent/ticker-posts

Java (Data Types)

DATA TYPES IN JAVA

Data types are used to specify data type and size of values that is store in an identifier and Java programming language has a rich amount of data types.
 

In Java language, data types can be classify into two categories.
1. Primitive Data type
2. Non-Primitive Data type







PRIMITIVE DATA TYPE

1. boolean data type

2. byte data type

3. char data type

4. short data type

5. int data type

6. long data type

7. float data type

8. double data type


BOOLEAN DATA TYPE
The Boolean data type is used to only store two types of values (true and false) and this data type is used for simple flags, that track the true/false conditions.

The Boolean data type contain only one bit size information, but its "size" can't be accurately defined.

Example: Boolean b = true


BYTE DATA TYPE
The byte data type is an another example of primitive data type and i
t is an 8-bit signed two's complement integer. 
Its value range between -128 to 127 therefore its maximum value is 127, minimum value is -128 and its default value is 0.

The byte data type is used to save memory in large arrays where the memory savings is most required. 
It saves memory space because a byte is 4 times smaller than an integer. 

Example: byte a = 10, byte b = -20


SHORT DATA TYPE
The short data type is a 16-bit signed two's complement integer. Its value-range between -32,768 to 32,767.
Its default value is 0.

The short data type can also be used to save memory space, like byte data type. 

A short data type is 2 times smaller than an integer.

Example: short s1 = 10000, short r1 = -5000


INT DATA TYPE
The int data type is a 32-bit signed two's complement integer. 

Its value-range between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1).
The minimum value of int data type is - 2,147,483,648, maximum value is 2,147,483,647 and default value is 0.

The int data type is generally used as a default data type for the integral values, if there is no problem about memory space.

Example: int x = 100000, int y = -200000


LONG DATA TYPE
The long data type is a 64-bit two's complement integer and its value range between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1). 

Its minimum value is - 9,223,372,036,854,775,808, maximum value is 9,223,372,036,854,775,807 and default value is 0. 

The long data type is used when we need a large integer value, whose size more than provided by int data type.

Example: long y = 100000L, long z = -200000L


FLOAT DATA TYPE
The float data type is a single-precision 32-bit IEEE 754 floating point and its value range is unlimited. 
It is recommended to use a float (instead of double) if you need to save the memory space in the large arrays of floating point numbers. 

The float data type should never be used for precise values, such as currency. 
Its default value is 0.0F.

Example: float f1 = 234.5f


DOUBLE DATA TYPE
The double data type is a double-precision 64-bit IEEE 754 floating point its value range is also unlimited. 

The double data type is generally used for decimal values like float. 

The double data type also should never be used for precise values, such as currency. Its default value is 0.0d.

Example: double d1 = 12.3



CHAR DATA TYPE
The char data type is a single 16-bit Unicode character and its value range between '\u0000' (or 0) to '\uffff' (or 65,535).

The char data type is used to store several types of characters.

Example: char c = 'A'





NON-PRIMITIVE DATA TYPE

A non-primitive data type is also known as reference data type and reference data type is used to refer to an object. 
A reference variable is declare to be specific and that type can never be change. 
To know more about the reference data type, please visit the Classes and Object concept.

IDENTIFIERS IN JAVA
All Java components require names and these names are used for naming classes, methods, interfaces and variables are called identifier. 

Identifier must follow some rules and here are the rules:

Rule 1: All identifiers must start with a letter (a to z or A to Z) or currency character($) or an underscore sign.

Rule 2: Only first letter must be written with a letter and after the first character, an identifier can have any combination of characters in its name.

Rule 3: A Java keyword cannot be used as an identifier name.

Rule 4: All the identifiers in Java are case sensitive, So there is huge difference between woo and Woo identifiers.















Post a Comment

0 Comments