Saturday, 17 October 2015

Core Java with OCJP_SCJP Language Fundamentals Part-4 __ Literals video Part-1


Literals

                  A literal represents a constant value which can be assigned to the variables.

Integral Literal

             We can specify an integral literal in the following ways.
            Decimal literals:allowed digits are 0 to 9
               Ex: int x = 10;

Octal literals:allowed digits are 0 to 7 but here literal value should be prefixed with 0(zero)
       Ex: int x = 010;
Hexadecimal literals: the allowed digits are 0 to 9, A-F (Both lower, Upper case) literals
should be prefixed with 0x or oX
        Ex: int x = 0x10;


Ex:
class Test
{
     public static void main(String arg[])
   {
       int x = 10;
      int y = 010;
       int z = 0x10;
       System.out.println(x+ "..." + y + "..." + z);
     }
}

O/P:-10…8…16

Except decimal, octal, hexadecimal there is no other way to represents constant values for the
integral datatype.
By default every integral lateral is of int datatype we can specify explicitly. An integral literal is oflong type by suffixing with l or L.

Ex:

10 ---> int value.
10l --->long value.
long l = 10l;
int i = 10l;
C.E: possible loss of precision found : long
                                             Required:int


There is no way to specify explicitly an integral literal is of type byte and short.
If the integral literal is with in the range of byte then the JVM by default treats it as byte literal.Similarly short literal also.

Floating –point literals

By default floating-point literals are double type we can specify explicitly as float type by suffixing with ‘f’ or ‘F’

Which of the following are valid declarations


we can specify explicitly a floating point literal of double type by suffixing with d or D.
we can also represent float-point literals by using scientific notation.
         Ex:
                double d = 10e23;
                int i = 10e23;  ---> C.E possible loss of precision found : double
                                                                                      required : int.
Floating point literals can be specified only in decimal form. i.e we can’t use octal and hexa decimal representation for floating point literals.
        Ex:
              Double d = 0x123.456;
              C.E: Malformed floating-point literal.

Which of the following are valid declarations

No comments:

Post a Comment