Skip to main content

Data Types

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:
                            Size: 2 bytes (16 bits)

                            Max value: 32,767
                            Min value: -32,768


  • int:
                            Size: 4 bytes (32 bits)

                            Range:  -2^31 to 2^31-1

            The most commonly used data type in java is int.

  • long:
                            Size: 8 bytes (64 bits)

                            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:
                    1. If we want 5-6 places of accuracy, then go for a float.
                    2. float follows single precision.
                    3. Size: 4 bytes (32 bits)
                    4. Range: -3.4e38 to 3.4e38

  • double:
                    1. If we want 14-15 places of accuracy, then go for double.
                    2. double follows double precision.
                    3. Size: 8 bytes (64 bits)
                    4. Range:  -1.7e308 to 1.7e308

  • boolean:
                    Size: Not applicable [Vertual Machine dependent]

                    Range: Not applicable [But allowed values are true and false]

                    The default value is false.

  • char:
                         Size: 2 bytes
                         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

Popular posts from this blog

Identifiers

Identifiers:                                         A name in java program is called identifier, which can be used for identification purposes. It can be a method name, variable name, class name, or label name . Rules for defining java identifiers: The only allowed characters in java identifiers are a - z, A - Z, 0 - 9, $, and _. If we are using other characters, we will get a compile-time error. Identifiers can't start with digits. Java identifiers are case sensitive. Of course, java is treated as case sensitive programming language. There is no limit for java identifiers but it is not recommended to take too lengthy identifiers. We can't use reserved words as identifiers. All predefined java class name and interface name we can use as identifiers.                     e.g.                         int String = 888 ;                         int Runnable = 999 ;                         Even this is valid, but it is not good programming practice. Examples Que 1) How many identi

Literals

Literals:                 Any constant value which can be assigned to the variable is called literal.     1 . Integer Literals:                                        For integral data types ( byte, short, int, long ), we can specify a literal value in the following base...                         a) Decimal literal (base 10):                                                     Allowed digits are 0 to 9.                                                                e.g.  int x = 10;                         b) Octal literal (base 8):                                                 Allowed digits are 0 to 7.                                             Literal should be prefixed with '0' (Zero).                                                       e.g.   int x = 010;                         c) Hexa decimal literal (base 16):                                                   Allowed digits are 0 to 9, a to f. For extra digits (a to f), we can use both lowercase and uppercase c

Reserved words

Reserved Words:                                    In java, some words are reserved to represent some meaning or functionality such types of words are called reserved words. Data type related keywords:                     1. byte                         2. short                         3. int                         5. long                         6. float                         7. double                         8. char Flow control-related keywords:                     1. if                         2. else                         3. switch                         4. case                         5. default                         6. do                         7. while                         8. for                         9. continue                         10. return                         11. break Modifiers related keywords:                     1. public                         2. private                         3. protected                         4. static