Primitive data types Lecture 03 Review of Last

  • Slides: 24
Download presentation
Primitive data types Lecture 03

Primitive data types Lecture 03

Review of Last Lecture • Write a program that prints the multiplication table of

Review of Last Lecture • Write a program that prints the multiplication table of 5. class Multiplication. Table { public static void main(String args[]) { // Following code shows the multiplication table of 5. System. out. println("5 x 1 = 5 "); System. out. println("5 x 2 = 10"); System. out. println("5 x 3 = 15 "); System. out. println("5 x 4 = 20"); System. out. println("5 x 5 = 25"); System. out. println("5 x 6 = 30 "); System. out. println("5 x 7 = 35"); System. out. println("5 x 8 = 40 "); System. out. println("5 x 9 = 45"); System. out. println("5 x 10 = 50"); } }

Lecture outcomes • Primitive data – – – – • • integer double string

Lecture outcomes • Primitive data – – – – • • integer double string char Float Long boolean Declaration Initialisation Assignments Arithmetic operators

Example • • • 123 (int) 1. 5 (double) “Hello. World” (String) `H’ (Char)

Example • • • 123 (int) 1. 5 (double) “Hello. World” (String) `H’ (Char) ….

Basic Definitions • Variables - A name that refers to a value. • Assignment

Basic Definitions • Variables - A name that refers to a value. • Assignment Statement - Associate a value with a variable. • Constant: – 456—a literal numerical constant • System. out. println(456); // Java – “A Literal String Constant” • System. out. println(“My First Java”); // Java

There are eight primitive data types • Boolean, byte, char, int, double, float, long,

There are eight primitive data types • Boolean, byte, char, int, double, float, long, short • In bytes, how long is the short data type? The int data type, the long data type? • In bytes, how long is the float data type? The double data type? • How long is the char data type?

Primitives Data types and Ranges PRIMITIVE SIZE IN BITS RANGE int 32 bits (4

Primitives Data types and Ranges PRIMITIVE SIZE IN BITS RANGE int 32 bits (4 bytes) -231 to 231 -1 -2, 147, 483, 648 to +2, 147, 483, 647 long 64 bits -- 8 bytes -263 to 263 - 1 float 32 bits- -4 bytes -+(1. 40129846432481707 e-45 to 3. 40282346638528860 e+38} double 64 +-(4. 94065645841246544 e-324 to 1. 79769313486231570 e+308) char 16 bits One character string 16 bits per char Not applicable boolean 8 bits--1 -byte True or false

Examples Type Set of values Sample literal vlues interges 99 (-12) 214748647 double Floating-point

Examples Type Set of values Sample literal vlues interges 99 (-12) 214748647 double Floating-point numbers 3. 14 (-1. 5) 6. 0021 1023 boolean Boolean values True or false characters ‘a’ ‘ 1’ ‘£’ ‘%’ ‘n’ String Sequence of characters “AC” ”Hello” ” 1. 5”

Variable declaration Variable name Variable type int x; x integer double d; d double

Variable declaration Variable name Variable type int x; x integer double d; d double char c; c character String s; s string Float f; f float

The assignment operator = declaration Variable name int x; Declare the variable x as

The assignment operator = declaration Variable name int x; Declare the variable x as an integer x = 36; int x = 36; Sets x to constant 36 at execution time Sets x = to the constant 36 at compile time Initializes x to 36 at the time memory is set aside for it String y; Declare the variable x as an integer y = “Hellow”; Sets y to constant “Hello” at execution time String y = “Hello”; Sets y = to the constant “Hello” at compile time Initializes x to “Hello” at the time memory is set aside for it

Initialisation • If no value is assigned prior to use, then the compiler will

Initialisation • If no value is assigned prior to use, then the compiler will give an error • Java sets primitive variables to zero or false in the case of a boolean variable • All object references are initially set to null • An array of anything is an object – Set to null on declaration – Elements to zero false or null on creation

Declaration Examples int index = 1. 2; // compiler error boolean ret. Ok =

Declaration Examples int index = 1. 2; // compiler error boolean ret. Ok = 1; // compiler error double five. Fourths = 5 / 4; // no error! float ratio = 5. 8 f; // correct double five. Fourths = 5. 0 / 4. 0; // correct • 1. 2 f is a float value accurate to 7 decimal places. • 1. 2 is a double value accurate to 15 decimal places.

Declaration (Cont) int a, b, c ; b =1; a=b; c =a; System. out.

Declaration (Cont) int a, b, c ; b =1; a=b; c =a; System. out. print(“c= “ + c); • What is the value of a, b & c

Example Int 1. java // uninitialised data // this program will declare and print

Example Int 1. java // uninitialised data // this program will declare and print a number Public class int 3 { public static void main(String[] arguments) { int weight; System. out. println("your weight is " + weight); } } //end of program

Example Int 2. java // this program will declare and print a number class

Example Int 2. java // this program will declare and print a number class int 2 { public static void main(String[] arguments) { int weight = 68; System. out. println("your weight is " + weight); } } //end of program

Example Int 5. java // uninitialised data // this program will declare and print

Example Int 5. java // uninitialised data // this program will declare and print a number class int 5 { public static void main(String[] arguments) { int weight; weight = 65 ; //65 = weight ; System. out. println("your weight is " + weight); } } //end of program

Example String 2. java // this program will declare and print a string class

Example String 2. java // this program will declare and print a string class string 2 { public static void main(String[] arguments) { String name = "Lahcen"; String x = "my name is "; System. out. println( x + name ); //print string x and then string name } } //end of program

Basic Mathematical Operators • * / % + - are the mathematical operators •

Basic Mathematical Operators • * / % + - are the mathematical operators • * / % have a higher precedence than + or double my. Val = a + b % d – c * d / b; • Is the same as: double my. Val = (a + (b % d)) – ((c * d) / b);

Basic arithmetic Operators Operator Meaning Example + Addition 2+3 = 5 * Multiplication 2*3=6

Basic arithmetic Operators Operator Meaning Example + Addition 2+3 = 5 * Multiplication 2*3=6 - subtraction 3 -2=1 / division 4/2=2 % mod 5 % 2 = 1, 6 % 2= 0

Precedence Rules 1. Evaluate all sub-expressions in parentheses 2. Evaluate nested parentheses from the

Precedence Rules 1. Evaluate all sub-expressions in parentheses 2. Evaluate nested parentheses from the inside out 3. In the absence of parentheses or within parentheses a. Evaluate *, /, or % before + or – b. Evaluate sequences of *, /, and % operators from left to right c. Evaluate sequences of + and – operators from left to right

Built-in Math Functions in Java

Built-in Math Functions in Java

Example: Calculate square root class Square. Root. Exp{ public static void main(String args[]) {

Example: Calculate square root class Square. Root. Exp{ public static void main(String args[]) { int x = 25; int result = Math. sqrt(x); System. out. println(“Square root of " + x + “ is " + result); } }

Exercise • Assume that the length and width of the rectangle is 10 and

Exercise • Assume that the length and width of the rectangle is 10 and 5. Write a program which compute area of the rectangle and display result on the screen.

Summary • • • Different data type Declarations Arithmetic operators Parse string to integer.

Summary • • • Different data type Declarations Arithmetic operators Parse string to integer. Boolean operators Assignments