You are on page 1of 29

Chapter-4 Constants, Variables and Data Types Presented by Anurag Kumar

Constants
Java Constant s

Numeric Constants

Character Based Constants

Integer Constants

Real Constants

Character Constants

String Constants

anuraggie-java@yahoo.co.in

Numeric Constants

Integer Constants
o Decimal integers e.g. 12 -321 0 654 o Octal integers e.g. 037 0435 0551 041 o Hexadecimal integers 0X2 0xbc4 0X79 o Fixed-Point Notation e.g. 0.0045 -45.6 .56 o Scientific Notation e.g. 4.5e-3 -4.5E1 5.6e-1

Real Constants

anuraggie-java@yahoo.co.in

Character Based Constants

Character Constants
o e.g. P d 6 ; } o Escape Sequences
\b \f \n \r \t \ \ \\

String Constants

o e.g. Hello World 2005 ?!

anuraggie-java@yahoo.co.in

Variables

A variable is an identifier that denotes a storage location


Unlike constants, variable may take different values at
different times during the execution of the program. used to store a data value.

anuraggie-java@yahoo.co.in

Data Types

Every 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 be
stored in the variable.

anuraggie-java@yahoo.co.in

Data Types in Java

Primitive(Intrinsic ) Numer ic Integ er byte short int long FloatingPoint float double NonNumeric Chara cter char Boole an Classe s

Non-Primitive (Derived) Arrays Interfa ces

boolea n

anuraggie-java@yahoo.co.in

Integer Types

anuraggie-java@yahoo.co.in

Floating Point Types

anuraggie-java@yahoo.co.in

Character Type

The data type is char. The size of char data type is 2 bytes. the range of char is 0 to 65536. There are no negative Java uses UNICODE to represent characters. UNICODE is a 16-bit character coding system and
supports more than 34000 characters derived from 24 languages from America, Europe, Middle East, Africa and Asia (including India). chars.

anuraggie-java@yahoo.co.in

Boolean Type

Java has a simple type, called boolean, for logical


It can have only two values, true or false. This is the type returned by all relational operators, such boolean is also the type required by the conditional
expressions that govern the control statements such as if, while, do...while and for. values.

as: a>b

anuraggie-java@yahoo.co.in

Declaring a Variable

Variables 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; type identifier1, identifier2,...,identifierN; float x,y,z;


anuraggie-java@yahoo.co.in

Initializing a Variable

Giving 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
o varaible 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

anuraggie-java@yahoo.co.in

Giving values to variables

By using an assignment statement By using read statement

By using assignment statement


varaiableName = value

anuraggie-java@yahoo.co.in

By using Read Statement


Reading 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); } }
anuraggie-java@yahoo.co.in

By using Read Statement


Reading 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); } }
anuraggie-java@yahoo.co.in

Reading other types of values


BufferedReader 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());

anuraggie-java@yahoo.co.in

Scope of Variables

instance variables
o are declared inside the class. o are created when the objects are instantiated. o are assoicated with the objects. o take different values for different objects.
o are declared inside the class as static. o are global to the class and belongs to entire set of objects that class creates. o only one memory location is created for each class variable.

class variables

local variables: are declared inside the methods.


anuraggie-java@yahoo.co.in

Scope of local variables


Block 1
{ { : int x=0; int n=5; : } { : int m=10; : }

Block 2

Block 3

anuraggie-java@yahoo.co.in

Symbolic Constants

Symbolic constants are declared using a keyword final. Symbolic names are generally written in capitals. After declaration of symbolic constants, they should not
Symbolic constants are declared for types. This is not Symbolic constants in java can not be decalred inside
final type SYMBOLIC_NAME = the methods. They can only be declared as class data members. e.g. value;
anuraggie-java@yahoo.co.in

be assigned any other value within the program by using an assignment operator.

done in C where symbolic constants are declared using #define preprocessor directive.

Javas Automatic Type Conversion

Javas Automatic Type Conversion takes place if


o the two types are compatible. o the destination type is larger than the source type. (widening takes place)
NOTE:

o All numeric types are compatible with each other. o Numeric types are not compatible with boolean and char. o Also char and boolean are not compatible with each other.

anuraggie-java@yahoo.co.in

Type Casting for casting Incompatible Types

For narrowing conversion we need type casting. Type casting may result in loss of information in case of
narrowing.

(target_type) value
e.g. int x=256; byte b=(byte) x;

anuraggie-java@yahoo.co.in

Automatic Type Promotion in Expressions

in 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);

anuraggie-java@yahoo.co.in

Type Promotion Rules

All byte and short values are promoted to int. if one operand is long, the whole expression is
if one operand is float, the entire expression is promoted if any operand is double, then the expression is
promoted to double promoted to long.

to float.

anuraggie-java@yahoo.co.in

Outputting values

print() println()
ex 1: System.out.print(Hello); System.out.print(, World); ex 2: System.out.println(Hello);

Hello, World Hello , World

System.out.println(, World);

anuraggie-java@yahoo.co.in

Outputting values
class 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

anuraggie-java@yahoo.co.in

Default Values of various types of Variables


byte 0

short 0
int 0 long 0

float 0.0f
double 0.0 char null

boolean false
reference null
anuraggie-java@yahoo.co.in

Assignments
Q.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 Automatic Type Conversion and Type Casting.

anuraggie-java@yahoo.co.in

End of Chapter-4

Thank You

You might also like