Data Types:
In java, Every variable and every expression has some type. Each and every data type is clearly defined. Every assignment should be checked by the compiler for data compatibility.
Because of the above reason, we can conclude, Java language is strongly typed programming language.
Que :- Is java pure object-oriented programming language?
Ans :- Java is not considered as the pure object-oriented programming language, because several oop features are not satisfied by java (like operator overloading, Multiple inheritances, etc.).
Moreover, we are depending on primitive data types, which are non-objects.
Except for boolean and char, remaining data types are considered as signed data types. Because we can represent both positive and negative numbers.
- byte:
Size: 1 byte ( 8 bits )
Max value: 127
Min value: -128
Range: -128 to 127
The most significant bit acts as signed bit. 0 means positive number and 1 means negative number. Positive numbers will be represented directly in memory. Whereas, negative numbers are represented in 2's complement form.
- short:
Max value: 32,767
Min value: -32,768
- int:
Range: -2^31 to 2^31-1
The most commonly used data type in java is int.
- long:
Range: -2^63 to 2^63-1
Note: All the above data types meant for representing integer values. If we want to represent floating-point values, then we should go for floating-point data types.
- float:
2. float follows single precision.
3. Size: 4 bytes (32 bits)
4. Range: -3.4e38 to 3.4e38
- double:
2. double follows double precision.
3. Size: 8 bytes (64 bits)
4. Range: -1.7e308 to 1.7e308
- boolean:
Range: Not applicable [But allowed values are true and false]
The default value is false.
- char:
Range: 0 to 65535
Que:- Why 2 bytes in java for char?
Ans:-
Old languages [like c/c++] are ASCII code-based and the number of different allowed ASCII code characters is less than or equal to 256. To represent these 256 characters, 8 bits are enough. Hence the size of char in old languages is 1 byte. But java is Unicode based under the number of different Unicode characters that are greater than or equal to 256 and less than or equal to 65536. To represent these many characters 8 bits may not enough. Compulsory we should go for 16 bits. Hence, the size of the char in java is 2 bytes.
Note: Null is the default value for object reference and we can't apply for primitives. If we are trying to use it for primitive, then we will get a compile-time error.
Comments
Post a Comment