3 Data Types Variables Constants
3 Data Types Variables Constants
CONSTANTS IN JAVA
LEARNING INTENTION
• The purpose of learning to code is to instruct a computer to
perform a specific task that is very tedious and time
consuming when it is done manually. Java programming
language was chosen to learn basic coding skills because,
• It is easy to learn and get an idea about an Object Oriented
Programming language
• It is a platform independent language - a code written in one
platform can run in other platforms without many
modifications to the original source code.
• It is used for developing all types of cross-platform Desktop
applications, Apps for Mobiles and Dynamic web
applications
LEARNING OBJECTIVES
• To declare variables and constants
appropriately using the concept of Data
types
• To identify the purpose of constants
• To identify the purpose of comments and its
usefulness in documenting programs
Success criteria
Students should be able to:
• identify the application areas of Java
• compare different types of programming languages and their
compilation processes
• understand the compilation process in Java
• understand the structure of a Java program and know the basic rules
that need to be followed to write a program in Java
• identify the building blocks (tokens) in a Java program and use them
appropriately in a program
• declare variables and constants appropriately using the concept of Data
types
• identify the purpose of constants
• identify the purpose of comments and its usefulness in documenting
programs
• create simple Java programs, compile them and execute the code
Data Types
• Data that can be handled by a computer can
be of many types such as Character, Numeric,
String etc.
• Suffixes are f or F , d or D, l or L.
Suffixes Examples
• float pi=3.14F;
• Suffix is required because the compiler treats
3.14 as double literal
• long num=8999999999999999999L;
• Suffix is required because the compiler treats
899999999999999999L as integer literal
SPECIFYING LITERALS IN A JAVA PROGRAM
• Integer Literals are whole numbers without
any fractional part.
– It must have at least one digit.
– Must not contain any decimal point or commas
– It may contain either + or – sign. If there is no sign
it is assumed to be positive.
– Must not be enclosed in single or double quotes.
• If the suffix l or L is specified in the end, it
is considered as a long literal.
Valid Literals: +45, -56, 12/7, 1234
Invalid Integer Literals: “23”, 25,990, 25.78
• Floating Literals are also called real literals.
– Must have at least one digit before the decimal
point and one digit after the decimal point.
– It may have either a + or – sign preceding it. A real
literal with no sign is assumed to be positive.
– Must not be enclosed in single or double quotes.