Chapter 4: Constants, Variables, and Data Types
4.1 Constants
Constants refer to fixed values that do not change during the execution of program.
JAVA CONSTANTS
Numeric Constants Character Constants
Integer Real Character String
Constants Constants Constants Constants
4.1.1 Integer Constants
An integer constant refers to a sequence of digits.
3 types: Decimal Integer, Octal Integer and Hexadecimal Integer
Decimal Integer
o Consist of a set of digits, 0 through 9, preceded by an optional minus sign. Eg: 123, -321,
0, 654321
o Embedded spaces, commas, and non-digit characters are not permitted between digits.
Eg: 15 750; 20,000 ; $1000
Octal Integer Constants
o Consists of any combination of digits from the set 0 through 7, with a leading O
Eg: O37, O551
Hexadecimal Integer
o A sequence of digits preceded by Ox or OX
o Also include alphabets A through F or a through f
o Letter A through F represents the numbers 10 through 15
Eg: OX2, OX9F
4.1.2 Real Constants
The quantities represented by numbers containing fractional parts are called Real constants.
Eg: 0.0083 , -0.75
Are shown in decimal notation, having a whole number followed by a decimal point and the
fractional part.
Possible that the number may not have digits before the decimal point or digits after the
decimal point.
Eg: .95 , -.71
May also be expressed in exponential notation.
General form is:
Mantissa e exponent
o Mantissa is either a real number expressed in decimal notation or an integer
o Exponent is an integer with an optional plus or minus sign
, o The letter ‘e’ separating the mantissa and the exponent and can be written either in
lowercase or uppercase.
4.1.3 Single Character Constants
Contains a single character enclosed within a pair of single quote marks.
Eg: ‘K’
4.1.4 String Constants
Is a sequence of characters enclosed between double quotes.
Eg: “Hello Java”
4.2 Variables
A variable is an identifier that denotes a storage location used to store a data value.
Unlike constants that remain unchanged during the execution of a program, a variable may take
different values at different times during the execution of the program.
A variable name can be chosen by the programmer in a meaningful way to reflect what it
represents in the program.
Variable names may consist of alphabets, digits, the underscore, and dollar characters
o Must not begin with a digit
o Uppercase and lowercase are distinct.
o Should not be a keyword.
o White space is not allowed.
o Variable names can be of any length
4.3 Data Types
4.3.1 Integer Types
Can hold only whole numbers
Supports 4 types: byte, short, int and long
Size and range of Integer Types
4.1 Constants
Constants refer to fixed values that do not change during the execution of program.
JAVA CONSTANTS
Numeric Constants Character Constants
Integer Real Character String
Constants Constants Constants Constants
4.1.1 Integer Constants
An integer constant refers to a sequence of digits.
3 types: Decimal Integer, Octal Integer and Hexadecimal Integer
Decimal Integer
o Consist of a set of digits, 0 through 9, preceded by an optional minus sign. Eg: 123, -321,
0, 654321
o Embedded spaces, commas, and non-digit characters are not permitted between digits.
Eg: 15 750; 20,000 ; $1000
Octal Integer Constants
o Consists of any combination of digits from the set 0 through 7, with a leading O
Eg: O37, O551
Hexadecimal Integer
o A sequence of digits preceded by Ox or OX
o Also include alphabets A through F or a through f
o Letter A through F represents the numbers 10 through 15
Eg: OX2, OX9F
4.1.2 Real Constants
The quantities represented by numbers containing fractional parts are called Real constants.
Eg: 0.0083 , -0.75
Are shown in decimal notation, having a whole number followed by a decimal point and the
fractional part.
Possible that the number may not have digits before the decimal point or digits after the
decimal point.
Eg: .95 , -.71
May also be expressed in exponential notation.
General form is:
Mantissa e exponent
o Mantissa is either a real number expressed in decimal notation or an integer
o Exponent is an integer with an optional plus or minus sign
, o The letter ‘e’ separating the mantissa and the exponent and can be written either in
lowercase or uppercase.
4.1.3 Single Character Constants
Contains a single character enclosed within a pair of single quote marks.
Eg: ‘K’
4.1.4 String Constants
Is a sequence of characters enclosed between double quotes.
Eg: “Hello Java”
4.2 Variables
A variable is an identifier that denotes a storage location used to store a data value.
Unlike constants that remain unchanged during the execution of a program, a variable may take
different values at different times during the execution of the program.
A variable name can be chosen by the programmer in a meaningful way to reflect what it
represents in the program.
Variable names may consist of alphabets, digits, the underscore, and dollar characters
o Must not begin with a digit
o Uppercase and lowercase are distinct.
o Should not be a keyword.
o White space is not allowed.
o Variable names can be of any length
4.3 Data Types
4.3.1 Integer Types
Can hold only whole numbers
Supports 4 types: byte, short, int and long
Size and range of Integer Types