- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Literals in Java programming
A literal is a source code representation of a fixed value. They are represented directly in the code without any computation.
Literals can be assigned to any primitive type variable. For example.
byte a = 68; char a = 'A';
byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or octal(base 8) number systems as well.
Prefix 0 is used to indicate octal, and prefix 0x indicates hexadecimal when using these number systems for literals. For example −
int decimal = 100; int octal = 0144; int hexa = 0x64;
String literals in Java are specified like they are in most other languages by enclosing a sequence of characters between a pair of double quotes. Examples of string literals are.
Example
"Hello World" "two
lines" ""This is in quotes""
String and char types of literals can contain any Unicode characters. For example.
char a = '\u0001'; String a = "\u0001";
Java language supports few special escape sequences for String and char literals as well. They are −
Sr.No. | Notation & Character represented |
---|---|
1 | Newline (0x0a) |
2 | \r Carriage return (0x0d) |
3 | \f Formfeed (0x0c) |
4 | \b Backspace (0x08) |
5 | \s Space (0x20) |
6 | \t tab |
7 | " Double quote |
8 | ' Single quote |
9 | \ backslash |
10 | \ ddd Octal character (ddd) |
11 | \uxxxx Hexadecimal UNICODE character (xxxx) |
- Related Articles
- Boolean Literals in Java
- What are literals in Java?
- What is the difference between character literals and string literals in Java?
- What are Boolean literals in Java?
- Using underscore in Numeric Literals in Java
- Define integer literals as octal values in Java
- Programming in Java.
- Integer literals vs Floating point literals in C#
- Stack in Java Programming
- Overloading in java programming
- Overriding in Java programming
- Java Program to Illustrate Use of Binary Literals
- Literals in C#
- Functional Interfaces in Java Programming
- PriorityQueue Class in Java Programming
