Literals in Java

 

Literals are numbers, characters, and string representations in your program. Literals can be classified as numeric literals, character literals and string literals.

 

Numeric literals

Numeric literals consist of digits with an optional sign and an optional decimal point. Numeric literals that contain a decimal point are by default double. Those prefixed with a 0x indicate the number is a hexadecimal number (base 16). Those that begin with a 0 are octal numbers (base 8). But usually they are base 10.

Integer literals are of type int by default. A suffix of f or F can be used to declare a floating point literal as of type float. Floating point literals are numbers that contain a decimal point or those that are written using scientific notation. E.g. 7.77 is double, 7e5 is double and 0.077F is float. A literal can be specified as type long by appending an L to the end of the literal. Either a lowercase or uppercase L can be used to designate an integer as type long. However, it is better to use an uppercase L to avoid confusing the letter with the numeric digit "1" (one).

Java 7 added the ability to uses underscore characters (_) in numeric literals. The use of the underscore is to make the code more readable to the developer but it is ignored by the compiler.

long ssn = 111_22_3333L;

System.out.println(ssn);

The output will be:

111223333

Consecutive underscores are treated as one and also ignored by the compiler. Also, underscores cannot be placed:

  • At the beginning or end of a number
  • Adjacent to a decimal point
  • Prior to the D, F, or L suffix

 

Character literals

Character literals are single characters enclosed in single quotes.

char letter = 'a';

letter = 'F';

Escape characters (also called escape sequences or escape codes) in general are used to signal an alternative interpretation of a series of characters. In Java, a character preceded by a backslash (\) is an escape sequence and has special meaning to the java compiler. For example, '\n' represents the carriage return line feed character. These special escape sequences represent certain special values. You can use escape sequences in character literals as well as in string literals. Read more about escape sequences at http://www.javajee.com/escape-sequences-in-java-with-examples.

 

String literals

String literals are a sequence of characters that are enclosed in a set of double quotes. String literals cannot be split across two lines.

String message = "Hello\tWorld\n";

 

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)