Page 139 - ComputerScience_Class_11
P. 139

Escape Sequence                 Required for
                                               \f         Inserts a form feed.
                                               \’         Inserts a single quote character.
                                               \”         Inserts a double quote character.
                                               \\         Inserts a backslash character.
                                               \0         Assigns a null to a String variable.
                                               \v         Inserts a vertical tab.


                     6.3 DATA TYPES
                 A data type is an attribute that tells the compiler or interpreter how the programmer intends to use the data or value.
                 It specifies the type of data that can be stored in a variable and the operations that can be performed on it. Choosing
                 an appropriate data type helps manage memory efficiently and ensures that the program works correctly.


                 6.3.1 Different Data Types in Java
                 There are two types of data that can be handled by the codes written in Java which are as follows:

                                                                Data Types




                                      Primitive                                          Non-Primitive



                 Primitive Data Types
                 Primitive data types are also known as built-in data types. They can store only values which are of predefined types.
                 There are eight different types of primitive data types. They are byte, short, int, long, float, double, boolean and char.

                                                                 Primitive




                                     byte    short     int     long    float  double   boolean  char

                 Now we will discuss the primitive data types in detail.
                 •  byte: The byte data type stores integer values ranging from -128 to 127. The default value is 0. For example, byte
                   a=111, byte b=-120, etc.
                 •  short: The range of the data type is from -32,768 up to 32,767 and takes the default value as 0. The total size is 2
                   bytes or 16 bits. For example, short s=12987, t=-23176.
                 •  int: This data type is commonly used to store integer values in a program. The size is 4 bytes or 32 bits and can hold
                                       31
                              31
                   data from -2  up to 2 -1. For example, int a=102948586, b=-29384.
                 •  long: The size of this data type is 8 bytes or 64 bits. The signed range is from -2  to 2 -1 and the unsigned range is
                                                                                         63
                                                                                              63
                   from 0 to 2 -1. For example, long a=19283746L, long b=-12384738L.
                             64
                 •  float: The size of this data type is 4 bytes. It can store fractional numbers. The range of this data type is from
                   3.4e-038 to 3.4e+038 up to values 7 decimals digits. For example,  float a = 10.67f;
                 •  double: The size of this data type is 8 bytes or 64 bits. The double data type can store fractional numbers from
                   1.7e-308 to 1.7e+308 i.e., values up to 16 decimal digits. For example, double b = 1234.87674;
                 •  boolean: The Boolean data type is used to store only two possible values: true or false. It specifies one bit of
                   information. The default value is “false”.
                 •  char: The char data type is used to store a single character. It represents a 16-bit Unicode character and occupies 2
                   bytes of memory.




                                                                      Primitive Values, Wrapper Classes, Types and Casting  137
   134   135   136   137   138   139   140   141   142   143   144