Page 65 - CA_Blue( J )_Class9
P. 65

Literals in Java are values that are assigned to a variable. They are used by programmers and can be of any data type.
                 Literals are also called constants. For example, int a=20; (Here, 20 is a literal). There are six different types of literals.

                                                              LITERALS




                       Integer      Floating Point     Character         String         Boolean           Null
                       Literals        Literals         Literals        Literals         Literals        Literals


                 Integer Literals
                 Integer literals are whole numbers that do not contain a decimal point. They may be positive or negative value.
                 There are of four different types of integer literals: Decimal integer literal, Binary integer literals, Octal integer
                 literals and Hexadecimal integer literals. For example: 10101, 27, 0xA2. (Here, 0x is used for numbers representing
                 Hexadecimal integer literals).

                 Floating Point Literals
                 Floating point literals are real numbers or numbers with decimal points. They may be positive or negative value.
                 They can only be specified in decimal forms and not in octal or hexadecimal form. For example, 2.5, 0.235, 1.0E-3,
                 3f (where f is used, to take float value and decimal numbers without f values are considered double).

                 Character Literals
                 Character literals are Alphanumeric in nature. There are four types of character literals:

                 •  Single Character: A single alphanumeric character enclosed in single quotes (e.g., 'A', '7').
                 •  Character as an Integral Literal: An integer value representing a character, such as 062 for the character 'C'
                    (using its ASCII value).
                 •  Unicode Representation: Characters can also be represented by Unicode escape sequences (e.g., '\u0061' for
                    the character 'a').
                 •  Escape Sequences: Special characters such as newline ('\n'), tab ('\t'), and others are also valid character
                    literals

                 String Literals
                 String literals are any sequence of characters enclosed in double quote. For example, “India”, “Computer Application
                 of class 10”.


                 Boolean Literals
                 Boolean literals represent truth values and can be either true or false.


                 Null Literals
                 The null literal represents the absence of a value and can only be assigned to reference types (objects). It cannot
                 be assigned to primitive types (such as int, float, etc.). For example, String s= null;


                  Program 12     Write a program to show the use of different literals.

                   1  public class literals

                   2  {
                   3      public static void main()




                                                                                               Values and Types  63
   60   61   62   63   64   65   66   67   68   69   70