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

Constants, Variable, Data Types

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

Constants, Variable, Data Types

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

UNIT II

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?

• In Java, to declare any variable as


constant, we use static and final modifiers.
• It is also known as non-access modifiers.
• According to the Java naming
convention the identifier name must be
in capital letters.
STATIC AND FINAL MODIFIERS

• The purpose to use the static modifier is to manage the


memory.

• It also allows the variable to be available without


loading any instance of the class in which it is
defined.

• The final modifier represents that the value of the


variable cannot be changed. It also makes the primitive
data type immutable or unchangeable.
THE SYNTAX TO DECLARE
A CONSTANT
static final datatype identifier_name=value;
Example:

static final double PRICE=432.78;

• Where static and final are the non-access modifiers.


• The double is the data type and PRICE is the
identifier name in which the value 432.78 is
assigned.

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.

• It also affects the performance because a


constant variable is cached by both JVM
and the application.

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

Rules to Define Fractional Form


1.It must have at least one digit.
2.It must have a decimal point
3.It may have positive or negative sign. The
default is positive sign and it is optional.
4.Comma, spaces, or any other symbols are
not allowed.
For example, 3.14, -9.1, 0.67.

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.

Rules to Define Exponent Form:


• Mantissa and exponent must be separated by e or E.
• Mantissa can be positive or negative, the default is positive.
• Exponent must have at least one digit.
• The exponent can be positive or negative, the default is positive
For example, 100.34e4, -56E10, 0.233E10, -0.94e15.
13
NON-NUMERIC CONSTANTS
A constant that does not contain digits is called non-
numeric constants. There are the following two types of non-numeric
constants:

Character Constants String Constants


• A Character constant is a single • String constants consist of zero or
alphabet, digit or any special more characters enclosed in double
symbol enclosed using single quotes ("").
quotes.
• At the end of the string, the null
• For example, 'Y', 'd', '6', '#', '&'. character i.e '\0' is automatically
placed by the compiler.
• The maximum length of a character
constant is 1 character long. • For example, "hello", " "
(denotes blank space), "111".
• It means that we cannot put more
than one character inside single
quotation marks.
14
BACKSLASH CHARACTER CONSTANTS
Java also supports backslash character constants. These are
used in output methods. It is also known as escape \b Backspace
sequence. For Example, \n, \t, \a, etc. \f From feed
• Although it consists of two characters but it represents a \n New line
single character.
\r Carriage return
• Each escape sequence has Unicode value.
\t Horizontal tab
• Each and Every combination must start with backslash
\" Double quote
character (\).
\' Single quote
• These are non-printable characters.
\\ Backslash
• It can also be expressed in terms of octal digits or
hexadecimal sequence. \v Vertical tab
\a Alert
• Escape sequence in character constants and string
literals are replaced by their equivalent and then \? Question mark
adjacent string literals are concatenated. \N Octal constant
• Escape Sequences are preprocessed by Preprocessor. \xN Hexadecimal constant
15
POINTS TO REMEMBER:
• Write the identifier name in capital letters that we want to declare as
constant. For example, MAX=12.
• If we use the private access-specifier before the constant name, the
value of the constant cannot be changed in that particular class.
• If we use the public access-specifier before the constant name, the
value of the constant can be changed in the program.

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

• It is the same as the final variables.


• It is a list of constants.
• Java provides the Enum keyword to define the enumeration.
• It defines a class type by making enumeration in the class that
may contain instance variables, methods, and constructors.

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

• A variable is a container which holds the value while the Java


program is executed.
• A variable is assigned with a data type.
• Variable is a name of memory location. There are three types of
variables in java: local, instance and static.
• There are two types of data types in Java: primitive and non-
primitive.

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.

1.int data=10;//Here data is variable

21
TYPES OF VARIABLES

There are three types of variables in Java:

• local variable
• instance variable
• static variable

22
1) LOCAL VARIABLE

• A variable declared inside the body of the method


is called local variable.
• You can use this variable only within that method
and the other methods in the class aren't even
aware that the variable exists.
• A local variable cannot be defined with "static"
keyword.

23
2) INSTANCE VARIABLE

• A variable declared inside the class but outside


the body of the method, is called an instance
variable. It is not declared as static.
• It is called an instance variable because its value
is instance-specific and is not shared among
instances.

24
3) STATIC VARIABLE

• A variable that is declared as static is called a static


variable.
• It cannot be local. You can create a single copy of
the static variable and share it among all the
instances of the class.
• Memory allocation for static variables happens
only once when the class is loaded in the memory.

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

boolean false 1 bit

char '\u0000' 2 byte

byte 0 1 byte

short 0 2 byte

int 0 4 byte

long 0L 8 byte

float 0.0f 4 byte

double 0.0d 8 byte

31

You might also like