LEXICAL ELEMENTS OF JAVA LANGUAGE
a) Literals
A literal is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are
literals. Java supports five types of literals:
Integer literals
Floating-point literals
Character literals
String literals
Boolean literals
• Integer Literals: - An integer literal can be assigned to an integer variable as long as it can fit into the
variable. A compilation error would occur if the literal is too large for the variable to hold.
• Floating-Point Literals: - Floating-point literals are written with a decimal point. By default, a floating-
point literal is treated as a double type value. You can make a number a float by appending the letter f or F,
and make a number a double by appending the letter d or D. E.g. you can use 100.2f or 100.2F for a float
number.
• Scientific Notation: - Floating-point literals can also be specified in scientific notation, for example,
1.23456e+2(1.23456e2), is equivalent to 123.456, and 1.23456e-2 is equivalent to 0.0123456. E (or e)
represents an exponent and it can be either in lowercase or uppercase.
b) Identifiers
They are programmer-designed tokens, and used for naming classes, methods, variables, objects, labels,
packages and interfaces in a program. It follows the following rules and conventions
They can have alphabets, digits, underscore and dollar sign characters.
They must not begin with digit.
Uppercase and Lowercase letters are distinct.
They can be of any length.
Names of methods and instance variables start with lowercase letter. When more than one word is
used in a name, the second word is marked with uppercase letter.
All classes and interface start with uppercase letter.
Identifiers that represent constant values use all uppercase letters and underscores between words.
Should be unique within a given area (Scope).
c) Variables
Memory location used to store data during program execution and data in the memory may change with time.
An identifier used to refer to the memory location and as convention, use descriptive identifiers
Declaring Variables
• All variables must be declared before they are used i.e. Java is strongly typed language, meaning that data
type of the variable must be stated.
• General format for declaring a variable:
data_type identifier;
• Multiple variables of same type can be declared together.
• int and double variables initially are given a value of zero unless they are initialized to a value.
d) Data types
A category or set of data values. Many languages have a notion of data types and require a programmer to
specify what type of data is being manipulated.
Object Oriented Programming 1
, Java's primitive types
This are Java's built-in simple data types for numbers, text characters, and logic. Java has eight primitive types.
Types that are not primitive are called object types.
Java Data Types
primitive Objects
(reference)
Integral Boolean Floating point
String class array
float double
byte char short int long
Integer Primitive Type Variable
This are declared using the “int” keyword and are allocated 4 bytes. The range of values is from -2147483648
to 2147483647. Example: int aNum;
Floating Point Primitive Types
This are declared using “float” keyword and are allocated 4 bytes. The range of values is roughly ±1.4 x 10-38 to
±3.4 x 1038 to 7 significant digits.
Double Primitive Type Variable
There are declared using “double” keyword and allocated 8 bytes. The range of values is roughly ±4.9 x 10-308
to ±1.7 x 10308 to 15 significant digits.
More Integer Primitive Types
Other primitive types: byte, short and long:
For byte, from -128 to 127, inclusive (1 byte).
For short, from -32768 to 32767, inclusive (2 bytes).
For long, from -9223372036854775808 to 9223372036854775807, inclusive (8 bytes).
Characters
A char variable stores a single character from the Unicode character set. A character set is an ordered list of
characters, and each character corresponds to a unique number. Character literals are delimited by single quotes
A Boolean Primitive Type Variable
This are declared using “boolean” keyword. It can store either true or false values. Example boolean aFlag;
String
String’s are not primitive data types, but are Objects and can be declared in the same way as a primitive type
using the keyword: String. The String class has several methods that are useful for manipulating strings
Object Oriented Programming 2
a) Literals
A literal is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are
literals. Java supports five types of literals:
Integer literals
Floating-point literals
Character literals
String literals
Boolean literals
• Integer Literals: - An integer literal can be assigned to an integer variable as long as it can fit into the
variable. A compilation error would occur if the literal is too large for the variable to hold.
• Floating-Point Literals: - Floating-point literals are written with a decimal point. By default, a floating-
point literal is treated as a double type value. You can make a number a float by appending the letter f or F,
and make a number a double by appending the letter d or D. E.g. you can use 100.2f or 100.2F for a float
number.
• Scientific Notation: - Floating-point literals can also be specified in scientific notation, for example,
1.23456e+2(1.23456e2), is equivalent to 123.456, and 1.23456e-2 is equivalent to 0.0123456. E (or e)
represents an exponent and it can be either in lowercase or uppercase.
b) Identifiers
They are programmer-designed tokens, and used for naming classes, methods, variables, objects, labels,
packages and interfaces in a program. It follows the following rules and conventions
They can have alphabets, digits, underscore and dollar sign characters.
They must not begin with digit.
Uppercase and Lowercase letters are distinct.
They can be of any length.
Names of methods and instance variables start with lowercase letter. When more than one word is
used in a name, the second word is marked with uppercase letter.
All classes and interface start with uppercase letter.
Identifiers that represent constant values use all uppercase letters and underscores between words.
Should be unique within a given area (Scope).
c) Variables
Memory location used to store data during program execution and data in the memory may change with time.
An identifier used to refer to the memory location and as convention, use descriptive identifiers
Declaring Variables
• All variables must be declared before they are used i.e. Java is strongly typed language, meaning that data
type of the variable must be stated.
• General format for declaring a variable:
data_type identifier;
• Multiple variables of same type can be declared together.
• int and double variables initially are given a value of zero unless they are initialized to a value.
d) Data types
A category or set of data values. Many languages have a notion of data types and require a programmer to
specify what type of data is being manipulated.
Object Oriented Programming 1
, Java's primitive types
This are Java's built-in simple data types for numbers, text characters, and logic. Java has eight primitive types.
Types that are not primitive are called object types.
Java Data Types
primitive Objects
(reference)
Integral Boolean Floating point
String class array
float double
byte char short int long
Integer Primitive Type Variable
This are declared using the “int” keyword and are allocated 4 bytes. The range of values is from -2147483648
to 2147483647. Example: int aNum;
Floating Point Primitive Types
This are declared using “float” keyword and are allocated 4 bytes. The range of values is roughly ±1.4 x 10-38 to
±3.4 x 1038 to 7 significant digits.
Double Primitive Type Variable
There are declared using “double” keyword and allocated 8 bytes. The range of values is roughly ±4.9 x 10-308
to ±1.7 x 10308 to 15 significant digits.
More Integer Primitive Types
Other primitive types: byte, short and long:
For byte, from -128 to 127, inclusive (1 byte).
For short, from -32768 to 32767, inclusive (2 bytes).
For long, from -9223372036854775808 to 9223372036854775807, inclusive (8 bytes).
Characters
A char variable stores a single character from the Unicode character set. A character set is an ordered list of
characters, and each character corresponds to a unique number. Character literals are delimited by single quotes
A Boolean Primitive Type Variable
This are declared using “boolean” keyword. It can store either true or false values. Example boolean aFlag;
String
String’s are not primitive data types, but are Objects and can be declared in the same way as a primitive type
using the keyword: String. The String class has several methods that are useful for manipulating strings
Object Oriented Programming 2