Lecture Two: Second Java Program, Constants, Data Types and Variables
Lecture Two: Second Java Program, Constants, Data Types and Variables
25/3/2010
class Test { public static void main(String args[]) { int a, b; a=100; b=23; Add ob = new Add(); System.out.println(The addition: + ob.add_num (a,b)); } }
25/3/2010
Points to be Noted
3
of the source code must be the name of the class containing the main method.
Object of a class is created using new operator. Method of a class is accessed through an object of that
class.
+ in System.out.println() method concatenate two
strings.
Sarker Tanveer Ahmed, CSE, DU
25/3/2010
type.
All assignments are checked for type compatibility.
Java compiler checks all expressions and parameters
25/3/2010
There are exactly eight primitive data types in Java Four of them represent whole valued signed numbers:
float, double
char
boolean
25/3/2010
types is their size, and therefore the values they can store:
Type
byte short int long float double
Storage
8 bits 16 bits 32 bits 64 bits 32 bits 64 bits
Min Value
-128 -32,768 -2,147,483,648 < -9 x 1018
Max Value
127 32,767 2,147,483,647 > 9 x 1018
+/- 3.4 x 1038 with 7 significant digits +/- 1.7 x 10308 with 15 significant digits
25/3/2010
It uses unicode to represent character. The char type is unsigned 16 bit values ranging from 0 to 65536. ASCII still ranges from 0 to 127.
Example: class test { public static void main (String args[]) { char ch1, ch2; ch1=88; ch2=Y; System.out.println (ch1 and ch2: + ch1+ +ch2); } } Output: ch1 and ch2: X Y
Sarker Tanveer Ahmed, CSE, DU
25/3/2010
Example: class test { public static void main (String args[]) { char ch1; ch1= X; Sytem.out.println (ch contains +ch1); ch1++; System.out.println (ch1 is now + ch1); } } Output: ch1 contains X Ch1 is now Y
25/3/2010
Booleans
9
Size is 1 bit two value: true and false. This type is returned by all relational operators. Example: boolean b; b= true; 1. System.out.println(b is +b); 2. System.out.println(10>9 is +(10>9)); Output: b is true 10>9 is true
25/3/2010
Literals
10
Integer Literals
1. base 10 1,2,43 etc. 2. base 8 octal values are denoted in java by a leading 0. 3. base 16 hexadecimal values are denoted by leading 0x or 0X. Any whole number is by default integer (32 bits). To specify a long literal, the number should appended with an upper- or lowercase L.
25/3/2010
Literals
11
1. Standard Notation 3.14159, 0.6667, 2.0 etc. 2. Scientific Notation 6.022E23, 2e+100. Floating point literals are by default of type double. To specify a float literal, we must append an F or f to the constant. Boolean Literals Two values true and false. True is not equal 1 and false is not equal to 0. They can be assigned to variable declared as boolean.
Sarker Tanveer Ahmed, CSE, DU
25/3/2010
Literals
Character Literals: A literal character is represented inside a pair of single quotes.
Escape sequence
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. \ \ \\ \r \n \f \t \b \ddd \uxxxx
Unicode Representation
\u0027 \u0022 \u005c \u000d \u000a \u000b \u0009 \u0008
Description
Single quote Double quote Backslash Carriage Return New line Form feed Tab Backspace Octal Character Hexadecimal Unicode character
25/3/2010
12
Literals
13
quotes. In java string must begin and end on the same line.
25/3/2010
Variables
14
variable is undefined.
Sarker Tanveer Ahmed, CSE, DU
25/3/2010
curly brace. A block determines scope, that defines which objects are visible to other parts of your program. Variables declared within a block localize themselves. In case of nested block, the outer block encloses the inner block. The variables declared in the outer block is visible to the inner block but the reverse is not true. A variable will not hold its value once it has gone out of its scope. In an inner block, it is not possible to declare a variable of the same name as in outer block.
Sarker Tanveer Ahmed, CSE, DU
25/3/2010
Example: public static void main( String args[]) { int x =10; if ( x == 10) { int y =20; System.out.println(x and y: +x + +y); x= y * 2; } y= 100; //Error System.out.println (x is +x); }
Sarker Tanveer Ahmed, CSE, DU
25/3/2010
17
Chapter Three
-Book of Herbert Schieldt Read about keyword, identifier, character set yourself.
25/3/2010