Java Programming Mr Muhammad Hanif Lecturer Information Technology

  • Slides: 13
Download presentation
Java Programming Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

Java Programming Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh 1

Integer Literals �Integers are used to store numbers in decimal, octal and hexa-decimal numbers.

Integer Literals �Integers are used to store numbers in decimal, octal and hexa-decimal numbers. �Byte and short also support integer value storage, but within their own limit i-e 8 and 16 bits. Decimal Octal (Zero at start) Hexadecimal (0 x at Start) 1, 2, 1024 04 0 x. A 2 06 0 x 3 F 1024 08 0 x. FF 677 09 (Invalid Number) 0 x 33 2

Floating Point Literals �Represent Decimal Values �Can represent standard or scientific notation Standard Scientific

Floating Point Literals �Represent Decimal Values �Can represent standard or scientific notation Standard Scientific Whole Number + Decimal + Fractional Number Floating Point Number + Power by 10 E. g: 2. 0, 3. 1345, 0. 6756 E. g: 6. 022 E 23, 314159 E-05, 2 e+100 Note: Exponent is represented by e or E. �Float (32 bit) literals append f or F and double (64 bit) literal append d or D. (Default is double) 3

Boolean Literals �Boolean literals can have only two possible values �Boolean values can not

Boolean Literals �Boolean literals can have only two possible values �Boolean values can not be converted into numerical representation of zero and one. Boolean 1 Boolean 2 True False 4

Character Literals �Each char is index of Unicode character. �It consumes 16 bits to

Character Literals �Each char is index of Unicode character. �It consumes 16 bits to store char type literal �Each character is enclosed in single quotes. �E. g. ‘a’, ‘z’ �Escape Sequence: �There are some characters which are not entered directly, called escape sequence. � Such as (Backslash)and ‘ (Single quote) 5

Character Literals 6

Character Literals 6

String Literals �String Literals are sequence of characters enclosed in pair of double quotes.

String Literals �String Literals are sequence of characters enclosed in pair of double quotes. �E. g. “Hello World” �They must begin and end on the same line. �There is no line-continuation escape sequence 7

Variables �Variable �Basic unit of storage of data in a java program. �Declaration of

Variables �Variable �Basic unit of storage of data in a java program. �Declaration of variable �It includes type and identifier of variable. �E. g. int a, b, c; // declares three integers, a, b, and c. �Initialization of variable �It includes type, identifier and value which will be stored in variable. �E. g. int a=66, b=555, c=30, 000; // initialize three integers, a, b, and c. �Both Declaration & Initialization �E. g. int a = 3, b, c = 5; // declares a, b & c integers, initializing a and c. 8

Variables �Dynamic Initialization: �Values will be assigned to variables at run time. �Built-in Methods

Variables �Dynamic Initialization: �Values will be assigned to variables at run time. �Built-in Methods �Methods having already defined bodies are called as a built-in methods. 9

Variables class Dyn. Init { public static void main(String args[]) { double a =

Variables class Dyn. Init { public static void main(String args[]) { double a = 3. 0, b = 4. 0; //constant initialization of variables a & b. double c = Math. sqrt(a * a + b * b); //Dynamic initialization of variable c. System. out. println("Hypotenuse is " + c); } } Output: Hypotenuse is: 5. 0 Note: Math is a class and sqrt() is a built-in method of that class. 10

Scope of Variable �Scope determines what objects are visible to other parts of your

Scope of Variable �Scope determines what objects are visible to other parts of your program. �Scope/Block can be nested �Each time you create a block of code, you are creating a new, nested scope. �This means that objects declared in the outer scope will be visible to code within the inner scope. �However, the reverse is not true. Objects declared within the inner scope will not be visible outside it. 11

// Demonstrate block scope. class Scope { public static void main(String args[]) { int

// Demonstrate block scope. class Scope { public static void main(String args[]) { int x; // known to all code within main x = 10; if(x == 10) { // start new scope int y = 20; // known only to this block // x and y both known here. System. out. println("x and y: " + x + " " + y); x = y * 2; } // y = 100; // Error! y not known here // x is still known here. System. out. println("x is " + x); } } Output: x and y: 10 20 x is 40 12

Program: Lifetime of variable class Life. Time { public static void main(String args[]) {

Program: Lifetime of variable class Life. Time { public static void main(String args[]) { int x; for(x = 0; x < 3; x++) { int y = -1; // y is initialized each time block is entered System. out. println("y is: " + y); // this always prints -1 y = 100; System. out. println("y is now: " + y); } } } Output: y is: -1 y is now: 100 13