0% found this document useful (0 votes)
39 views

3 Data Types Variables Constants

This document discusses data types, variables, literals and constants in Java programming. It describes the different primitive and reference data types in Java including integral, fractional, character, boolean and reference types. It explains how to declare and initialize variables and constants in Java and the purpose of using constants. It also covers literals in Java including integer, floating point, boolean, character and string literals and provides examples of valid and invalid literals.

Uploaded by

Aakriti Bansal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

3 Data Types Variables Constants

This document discusses data types, variables, literals and constants in Java programming. It describes the different primitive and reference data types in Java including integral, fractional, character, boolean and reference types. It explains how to declare and initialize variables and constants in Java and the purpose of using constants. It also covers literals in Java including integer, floating point, boolean, character and string literals and provides examples of valid and invalid literals.

Uploaded by

Aakriti Bansal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

DATA TYPES, VARIABLES, LITERALS AND

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.

• Java classifies data types as follows:


– Primitive or in-built data type
– Reference data type
Primitive Data Types
• Primitive data types (also known as the
fundamental data types) are the inbuilt data
types offered by Java.
• Java provides 8 primitive data types which are
categorized as follows:
– Integral
• byte, short, int, long
– Fractional
• float, double
– Character
• char
– Boolean
• boolean
Integral Data Type
TYPE SIZE RANGE
byte 1 byte -128 to +127
short 2 bytes -32768 to +32767
int 4 bytes -2 billion to +2 billion
i.e., -231 to +231 – 1
long 8 bytes -263 to +263 -1

Note: 1 byte is 8 bits


Fractional or Floating point type
TYPE SIZE RANGE REMARKS
float 32 bits (4 bytes) -3.4 E + 38 To Precision up
+3.4 E+38 to 6 digits
Single
Precision
double 64 bits (8 bytes) -1.7 E + 308 Precision up
to 1.7 E + 308 to 15 digits
Double
Precision
Character Data type
TYPE SIZE RANGE REMARKS
char 16 bits (2 bytes) 0 to 65535 Unicode
characters

Boolean Data type


TYPE SIZE RANGE
boolean 1 byte true or false
Reference Types
• Inbuilt reference types
– Arrays
– Strings

• User-defined reference types (also known as


user-defined objects or user-defined data
types)
Variables
• A variable is a named storage location in the
computer’s memory.

• It can hold only one value of a particular data


type.

• The value in a variable can be manipulated


during the execution of the program.
Declaration of a variable
• <type> <variable name>
• Examples
– int x;
– double y;
– float m;
– char ch;
– byte n;
– short x;
– long k;
– boolean b;
• Note:
• In Java, a variable can be declared anywhere, provided it is
declared before it is used.
• Every variable in a program must have a value before it is used in
an expression.
• The above variables are only declared and it does not store any
initial value.
Initialization of a variable
• At the time of declaration of a variable, an
initial value may be specified which can be a
literal or another variable as shown below.
int x=3;
int y=5;
int z=y;
char ch=‘c’;
String nam=“tom”;
double d=4.5667;
Constants
• A constant is a variable whose value cannot
be changed once it has been assigned a
value. If the value is changed subsequently in
the program, the compiler gives an error.
• The keyword final makes a variable as
constant.
• final double pi=3.14;
• final double tax=0.2;
Advantages of Constants
• Indicates to the programmer that the value in
that constant (variable) cannot be changed and
hence makes it easier to read and check for
correctness. Even if the programmer accidentally
changes the value of that constant in the code,
the compiler gives an error.
• If the value in the constant needs to be changed,
only the declaration needs to be modified. On
the other hand if a literal is used, one has to
search through the entire program for every
occurrence of that specific value and then
change all the occurrences.
Suffixes
• Suffixes are used to explicitly specify the data type of a
literal data value.
• In some situations it is required, because the compiler
gives an error, when the value that is assigned to a
variable is beyond the capacity of the variable on the left
hand side.

• float pi=3.14; // results in error because java assumes the


fractional number to be of double data type and gives the
error, “possible loss of precision”

• long num=8999999999999999999; // results in error


because java assumes the number to be of type int
and gives the error, “integer number too large”

• 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.

If the suffix f or F is specified in the end, it is


considered as a single precision floating literal.
If there is no suffix it is considered as a double
precision floating literal

Valid Float Literals: 2.0f, 17.5f, -13.0F, -0.00625F


Invalid Float Literals: 17,245.78, 75, “45.67”
• Boolean Literals are true and false.
– Must not be enclosed in single quotes or double
quotes.
– Must be specified in lower case only as shown
below.

• Valid Boolean literals: true, false

• Invalid Boolean literals: True, False, TRUE,


FALSE
• A character Literal is one character enclosed
in single quotes.
• Valid Character literals: ‘a’, ‘#’ , ‘ ‘, ‘5’
• Invalid Character literals : “a”, “#”

• Note: There are some characters that cannot


be typed directly from keyboard. These are
called Escape sequences and they will be
discussed later.
• A String Literal is a sequence of zero or more
characters surrounded by double quotes.
• Valid String Literals: “ABC”, “K”, “234”, “A!@”,
“V123”
• Invalid literals: ‘abc’, ‘J’

• Null Literal: It is specified as null or as open


and close quotes.
• String s=null;
• or
• String s=“”;
Exercise-Categorize the following data
types as per their respective
categories
• long
• double
• float
• byte
• int
• char
• boolean
• short
Exercise-Identify the errors if any.
• long- 4 bytes
• double-6 bytes
• float-3 bytes
• byte-8 bits
• int- 2 bytes
• char- 2 bytes
• boolean- 1 byte
• short-4 bytes
Test your understanding

• Give the output of the following:


1. System.out.println(3/5);
2. float x= 11/5;
System.out.println(x);
3. System.out.println(22/7);
• What changes you would make to the above
statement to get a more precise answer?
END

You might also like