Literals are source code representation of a FIXED value. This means that you can use a literal, well, literally. What the previous statement means is that when a Java literal is used, no further computations are performed. A literal is a value that can be directly assigned to a variable of the corresponding type. A literal can also be used as parameters to pass values to Java methods. Finally, literals can also be used as constants either when used standalone (without ever assigning them to a variable) or by assigning them to public static final variables.
Here are a few examples –
1 2 3 4 5 6 7 8 | int christmasDay = 25; String biggestThief = "Politicians"; float approximateMoneyStolen = 1760000000000; // yeah, count that. // it is approximate and so float can be used // BTW, that figure is from a real scam |
Integer Literals
A Java integer literal is either of type long or int. A long literal has ‘L’ or a ‘l’ at the end of the value. All integer literals not having L or l at the end are of type int. Note that ‘L’ is preferred over ‘l’. This is for code readability, otherwise they are the same for Java compiler.
123456 | // 1939 is an int literal 1939 // exact amount stolen by a politician 17600000000000L |
An integer literal can be expressed in one the three supported number systems – decimal, octal and hex. An octal literal is prepended by a 0 and a hexadecimal by 0x.
123456789 | 031 ; // 0 in the beginning implies octal 0x25 ; // 0x implies hexadecimal System.out.println( 031 == 0x25 ); // Yeah, Oct 31 is same Dec 25 // Haloween and Christmas are related after all |
Remember that short and byte are also integer data types. So an integer literal can be assigned to variables which are of one of these primitive types – int, long, short, byte. Of course, integer literals can also be asssigned to Integer, Long, Short and Byte.
Java Floating Point Literals
A floating point literal defaults double precision. If you intend to use single precision, specify F or f at the end. A double precision can be specified by using D or d at the end. You can also use the e or E for scientific notation .
12345678910111213 | // following are valid float literals 3 .14f 3 .14F 3 .14d 3 .14D 3 .14e0 3 .14E0 |
Java Boolean Literals
A boolean literal can be either true or false. The values true and false are not reserved keywords but are boolean literals. They can be assigned to a variable of type boolean or Boolean.
Java Character and String Literals
A character literal consists of unicode character enclosed in a single quote. A unicode value is indicated by prepending \u.
12345 | 'P' ; // a char literal being assigned to char variable char last = '\uffff' ; |
A character literal can be assigned to a variable of type char or Character.
A String literal is a collection of characters inside double quotes.
1 | String bestSite = "CodingRaptor.com" ; |
Java Class Literal
A class literal is a value created by appending ‘.class’ to the end of type name. It represents the class object in the method area of JVM.
1234567 | Math.class // A class literal System.out.println (Class.forName(“CodingRaptor.com”) == String.class); // prints true// because the Class object of // “CodingRaptor.com” is String |