Page 232 - IT-802_class_12
P. 232

Variables in a program, as you may have noticed, serve as placeholders for data handled by the program. Variables’
        values can change while the program is running.
        Local variables are what you’ve seen so far in this section. These variables are only accessible within the methods in
        which they are stated.

        3.2.2 Primitive Data Types
        We may use six more data types in addition to the two Java data types (int, double) that you saw in the Percentage
        Calculator program. There are eight primitive data types in Java which are given in the table below:

                          Data Type                   Type of values                     Size
                Byte                          Integer                       8-bit
                Short                         Integer                       16-bit
                Int                           Integer                       32-bit
                Long                          Integer                       64-bit
                Float                         Floating Point                32-bit

                Double                        Floating Point                64-bit
                Char                          Character                     16-bit
                Boolean                       True or False                 1-bit

        Rules for naming Variable Name
        Some rules for naming variables are as follows:

        Ð ÐVariable names can begin with either an alphabetic character, an underscore (_), or a dollar sign ($). However,
           convention is to begin a variable name with a letter. They can consist of only alphabets, digits, and underscore.

        Ð ÐVariable  names  must  be  one  word.  Spaces  are  not  allowed  in  variable  names.  Underscores  are  allowed.
           “total_marks” is fine but “total marks” is not.

        Ð ÐThere are some reserved words in Java that cannot be used as variable names, for example – int.
        Ð ÐJava is a case-sensitive language. Variable names written in capital letters differ from variable names with the same
           spelling but written in small letters. For example, the variable name “percentage” differs from the variable name
           “PERCENTAGE” or “Percentage”.

        Ð ÐIt is good practice to make variable names meaningful. The name should indicate the use of that variable.
        Ð ÐYou can define multiple variables of the same type in one statement by separating each with a comma. For example,
           you can define three integer variables as shown: int num1, num2, num3;
        3.2.3 String Variables

        We use variables to hold numeric data in the previous section’s Percentage Calculator program. Variables are frequently
        used to hold textual data, such as a student’s name.
        A single character can be stored in a variable of the primitive data type char. To provide a char variable a value, we
        enclose the character in single quotes.

        char middle_name = ‘m’;
        To store more than one character, we use the String class in Java. To assign a text value to a String variable we enclose
        the text between double quotes.
        For example,

        String first_name = “Mayank”; String last_name = “Saxena”;




          230   Touchpad Information Technology-XII
   227   228   229   230   231   232   233   234   235   236   237