of 29
7/28/2019 ConstVarDataTypes
1/29
Chapter-4
Constants, Variables and Data TypesPresented by Anurag Kumar
7/28/2019 ConstVarDataTypes
2/29
anuraggie- [email protected]
Constants
Java
Constant
s
Numeric
Constants
Character
Based
Constants
Integer
Constants
Real
Constants
Character
Constants
String
Constants
7/28/2019 ConstVarDataTypes
3/29
anuraggie- [email protected]
Numeric ConstantsInteger Constants
oDecimal integers e.g. 12 -321 0 654
o
Octal integers e.g. 037 0435 0551 041oHexadecimal integers 0X2 0xbc4 0X79
Real Constants
oFixed-Point Notation e.g. 0.0045 -45.6 .56
oScientific Notation e.g. 4.5e-3 -4.5E1 5.6e-1
7/28/2019 ConstVarDataTypes
4/29
anuraggie- [email protected]
Character Based ConstantsCharacter Constants
oe.g. P d 6 ; }
o
Escape Sequences \b \f \n \r \t \ \ \\
String Constants
oe.g. Hello World 2005 ?!
7/28/2019 ConstVarDataTypes
5/29
anuraggie- [email protected]
VariablesA variable is an identifier that denotes a storage location
used to store a data value.
Unlike constants, variable may take different values atdifferent times during the execution of the program.
7/28/2019 ConstVarDataTypes
6/29anuraggie- [email protected]
Data TypesEvery variable in Java must have a datatype.
Data type specify the size of the variable.
Data type also specify the type of values that can bestored in the variable.
7/28/2019 ConstVarDataTypes
7/29anuraggie- [email protected]
Data Types inJava
Primitive(Intrinsic
)
Non-Primitive
(Derived)
Numer
ic
Non-
Numeric
Classe
s
Arrays Interfa
ces
Integ
er
Floating-
Point
Chara
cter
Boole
an
byte
short
int
long
float
double
char boolea
n
7/28/2019 ConstVarDataTypes
8/29anuraggie- [email protected]
Integer Types
7/28/2019 ConstVarDataTypes
9/29anuraggie- [email protected]
Floating Point Types
7/28/2019 ConstVarDataTypes
10/29anuraggie- [email protected]
Character TypeThe data type is char.
The size of char data type is 2 bytes.
the range of char is 0 to 65536. There are no negativechars.
Java uses UNICODE to represent characters.
UNICODE is a 16-bit character coding system and
supports more than 34000 characters derived from 24languages from America, Europe, Middle East, Africa
and Asia (including India).
7/28/2019 ConstVarDataTypes
11/29anuraggie- [email protected]
Boolean TypeJava has a simple type, called boolean, for logical
values.
It can have only two values, true orfalse.
This is the type returned by all relational operators, such
as: a>b
boolean is also the type required by the conditional
expressions that govern the control statements such asif, while, do...while and for.
7/28/2019 ConstVarDataTypes
12/29anuraggie- [email protected]
Declaring a VariableVariables are the names of storage locations.
A variable declaration tells the compiler what the
variable name is.
It specifies what type of data the variable will hold.
The place of declaration decides the scope of the
variable.
A variable must be declared before its use.
e.g. int x;
float x,y,z;type identifier1, identifier2,...,identifierN;
7/28/2019 ConstVarDataTypes
13/29anuraggie- [email protected]
Initializing a VariableGiving a value to a variable at the time of its declaration
is called Initialization. e.g.
int x=5;float x=4, y=3, z=5;
char c = p;
Dynamic Initialization
ovaraible can be initialized dynamically, using an
expression at the time when the variable is declared. e.g.
int a=4,b=5; //initialization
int x=a*b; //dynamic initialization
7/28/2019 ConstVarDataTypes
14/29anuraggie- [email protected]
Giving values to variablesBy using an assignment statement
By using read statement
By using assignment statement
varaiableName = value
7/28/2019 ConstVarDataTypes
15/29
anuraggie- [email protected]
By using Read StatementReading a Single Character
import java.io.*;
class BRRead{public static void main(String args[ ]) throws IOException {
char c;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));c = (char) br.read();
System.out.println(c);
}
}
7/28/2019 ConstVarDataTypes
16/29
anuraggie- [email protected]
By using Read StatementReading an Integer
import java.io.*;
class BRRead{public static void main(String args[ ]) throws IOException {
int n;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));n =Integer.parseInt(br.readLine());
System.out.println(n);
}
}
7/28/2019 ConstVarDataTypes
17/29
anuraggie- [email protected]
Reading other types of valuesBufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String s=in.readLine();Byte b=Byte.parseByte(in.readLine());
short s= Short.parseShort(in.readLine());
int i=Integer.parseInt(in.readLine());
long l=Long.parseLong(in.readLine());
float f=Float.parseFloat(in.readLine());
double d=Double.parseDouble(in.readLine());
7/28/2019 ConstVarDataTypes
18/29
anuraggie- [email protected]
Scope of Variablesinstance variables
oare declared inside the class.
oare created when the objects are instantiated.
oare assoicated with the objects.
otake different values for different objects.
class variables
oare declared inside the class as static.
oare global to the class and belongs to entire set ofobjects that class creates.
oonly one memory location is created for each class
variable.
local variables: are declared inside the methods.
7/28/2019 ConstVarDataTypes
19/29
anuraggie- [email protected]
Scope of local variables
{
int x=0;
}
{
:
int n=5;
:
}
{
:int m=10;
:
}
Block 1
Block
2
Block
3
7/28/2019 ConstVarDataTypes
20/29
anuraggie- [email protected]
Symbolic ConstantsSymbolic constants are declared using a keyword final.
Symbolic names are generally written in capitals.
After declaration of symbolic constants, they should notbe assigned any other value within the program by
using an assignment operator.
Symbolic constants are declared for types. This is not
done in C where symbolic constants are declared using#define preprocessor directive.
Symbolic constants in java can not be decalred inside
the methods. They can only be declared as class data
members. e.g.
final type SYMBOLIC_NAME =
value;
7/28/2019 ConstVarDataTypes
21/29
anuraggie- [email protected]
Javas Automatic Type ConversionJavas Automatic Type Conversion takes place if
othe two types are compatible.
othe destination type is larger than the source type.
(widening takes place)
NOTE:
oAll numeric types are compatible with each other.
oNumeric types are not compatible with boolean and char.
oAlso charand boolean are not compatible with each
other.
7/28/2019 ConstVarDataTypes
22/29
anuraggie- [email protected]
Type Casting for casting Incompatible TypesFornarrowing conversion we need type casting.
Type casting may result in loss of information in case of
narrowing.
e.g.
int x=256;byte b=(byte) x;
(target_type) value
7/28/2019 ConstVarDataTypes
23/29
anuraggie- [email protected]
Automatic Type Promotion in Expressionsin an expression the precision required of an
intermediate value will sometimes exceed the range of
either operand.
byte b = 50;
b = b * 2; // Java promotes byte and short operand to int
whenevaluating an expression
so the correct statement will be:
b = (byte)( b * 2);
7/28/2019 ConstVarDataTypes
24/29
anuraggie- [email protected]
Type Promotion RulesAll byte and short values are promoted to int.
if one operand is long, the whole expression is
promoted to long.
if one operand is float, the entire expression is promoted
to float.
if any operand is double, then the expression is
promoted to double
7/28/2019 ConstVarDataTypes
25/29
anuraggie- [email protected]
Outputting valuesprint()
println()
ex 1:
System.out.print(Hello);
System.out.print(, World);
ex 2:
System.out.println(Hello);
System.out.println(, World);
Hello,
World
Hello
, World
7/28/2019 ConstVarDataTypes
26/29
anuraggie- [email protected]
Outputting valuesclass PrintTest{
public static void main(){
int n=5;
int sq = n * n;
System.out.println(The Square of + n + is : + sq);
}
}
The Square of 5 is: 25
7/28/2019 ConstVarDataTypes
27/29
anuraggie- [email protected]
Default Values of various types of Variables
byte 0
short 0
int 0
long 0
float 0.0f
double 0.0char null
boolean false
reference null
7/28/2019 ConstVarDataTypes
28/29
anuraggie- [email protected]
AssignmentsQ.1 What are symbolic constants? How are they useful in
developing programs.
Q.2 List the eight basic data types available in java. Explain
them using examples.
Q.3 Explain Javas Au tomat ic Type Convers ionand Type
Casting.
7/28/2019 ConstVarDataTypes
29/29
End of Chapter-4
Thank You