Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char...

37
Primitive data Week 3

Transcript of Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char...

Page 1: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Primitive data

Week 3

Page 2: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Lecture outcomes

• Primitive data– integer– double– string– char– Float– Long– boolean

• Declaration • Initialisation • Assignments • Arithmetic operators• Boolean operators

Page 3: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Example

• 123 (int)• 1.5 (double)• “HelloWorld” (String)• `H’ (Char)• ….

Page 4: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Data Types

• Constants• Variables

Page 5: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

What is a Constant?

• 456—a literal numerical constant– System.out.println(456); // Java– Console.writeline(456); // Visual C#

• “A Literal String Constant”– System.out.println(“My First Java”); // Java– Console.writeline(“My First C#”); // Visual C#

Page 6: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

What is a variable?• It is a named computer location in memory

that holds values that might vary• Must that location have an address?– YES

• What has addresses? Bits, bytes, words, what?– Bytes

• Can a variable be more than one byte long?– YES

Page 7: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Data type Declarations

• Specify the type of data and the length of the data item in bytes

• int, short, long• float, double• boolean• char

Page 8: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Data Types -- Integer

• Int – the default declaration – 4-byte integer• Byte—1-byte integer• Short—2-byte integer• Long—8-byte integer

Page 9: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Floating Point

• Float—a 4-byte floating point number• Double—an 8-byte floating point number

Page 10: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

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?

Page 11: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Primitives sizes and RangesPRIMITIVE SIZE IN BITS RANGE

int 32bits (4 bytes) -231 to 231 -1 -2,147,483,648 to +2,147,483,647

long 64bits -- 8 bytes -263 to 263 - 1

float 32bits- -4 bytes -+(1.40129846432481707e-45 to 3.40282346638528860e+38}

double 64 +-(4.94065645841246544e-324 to 1.79769313486231570e+308)

char 16bits One character

string 16bits per char Not applicable

bool (boolean in Java) 8bits--1-byte True or false

Page 12: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Examples

Type Set of values Sample literal vlues

int interges 99 (-12) 214748647

double Floating-point numbers 3.14 (-1.5) 6.0021 1023

boolean Boolean values True or false

char characters ‘a’ ‘1’ ‘£’ ‘%’ ‘\n’

String Sequence of characters “AC” ”Hello” ” 1.5”

Page 13: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Variable declaration

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

Page 14: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

The assignment operator =

declaration Variable name

Int x;

x = 36;

Declare the variable x as an integer

Sets x to constant 36 at execution time

int x = 36;Sets x = to the constant 36 at compile time

Initializes x to 36 at the time memory is set aside for it

String y;

y = “Hellow”;

Declare the variable x as an integer

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

Page 15: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

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

Page 16: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Declaration Examples

int index = 1.2; // compiler errorboolean retOk = 1; // compiler errordouble fiveFourths = 5 / 4; // no error!float ratio = 5.8f; // correctdouble fiveFourths = 5.0 / 4.0; // correct

• 1.2f is a float value accurate to 7 decimal places.• 1.2 is a double value accurate to 15 decimal places.

Page 17: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

int a, b, c ; b =1; a=b; c =a; System.out.print(“c= “ + c);

• What is the value of a, b & c

Declaration (Cont)

Page 18: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

ExampleInt1.java

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

Page 19: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

ExampleInt2.java

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

Page 20: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

ExampleInt5.java

// uninitialised data// this program will declare and print a numberclass int5{ public static void main(String[] arguments) { int weight;

weight = 65 ; //65 = weight ;

System.out.println("your weight is " + weight); }}//end of program

Page 21: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

ExampleString2.java

// this program will declare and print a stringclass string2{ 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

Page 22: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Basic Mathematical Operators

• * / % + - are the mathematical operators• * / % have a higher precedence than + or -double myVal = a + b % d – c * d / b;

• Is the same as:double myVal = (a + (b % d)) –

((c * d) / b);

Page 23: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Basic arithmetic Operators

Operator Meaning E=3 xample

+ Addition 2+3 = 5

* Multiplication 2*3=6

- subtraction 3-2=1

/ division 4/2=2

% mod 5 % 2 = 1, 6 % 2= 0

Page 24: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Precedence Rules

1. Evaluate all sub-expressions in parentheses2. Evaluate nested parentheses from the inside out3. In the absence of parentheses or within

parenthesesa. Evaluate *, /, or % before + or –b. Evaluate sequences of *, /, and % operators from left to

rightc. Evaluate sequences of + and – operators from left to

right

Page 25: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

ExampleSumStr.java

public class SumStr

{

public static void main(String args[])

{

System.out.print(args[0] + args[1] );

}

}

}

} 27

Java Argument 2 7

Page 26: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

parse a string to integer.SumInt.java

public class SumInt

{

public static void main(String args[])

{

System.out.print( Integer.parseInt(args[0] )+ Integer.parseInt(args[1] ) );

}

}

}

} 9

Java SumInt 2 7

Page 27: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Basic boolean Operators

Operator Meaning E=3 xample

== equal (4-2)==(8-6)

!= Not equal 3 !=2 is true but 4!=(6-2) is false

> Greater than (3>2) is true

>= Greater or equal (3>=2) is true

< Less than (3<2) is false

<= Less or equal (3<=4) is true

Page 28: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Statements & Blocks

• A simple statement is a command terminated by a semi-colon:name = “Fred”;

• A block is a compound statement enclosed in curly brackets:{

name1 = “Fred”; name2 = “Bill”;

}• Blocks may contain other blocks

Page 29: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Flow of Control

• Java executes one statement after the other in the order they are written

• Many Java statements are flow control statements:Alternation: if, if else, switchLooping: for, while, do whileEscapes: break, continue, return

Page 30: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

If – The Conditional Statement

• The if statement evaluates an expression and if that evaluation is true then the specified action is taken

if ( x < 10 ) x = 10;

• If the value of x is less than 10, make x equal to 10• It could have been written:

if ( x < 10 )x = 10;

• Or, alternatively:if ( x < 10 ) { x = 10; }

Page 31: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

If… else

• The if … else statement evaluates an expression and performs one action if that evaluation is true or a different action if it is false. if (x != oldx) {

System.out.print(“x was changed”);

}

else {

System.out.print(“x is unchanged”);

}

Page 32: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Nested if … else

if ( myVal > 100 ) {if ( remainderOn == true) {

myVal = mVal % 100;}else {

myVal = myVal / 100.0;}

}else{System.out.print(“myVal is in range”);

}

Page 33: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

else if

• Useful for choosing between alternatives:if ( n == 1 )

{

// execute code block #1

}

else if ( j == 2 )

{

// execute code block #2

}

else {

// if all previous tests have failed, execute code block #3

}

Page 34: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

A Warning…

WRONG!

if( i == j )if ( j == k )

System.out.print(

“i equals k”);

else

System.out.print(

“i is not equal to j”);

CORRECT!

if( i == j ) {

if ( j == k )

System.out.print(

“i equals k”);

}

else

System.out.print(“i is not equal to j”);

// Correct!

Page 35: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

The switch Statementswitch ( n ) {case 1:

// execute code block #1break;

case 2:// execute code block #2break;default:// if all previous tests fail

then //execute code block #4

break;}

Page 36: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Summary

• Different data type • Declarations• Arithmetic operators• Parse string to integer.• Boolean operators• Assignments• If statement • Switch statement.

Page 37: Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Home work

• Practice with all the exercise on the website • Read chapter 4 on the study guide:– Redo all the examples– Do all the exercises.