Literal representation:

Literals (Constant values)
(refer data types concept before proceeding this..)
Integer Literals
  Integer literals can be denoted in the following ways:
  1. Decimal representation(base 10)
  2. Octal representation(base 8)
  3. HexaDecimal representation(base 16)
  4. Binary representation(base 2)--->added in  java 7
Q) What is the output of the following code?
class MyClass5{
  public static void main(String  args[]){
  int b=09;
  System.out.println(b);
  }
}
Output: Compile time error
Reason: Given number is in octal form, whose digits range is in between 0 to 7.
Examples:
1,10,23,45,345,43232,….à All these are in decimal form.
01,03,012,0345,02436,….àAll these are in Octal form.
0x1,0x22,0x324,0xA213,0x123F,….à All these are in hexadecimal form

0b0000,0b1101,0b10101,0b11110,….à All these are in binary form

Floating-Point Literals
Floating-point numbers represent decimal values with a fractional component.
They can be expressed in either standard or scientific notation
Examples:
  2.3,3.456,34.098,….à Standard form
  1.4e+2,3.143e-5,4.567e+3,….àScientific form (exponent part will be here)

Q) In Java, the floating-point literals default to ______ precision?
Ans: double
  So,
  3.14---> (What type of value it is?)
Ans: double
But, I want to represent that value as float type. What will you do?
Ans: Append f or F at the end of value. (3.14f)

Boolean Literals
Q) Let me know the boolean type values again.What are they?
Ans: true and false
Q) Is 0 equal to false?
Ans: No
Q) Is 1 equal to true?
Ans: No
Note: The values of true and false do not convert into any numerical representation.

Character Literals
Q) Recollect, Java follows ______ standard set to represent characters?
Ans: UNICODE character set
Q) Number of bits for a character literal?
Ans: 16 bits
Q) How you write character literals?
Ans: by enclosing in single quotes.
Q) Can I consider the escape sequences as characters?
Ans: Yes ( these are also characters but we can not represent them directly )
Q) Can you tell me some escape characters?
Ans: 
         \n (new line character),
         \t (tab),
         \r (carriage return),
        \' (single quote),
       \" (double quote),
       \f (form feed),
      \XXX (octal character XXX),
      \XXXX (Hexadecimal character XXXX)

Note:
In java, we can also represent a character literal in the following ways:
  -Octal representation
  -HexaDecimal representation
Examples:
§‘\121’,  ’\122’,  ’\456’,….à Octal  (backslash followed by the three digit number).
§‘\u123’,   ‘\u12d4’,   ‘\uf435b’,…àHexaDecimal  (a backslash-u  followed by exactly four hexadecimal digits)


String Literals
Q) How you write string literals?
Ans: by enclosing the values in double quotes.
Note: In java, String literals are objects.
  (We will see about this statement later)

Exercise:
Q) Write a program to display a message like: 
            Hello Sachin
                            Tendulkar    How’z  ur health ?
Q) Write a program to display messages like:
            “ill health”
                             “Move to C\VVB\CSE\Welcome.java”
Q) What is the output of the following code snippet?
class MyClass5{
  public static void main(String  args[]){
  System.out.println("vijaya\rhello");
  System.out.println("Hello vijayabhaskara\rreddy\bhow do you do?");
  }
}
Output:__________ 

Comments

Popular posts from this blog

Type Conversion and Casting

Data types: