Data Types and Literals
Data Types and Literals
Variables
Rakib Mahmud
Introduction
• Data is the important part of a program.
• All the processing we do upon data.
• When the program is running in the memory, program
hold/store the data temporarily in the memory.
• The data is stored in the variables.
• Variables have some data types.
Data types in Java
• Primitive data types: means the basic/ built in data
types in java
• integral : They can have any numeric value without decimal
point
• byte, short, int, long
• floating Point: They can have any numeric value with decimal
point
• float, double
• char: This is just for storing character.
• boolean: This is for storing true and false.
• Non-primitive data types: These are created by the
programmer for example: classes, arrays.
Data types in Java (Cont’d)
Type Size Range Default
byte 1 -128 to 127 0
short 2 -32768 to 32767 0
int 4 -231 to 231-1 0
long 8 -263 to 263-1 0L
float 4 ±1.4×10−45 to ±3.4×1038 0.0f
double 8 ±4.9×10−324 to ±1.798×10308 0.0d
char 2 (supports unicode) 0 to 65535 \u0000
boolean ? (minimum 1 bit, True/false false
maximum 1 byte)
Data types in Java (Cont’d)
• Java has defined classes around these primitive data
type.
• For every primitive data type there is a class available.
• int – Integer, float- Float, double – Double, char -Character,
long – Long, byte - Byte
• Those classes contain information about:
• The range of the primitive data type.
• The size or the bytes it takes etc.
To find out write
javap
java.lang.class_name
Data types in Java (Cont’d)
Variables
• Variables are used for storing data.
• Declaring a variable:
data_type variable_name
• We can initialize the variable while declaring it.
data_type variable_name = value
local variables (inside methods) don’t get default values. It must be assigned
before use
• Depending upon the values, data type should be
selected.
• We can also change the value of variable.
Variables – Naming Rules
• Case Sensitive
• Contains Alphabets, Numbers, _ or $
• Starts with Alphabets, _ or $
• Should not be a keyword. For example: if, else, switch,
for etc.
• Should not be a class name, if class is also in use.
• No limit on length of name
• Follow Camel Cases. For example: averageMarksOfClass
• Every word first letter is capital except the first one
Variables (Cont’d)
Variables (Cont’d)
ava doesn’t allow to store any value beyond the range of any data typ
Variables (Cont’d)
Literals
• Literals are constant values that are used in a program
Z= 5*x + 7*y
• Here 5, 7 are literals.
• there are different types of literals:
• int literals- representing integer type.
• float and double literals- representing decimal type.
• char literals- representing characters in single quotes.
• string literals- representing characters in double quotes.
Literals (Cont’d)
Literals (Cont’d)
Literals (Cont’d)
• Integer literals can be represented in various number
system:
• Decimal – (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
• Binary – (0, 1)
• Octal – (0, 1, 2, 3, 4, 5, 6, 7)
• Hexadecimal – (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F)
Literals (Cont’d)
Literals (Cont’d)
Thank You