Literals
- A Java literal is a sequence of characters (digit, letters and other characters) that represent constant value to be stored in variable.
- A literal is the source code representation of a fixed value.
Java Specifies 5 major types of literals :
Integer Literals
-
Floating-Point Literals
-
Character Literals
-
String Literals
-
Boolean Literals
Integer Literals :
- An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int.
- Values of the integral types byte, short, int, and long can be created from int literals.
-
There are four ways to represent integer number in the Java language :
Decimal ( base 10 )
int x = 20;
-
Octal ( base 8 )
int x = 076; (starts with 0)
-
Hexadecimal ( base 16 )
int x = 0x13A; (starts with 0x)
-
Binary (base 2 )
int x = 0b101; (starts with 0b)
Float-Point Literals :
-
Floating-Point numbers are defined as a number, a decimal symbol, and one or more numbers representing the fraction.
For Example:
double d = 223.56; -
Floating-points literals are defined as double (64 bits) by default, so if you want to assign a floating-point literal to a variable of type float
( 32 bits),you must attach the suffix F or f to the number;
For Example:
float f = 223.56F;
Character Literals :
-
A character literal is represented by a single character in single quotes.
For Example:
char a = 'a' ;
char c = '@' ; -
You can also type in the Unicode value of the character, using the Unicode notation of
prefixing the value with \u.
For Example:
char ch = '\u0041' ; //letter 'A'
String Literals :
-
In Java, string literals are enclosed within double quotes.
For Example:
String s = "Java Prowess" ; -
String literals are not only a sequence of character as in 'C', these are objects of type java.lang.String .
For Example:
String s = "Java Prowess" ;
Boolean Literals :
- Boolean literals are the source code representation for boolean value. A boolean value can only be defined as true or false.
-
Although in 'C' (and some other languages) it is common to use number to represent true
or false, this will not work in Java.
 boolean b = true ;//Legal
 boolean b = 1 ;//Compiler Error
Next topic is Java-Constants
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in