Page 69 - CA_Blue( J )_Class9
P. 69

Type            Size       Default value      Example
                                   char            2 bytes        \u0000         char c='a';
                                                                                 char d= '\u0041';

                 Java uses Unicode system not ASCII code system and to represent Unicode system 8 bits is not enough to represent
                 all characters so java uses 16 bits or 2 bytes for characters.


                 Boolean Type
                 The boolean data type is used to store only two possible values: true and false. This data type is used for simple
                 flags that track true/false conditions. The boolean data type specifies one bit of information.


                            Type           Size       Default value   Range of values that       Example
                                                                         can be stored

                       boolean        1 bit           false          true or false          boolean a=true;


                       Note:  1. Integer type and Float type are Numeric data types.
                              2. Character type and Boolean type are Non-numeric Data types.


                 4.5.2 Non-Primitive Data Type
                 They are also called Reference Data Types or Composite Data types. They contain a memory address of variable
                 value because the reference types won’t store the variable value directly in memory. They are defined by the
                 programmer according to the requirement of the program. Different types of Non-Primitive data types are strings,
                 objects, arrays and interfaces.

                 4.5.3 Difference between Primitive Data Type and Non-Primitive Data Type
                                 Primitive Data Type                             Non-Primitive Data Type

                 1. They are built-in data types.                   They are used defined data types.
                 2. There are 8 Primitive data types.               There are mainly 4 types of non-Primitive data types.

                 3. To use them “new” operator is not required.     Use of “new” operator is  mandatory for creating
                                                                    instances.
                 4. The size of a primitive type depends on the data type. Size of non-primitive types may vary.
                 5. Primitive data  types hold only single values.  The non-primitive  data types  are  used to store  the
                                                                    group of values.


                     4.6 VARIABLE
                 A variable is a name given to a space in computer memory to store a value. In other words, it is a reference to a
                 memory location where data is held. The term "variable" comes from the combination of "vary" and "able," meaning
                 its value can change during the program's execution. Variables can take different values at various points, depending
                 on the logic of the program. You might be wondering, what is the difference between an identifier and a variable? An
                 identifier is a name used to uniquely identify an entity (such as a function, class, or variable) in the program. It is a
                 general term that encompasses variables, functions, constants, and more. On the other hand, a variable is a specific
                 type of identifier that refers to a memory location used to store data that can change as the program runs.


                 4.6.1 Declaring a Variable
                 Before using a variable in Java, it must first be declared. If a variable is not declared, it cannot be used, and
                 attempting to do so will result in a compile-time error.


                                                                                               Values and Types  67
   64   65   66   67   68   69   70   71   72   73   74