Image

Java - Core Java - Datatype

Datatype

Datatype tells what type of value hold by the variable.

  • Each and every variable and expression has type
  • Strongly typed programming language
  • Each and every datatype is clearly defined. Every assignment checked by compiler for type compatibility because of above reason we can conclude java language is strongly typed programming language.
Q. Is java pure object oriented programming language?

Ans: Java is not considered as pure object oriented programming language because several OOPs features are not satisfied by java like operator overloading & multiple inheritance etc. Moreover we are depending on primitive datatypes which are non objects.

There are two types of datatypes in java
1. Primitive data types
2. Non-primitive data types 1562493070-image.png
DatatypeValueSize
booleanfalse1 bit
char'\u0000'2 byte
byte01 byte
short02 byte
int04 byte
long0L8 byte
float0.0f4 byte
double0.0d8 byte
1. Integer Group

Java defines four integer types:
byte, short, int, long.
All of these are signed, positive and negative values. Java does not support unsigned, positive-only integers. Many other computer languages, including C/C++, support both signed and unsigned integers.

2. Byte

The smallest integer type is byte. This is a signed 8-bit type that has a range from –128 to 127. Application (Variables of type byte are especially useful when you’re working with a stream of data from a network or file.

•	Size		        -	 1 byte (8 bit)
•	Max value	-	+127
•	Min value	-	-128
•	Range		-	-128 to +127

1562909271-image.png
Example:
byte b=10;		(valid)
byte b=127;   	(valid)
byte b=128; 	        (invalid)
byte b=10.5;	        (invalid)
byte b=true;	        (invalid)
byte b=”ashish”;	(invalid)
Q. When should we go for byte

Ans: In java if we want to handle data as stream over network or files then we should go for byte.

3. short

short is a signed 16-bit type. It has a range from –32768 to 32767. It is probably the least-used Java type.

short
short is a signed 16-bit type. It has a range from –32768 to 32767. It is probably the least-used Java type.
•	Size		-	2 byte (16 bit)
•	Max value	-	+32767
•	Min value	-	-32768
•	Range		-	–32768 to +32767

Example:
short s=32767; 	(valid)
short s=32768; 	(invalid)

Q. When should we go for byte?

Ans: It is best suitable for 16 bit processors like 8085, but this processors are completely outdated hence corresponding short is also outdated datatype.

4. int

The most commonly used integer type is int. It is a signed 32-bit type that has a range from –2147483648 to +2147483647. In addition to other uses, variables of type int are commonly employed to control loops and to index arrays.

•	Size		-	4 byte (32 bit)
•	Max value	-	+2147483647
•	Min value	-	–2147483648
•	Range		-	–2147483648 to +2147483647

Example:
int a=2147483647; 	(valid)
int a=2147483648;	(invalid)
int a=true;			(invalid)
5. long

long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value. The range of a long is quite large. This makes it useful when big, whole numbers are needed.

•	Size		-	8 byte (64 bit)
•	Max value	-	+2147483647
•	Min value	-	–2147483648
•	Range		-	-9223372036854775808 to +9223372036854775807
Float Group

Float category datatype are used for representing the data in the form of scale and precision i.e. these category datatype are used for representing float value. These category contain two type first is float and second is double.

1. float

• Float data type is a single-precision 32-bit IEEE 754 floating point
• Float is mainly used to save memory in large arrays of floating point numbers
• Default value is 0.0f
• Float data type is never used for precise values such as currency
• Example: float f1 = 234.5f

2. double

• double data type is a double-precision 64-bit IEEE 754 floating point
• This data type is generally used as the default data type for decimal values, generally the default choice
• Double data type should never be used for precise values such as currency
• Default value is 0.0d
• Example: double d1 = 123.4
Note: Both float and double data types were designed especially for scientific calculations, where approximation errors are acceptable. If accuracy is the most prior concern then, it is recommended not to use these data types and use Big Decimal class instead.

Character Group
1. char

A character is an identifier which is enclosed within single quotes. In java to represent character data we used a data type called char. This data type takes two bytes since it follows UNICODE character set. Java is available in 18 international languages and it follows UNICODE character set. UNICODE character set is one which contains all the characters which are available in 18 international languages and it contains 65536 characters that is why char is 2 byte.
• char data type is a single 16-bit Unicode character
• Minimum value is '\u0000' (or 0)
• Maximum value is '\uffff' (or 65,535 inclusive)
• Char data type is used to store any character
• Example: char letter = 'A'

Boolean Group
boolean

Boolean category data type is used for representing logical values i.e true or false values. This datatype takes 0 byte.
• boolean data type represents one bit of information
• There are only two possible values: true and false
• This data type is used for simple flags that track true/false conditions
• Default value is false
• Example: boolean one = true

Datatype	Value	Size
boolean	        false	1 bit
char	     '\u0000'	2 byte
byte	          0     	1 byte
short	          0	        2 byte