Monday, 12 November 2018

Java Literals | Literals in Java | What is literals in java and their types - Online Help


Guyz in this post we are going to see Java literals i.e literals in Java and their types. Literals are a sequence of characters that represent constant values. In java language, it has five type of literals as follow:

1. Integer Literals
2. Floating point Literals
3. Character Literals
4. String Literals
5. Boolean Literals



Integer Literals:

  Integer literals are the primary literals used in java programming. They come in a few different formats:


Decimal(Base 10)

Decimal literals appear as ordinary numbers with no special notation
For example, an integer literal for the decimal number is 12 is represented in Java as 12 in decimal.


Hexadecimal(Base 16) : 

It appears with leading 0x or 0X
For example, an integer literal for decimal numbers 12 is represented in Java as 12 in decimal.


Octal(Base 8):

 It appears with a leading 0 front of the digits.
For example, an integer literal for the decimal number 12 is represented in Java as 014 as octal.

Sample program:

class octalliterals
{
public static void main(string args[])
{
int nine=011;
int ten=012;
int eleven=013;
int twelve=014;
system.out.println("octal 011="+nine);
system.out.println("octal 012="+ten);
system.out.println("octal 013="+eleven);
system.out.println("octal 014="+twelve);
}
}  

output is:

octal 011=9
octal 012=10
octal 013=11
octal 014=12



Floating point literals:


Floating point literals represent decimal numbers with fractional parts, such as 3.142. They can be express in either standard or specific notation, meaning that the number 563.84 also can be expressed as 5.6384e2.


Character literals:


      Character literal represents a single Unicode character and appears within a pair of a single quotation mark.
                                   Example:           'A' '5'     'p'


Boolean Literal:

      In C, there is no boolean type, and therefore no boolean literals. The boolean value true and false or represented by the integer values 1 and 0.Java fixes the problem by providing a boolean type with two possible states: True and false.


String literals:

      String literals represent multiple characters and appear within a pair of double quotation marks. String literals are implemented in Java by the string class.
     
    When java encounters a string literal, it creates an instance of the string class and sets its state to the characters appearing within the double quotation marks.

For example:
                       "rollno" "john" "1234".

No comments:

Post a Comment