Datatype tells what type of value hold by the variable.
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
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.
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
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 +9223372036854775807Float 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
â€¢ 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.
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 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