Literals in Java Simplified

Literals in Java

Java is a language made of keywords,literals rules. This post talks about literals in java and Characters in Java,Hexadecimal literals in java,Octal literals in Java.

In general when we insert a literal value into a program the compiler knows what type to make it.However if the type is ambiguous,we need to provide compiler some more information about it and guide it in the form of characters associated with the literal value.

Unicode and Character Escape

Java characters, string and identifiers (like variables,methods, and class names) are of 16 bit unicode characters. It makes the Java more powerful for becoming internationalize language.The unicode Character set is compatible with ASCII and first 256 characters (0x0000 to 0x00FF) are identical to ISO8859-1(Latin) characters.

If we are using a Java 16 bit character, we can not distinguish them from 8-bit character set. No platforms can display all 38885 characters currently identified as Unicode characters hence Java code may be written with special unicode escape sequence. In Java unicode characters may be represented with Unicode escape sequence \UXXXX where XXXX is a sequence of four hexadecimal digits.

C escape sequence in Java

Java supports almost all C’s escape sequence like \n,\t and \xxx(xxx is three octal digits). Java does not support line continuation with \ at the end rather Java provides ‘+’ concatenation operator for string continuation. So a long String should be either kept in the same line or divided and concatenated with a ‘+’ operator.

READ  Class CharArrayWriter in Java


Hexadecimal literals in Java:

Hexadecimal(base-16) is denoted by leading ox or OX followed by 0 to 9 and a-f either in upper case or lowercase Exp-ox7fff

Octal literals in Java:

Octal (base-8) is denoted by a leading zero and digits from 0 to 7.

Binary literals in Java:

Binary has no representation except 0 and 1.

A training character after a literal is its l/L stand for long,D/d stand for double,f/F stands for float.If we try to initialize a variable with a value bigger than it can hold,the compiler will provide an error message or it will automatically makes a bigger datatype and ask us to narrow it for assignment. To convert a lower capacity data type to higher capacity is called promotion.
In an operation,where two different types of data are present , the resultant automatically get promoted to max of the two data type.

Java characters are enclosed in a single quotes(”). So a starting and ending determines the characters in java.Java supports an alphanumeric character set called unicode.

Blank is represented as ‘ ‘ and it is still valid character. A String must be typed in one line.

"I love

is not a valid string.JVM will throw an error on line-1 stating Quote Expected.

Mostly in java identifiers starting with underscore(_) carry a different meaning and so as it can not start with dollar($) sign. it is always better to start with letter.

Java String API makes characters representation very transparent to the user.

Difference between Unicode Escape and C style escape?

There are two important difference between Unicode escape and c Style escape characters. They are as follows:

  1. Unicode escapes can appear anywhere within a Java program while the other escape characters can appear only in characters and String constants.
  2. Unicode \U escape sequence are processed before the other escape characters thus two types of escape sequence can have very different semantics. So an unicode escape is an alternative way to represent a character that may not displayed on non unicode system.
READ  Abstract Interface Observer in Java

some special characters represent in such a way that prevents the usual interpretation of those characters by the compiler like ‘\u0022 and \U005C are unicode escape characters for double quote and backslash character respectively. \” represents a ” character.

If a String consists of a double quote then String quote =”\””( We can not represent the same with Single unicode escape).Single unicode character escape -\U0022 also does the same thing. Both put a double quote while compiled.

Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *