0% found this document useful (0 votes)
49 views11 pages

Literals

The document discusses literals in Java. Literals are constants that represent fixed values like numbers, characters, strings, and boolean values. The key points are: - Literals can be used to initialize variables, in expressions, as method arguments/return values but not on their own as statements. - There are 7 types of literals in Java: integer, floating point, boolean, character, string, and null. - Literals have implicit data types determined by their value like int for integers and double for floating-point literals. - Integer literals can be decimal, hexadecimal, octal, and binary and Java converts them to decimal for internal use. Literals of other types are supported only for

Uploaded by

muralireddyzen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views11 pages

Literals

The document discusses literals in Java. Literals are constants that represent fixed values like numbers, characters, strings, and boolean values. The key points are: - Literals can be used to initialize variables, in expressions, as method arguments/return values but not on their own as statements. - There are 7 types of literals in Java: integer, floating point, boolean, character, string, and null. - Literals have implicit data types determined by their value like int for integers and double for floating-point literals. - Integer literals can be decimal, hexadecimal, octal, and binary and Java converts them to decimal for internal use. Literals of other types are supported only for

Uploaded by

muralireddyzen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Literals

What is a Literal?
Literal is a constant.
Ex: 10, 20.5, ‘a’, “zen”, true, etc.,

What is the use of a literal?


We can use literal
 To initialize variables,
 to specify values in the expression,
 to pass arguments to a method,
 to return a value from a method.

Where can we use a literal?


We can’t use a literal directly as an independent statement. If we use the compiler
produces a compilation error.
public class Demo {
public static void main(String[] args) {
12; // error: not a statement
}
}

We can use literal only

 In variable assignment,
 as an expression operand,
 as a method argument,
 as a return value.
Types of Literals:
Java supports 7 types of literals. They are

Note: According to SUN Microsystems, Class literals are not literals.

What is the data type of the above literals?


 Every variable and literal must have a data type.
For example:

 For a variable, we must specify the type by using a data type explicitly.
 But for every literal, an inbuilt data type is assigned by Java. That data type is known to
the compiler and JVM.
For example:
 10 is int type
 10.5 is double type
 ‘a’ is char type
Working with Integer Literals
 All integer literals are by default considered int type.
For example:
10, 20, 30, etc., are considered by the compiler and JVM as int type.

 If we want to treat them as byte or short or long type, we must use either assignment
operator(=) or cast operator or suffix letter.

 As per the Number System, we have 4 types of integers:


1. Binary Numbers
2. Octal Numbers
3. Decimal Numbers
4. Hexa-Decimal Numbers
1. Binary Numbers:
If a number starts with 0b or 0B, the compiler and JVM will treat that number as
a Binary Number.
2. Octal Number:
If a number starts with the prefix 0, then that number is considered as an Octal
Number by the compiler and JVM.
3. Decimal Number:
Numbers without any prefix are treated as decimal numbers by the compiler
and JVM.
4. Hexa-Decimal:

Hexa-Decimal numbers start with the prefix 0x or 0X.


Note: Binary numbers are introduced in Java 7v.
Ex:

 When we use binary, octal, hexa-decimal literals in program, they are converted to
decimal and they are stored in variable, used in validations, calculations and printed
Internally compiler converts Binary, Octal, Hexa-Decimal literals to decimal by using
their base value as shown below.
Note on Hexa-decimal number system:
Since 16 symbols are used, 0 to F, the notation is called Hexa-Decimal. The first 10
symbols are the same as in the decimal system, 0 to 9 and the remaining 6 letters are
taken from the first 6 letters of the alphabet sequence, A to F, where A represents 10, B is
11, C is 12, D is 13, E is 14 and F is 15.
 The numbers from all the number systems are by default are of int type. We can store
them in an int type variable. We can also store them in byte/short variable provided
they are in the range of byte/short variable. We can assign int binary, octal, hexa-
decimal to the int variable.
 We can represent decimal number as int, long, float and double.

 We can represent binary number as only int and long type. If we try to represent it
with float or double by suffixing F, D or .0, then the compiler will throw an error.
 Similar to decimal, We can also represent hexa-decimal numbers as int and long type
only.
 If we try to represent hexa-decimal as float or double by suffixing it with F, D,
or .0, we get compilation error.
 But we don’t get CE if we suffix F or D. Here, F or D will not represent the
number as float or double. F and D are in the range of hexa-decimal, they are
replaced with 15 and 13 respectively.

 Octal literal can be represented as int and long.


o We can suffix F, D and .0 to the octal number. We don’t get any CE. Here the
twist is the octal number is not converted into decimal. Instead, the octal
number is as it is used as floating-point number means 0123F is converted to
123.0.
 If we suffix F, D, or .0 to an octal Number, it is not considered as octal Number rather it
is consider as floating -point Number. Hence, with suffix character F, D and .O in an
octal Number we can use the digits 8 and 9. It means 012389F , 012389D , 012389.0
are correct. the result of these Number 12389.0.

o But suffix character ‘L’, we can't use 8,9 in an Octal Number. If we use compiler
throws an error because it is consider as octal Number.

 We cannot use L to a floating-point Number. The Compiler will throw an error, but we
can convert floating-point number to long by using cast operator.

 Binary, octal and hexa-decimal floating-point literals are not directly supported by Java.
In Java, they are typically used for integer values, not floating-point values. But we can
convert binary, octal and hexa-decimal numbers to floating-point numbers by using
cast operator.
 We can assign and store binary, octal, decimal and hexa-decimal numbers in all
primitive data type variables except boolean data type.
 The default datatype of these literals is int.

 Here, the rule is the literal assigned to a variable must be in the range of variable. If
not, we get CE.
Primitive Data Type Conversion:
The process of changing one type of value to another type of value is called Type
Conversion.
We develop type conversion by assigning a value of one variable to a variable of another data
type.
For example:

We have two types of conversions. They are:


1. Implicit type conversion / Automatic type conversion / Widening
2. Explicit type conversion / Casting / Narrowing

You might also like