Professional Documents
Culture Documents
Constants
Java Constant s
Numeric 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 Constants
o e.g. P d 6 ; } o Escape Sequences
\b \f \n \r \t \ \ \\
String Constants
anuraggie-java@yahoo.co.in
Variables
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
Primitive(Intrinsic ) Numer ic Integ er byte short int long FloatingPoint float double NonNumeric Chara cter char Boole an Classe s
boolea n
anuraggie-java@yahoo.co.in
Integer Types
anuraggie-java@yahoo.co.in
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
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.
Initializing a Variable
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
anuraggie-java@yahoo.co.in
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
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.
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
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
anuraggie-java@yahoo.co.in
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);
System.out.println(, World);
anuraggie-java@yahoo.co.in
Outputting values
class PrintTest{ public static void main(){ int n=5; int sq = n * n;
anuraggie-java@yahoo.co.in
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.
anuraggie-java@yahoo.co.in
End of Chapter-4
Thank You