a peek into my MIND

April 2, 2014


Filed under: Java7Spec — Bharat Kondeti @ 12:07 am


A literal is a representation of a value of primitive type, string type or a literal type. A primitive literal can be a Integer literal / Float literal / Boolean literal / Character literal

– Integer literal can be decimal, hexadecimal, octal and binary
– Integer literal is of type long if it is suffixed with an ASCII letter L or l
– Underscores can be used as separators between integers
– Hexadecimal numerical starts with 0x or 0X followed by ASCII hexadecimal digits (0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,A,B,C,D,E,F)
– Octa numerical starts with 0 followed by ASCII digits 0 through 7
– Binary numerical starts with 0b or 0B followed by ASCII digits 0 or 1

// Decimal with underscore
 int decimal = 10_000_000;

// Hexa decimal
 int hexadecimal = 0X989_680;

// Octa decimal
 int octadecimal = 046113200;

// Binary
 int binary = 0B100110001001011010000000;

– Float literal can be decimal and hexadecimal
– Float literalĀ is of type float if it is suffixed with an ASCII letter F or f
– By default, a Float literal is of type double and optionally it can be suffixed with an ASCII letter D or d

– Boolean literal can be true or false

– Character literal is represented as a character or a escape sequence enclosed in a ASCII single quotes
– Character literal can only represent UTF-16 code’s whose values are in between ‘\u0000’ to ‘\uffff’

– A String literal is a reference to an instance of String class
– A string literal always refers to the same instance of the String class
– String literals defined with in the same class / defined with in different classes of the same package / defined with in different classes of different packages all reference to the same String object instance

public class Sample {
    public static void main(String[] args) {
        String ss1 = "samplestring";
        String ss2 = "samplestring";

        System.out.println( ss1==ss2); //true
        System.out.println( ss1=="samplestring"); //true
        System.out.println( Sample2.ss=="samplestring"); //true
class Sample2 {
    public static String ss = "samplestring";

– String literals computed by constant expressions are computed at compile time and thus refer to same String instance

String ss = "samplestring";
System.out.println(ss=="sample" + "string"); //true

– String literals computed at runtime by concatenation are newly created and thus reference to different String instance

String ss = "samplestring";
String sample = "sample";
String str = "string";

System.out.println(ss == sample + "string"); //false
System.out.println(ss == sample + str); //false

– intern() method on the computed string will refer to the same String object reference to a literal, if that literal instance is already created

String ss = "samplestring";
String sample = "sample";
String str = "string";

System.out.println(ss == (sample + "string").intern()); //true
System.out.println(ss == (sample + str).intern()); //true

– A null type has a value of null reference that is represented by Null literal

Create a free website or blog at WordPress.com.