Constants, Variable, Data Types
Constants, Variable, Data Types
CONSTANTS
• Constant is a value that cannot be changed after
assigning it.
• Java does not directly support the constants.
• There is an alternative way to define the constants in
Java by using the non-access modifiers static and final.
2
HOW TO DECLARE CONSTANT IN
JAVA?
5
• In the above statement, the static modifier causes the
variable to be available without an instance of its
defining class being loaded and the final modifier
makes the variable fixed.
• Here a question arises that why we use both static
and final modifiers to declare a constant?
• If we declare a variable as static, all the objects of
the class (in which constant is defined) will be able
to access the variable and can be changed its value.
• To overcome this problem, we use the final modifier
with a static modifier.
• When the variable defined as final, the multiple
instances of the same constant value will be created
for every different object which is not desirable.
6
• When we use static and final modifiers together,
the variable remains static and can be initialized
once.
• Therefore, to declare a variable as constant, we
use both static and final modifiers.
• It shares a common memory location for all
objects of its containing class.
7
WHY WE USE CONSTANTS?
• The use of constants in programming makes
the program easy and understandable which
can be easily understood by others.
8
TYPES OF CONSTANTS
9
NUMERIC CONSTANTS
Numeric constants are the constants that contains numerals. It may also
have a leading sign and decimal point.
Rule to Define Numeric Constants
• Must have at least one digit.
• It should not have comma, space, and another special symbol.
• It may have positive or negative sign. If no sign is preceded then the
constant assumed positive. It is optional to preceded a constant with a
positive sign.
10
REAL CONSTANTS
• Numeric constants that have
a decimal point are
called real or floating-point constants.
• By default, the real constants are
of double type. The real constants can be written in
• We can explicitly mention the type of a the following two forms:
floating-point constant as a float by
appending the letter f or F at the end of • Fractional Form
the constant. • Exponential Form
• For example, 45f, -0.14f, 5.6F.
11
FRACTIONAL FORM
12
Exponential Form
• It is used to represent a real constant when a number is too small or too large.
• For example, 0.00000149 can be represented as 1.49e-6.
• The part of the number before e is called mantissa i.e 1.49, whereas, the part after e
is called the exponent i.e, 6.
16
DECLARING CONSTANT
import java.util.Scanner;
public class ConstantExample1 {
//declaring constant
private static final double PRICE=234.90;
public static void main(String[] args) {
int unit;
double total_bill;
System.out.print("Enter the number of units you have used: ");
Scanner sc=new Scanner(System.in);
unit=sc.nextInt();
total_bill=PRICE*unit;
System.out.println("The total amount you have to deposit is: "+total_bill);
} }
17
USING ENUMERATION (ENUM)
AS CONSTANT
18
EXAMPLE:
1.public class EnumExample
2.{
3.//defining the enum
4.public enum Color {Red, Green, Blue, Purple
, Black, White, Pink, Gray}
5.public static void main(String[] args)
6.{
7.//traversing the enum
8.for (Color c : Color.values())
9.System.out.println(c);
10.}
11.} 19
JAVA VARIABLES
20
VARIABLE:
• A variable is the name of a reserved area allocated in memory.
• In other words, it is a name of the memory location.
• It is a combination of "vary + able" which means its value can be
changed.
21
TYPES OF VARIABLES
• local variable
• instance variable
• static variable
22
1) LOCAL VARIABLE
23
2) INSTANCE VARIABLE
24
3) STATIC VARIABLE
25
1. Declaration of variable:
datatype variable=value;(int a=10;)
2. Giving values to variable:
I. By using assignment statement(a=2;).
II. By using a read statement (readLine()
method) reading or getting a data's from
keyboard.
26
public class A
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}//end of class
27
DATA TYPES
Data types specify the different sizes and values that can
be stored in the variable.
There are two types of data types in Java:
1. Primitive data types: The primitive data types
include boolean, char, byte, short, int, long, float and
double.
2. Non-primitive data types: The non-primitive data
types include Classes, Interfaces, and Arrays.
28
JAVA PRIMITIVE DATA
TYPES:
• In Java language, primitive data types are the
building blocks of data manipulation.
• These are the most basic data types available
in Java language.
29
30
Data Type Default Value Default size
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
31