Primitive variables Android Club 2015. 2 types of variables: Primitive single value (starts with...

49
Primitive variables Android Club 2015

Transcript of Primitive variables Android Club 2015. 2 types of variables: Primitive single value (starts with...

Primitive variables

Android Club 2015

2 types of variables:

• Primitive single value (starts with uppercase letter)

• Complex multiple value (starts with lowercase letter)

Primitive data type

• int (0, 5, 87)• boolean (true/false)• char (‘a’, ‘b’, ‘c’)

Primitive: declaration

• [DATA_TYPE] [NAME];

• int amount;• boolean isOpen;• char grade;

Primitive: initialization

• [DATA_TYPE] [NAME] = [INITIAL VALUE]

• int amount = 5;• boolean isOpen = true;• char grade = ‘a’;

Primitive: practice

Example:int myNum;myNum = 5;System.out.println(myNum);

Do this for char and boolean

Primitive: practice 2

Example:int myNum = 5;System.out.println(myNum);

Do this for char and boolean

Primitive: practice 3

• Name: Joe Richard• Score: 30• Grade: D• Passed: false

Primitive: numeric

Data Type Bits Minimum Maximum Example

byte 8 -128 127 1

short 16 -32 768 32767 10

int 32 -2 147 483 648 2 147 483 647 10

long 64 -9,22E+18 9,22E+18 100L

float 32 2-149 (2-2-23)·2127 150.5f

double 64 2-1074 (2-2-52)·21023 150.5d

Numeric: practice

• 6: byte, short, int, long, float, double• A. Declare: int myInt;• B. Initialize: myInt = 5;• C. Print: System.out.println(myInt);

Convert numeric

• Upward – SAFE (byte -> double)• Downward – UNSAFE (double -> byte)

Upward convert: example

short myShort = 256;System.out.println("myShort:"+myShort);

float myFloat = myShort;System.out.println("myFloat:"+myFloat);

Upward convert: practice

• Create one short: 256• Convert it to float• Print result

Downward convert: example

double myDouble = 25.99;System.out.println("myDouble:"+myDouble);

int myInt = (int)myDouble;System.out.println("myFloat:"+myInt);

Downward convert: practice

• Declare and initialize double: 8888,88d

• Convert double to int• Print result

Operators

• Assignment• Equality• Mathematical• Conditional

Assigning operator

• = assign

= example

int x = 60;x = 50;

int y=40;x=y;

= practice

• Create x which equals to 100• Create y which equals to 200• Then x equals to y• Print

Mathematical operators

• + add• - minus• * multiply• / divide• % reminder

+ example

int x = 60;int y=40;int z = x+y+10;

+ practice

• Create x which equals to 100• Create y which equals to 200• Create z which is sum of x and y• Print

++ example

• int x = 5;• x++;

++ practice

• Create x which equals to 8• Increment it by 1• Print

- example

int x = 60;int y=40;int z = x-y-10;

- practice

• Create x which equals to 100• Create y which equals to 200• Create z which is y minus x• Print

-- example

• int x = 5;• x--;

-- practice

• Create x which equals to 17• Decrement it by 1• Print

Prefix and postfix: ++--

int x = 5;System.out.println(++x);

int y = 5;System.out.println(y++);

* example

• int x = 100;• int y = 2;• int z = x*y;

* practice

• Create x which equals to 100• Create y which equals to 2• Create z which is x multiplied by y• Print

/ example

• int x = 100;• int y = 2;• int z = x/y;

/ practice

• Create x which equals to 100• Create y which equals to 2• Create z which is x divided by y• Print

% example

• int x = 13;• int y = x % 10;

% practice

• Create x which equals to 100• Create y which equals to reminder of

100 divided by 7• Print

=[SOME OPERATOR]

• =+• =-• =*• =/

Complex data type

• String (“Joe Richard”, “Android”)• Date• Any object

Comparing operators

• == equal (do not hesitate with = assign)

• != not equal• > more than• >= more or equals• < less than• <= less or equals• instanceof

Comparing example

int x = 5;int y = 9;

if (x > y) {System.out.println("x more than y");

} else {System.out.println("x less than y");

}

Comparing: practice

• Create x = 100;• Create y = 200;• Find which is less

Conditional operators

• && AND• || OR

&& vs ||

• true && true = true• true && false = false• true || true = true• true || false = true

Complex: declaration

[DATA_TYPE] [NAME];String name;String title;Date today;Date expireDate;

Complex: initialization

[DATA_TYPE] [NAME] = new [DATA_TYPE](PARAMETER);

• String title = new String(“Alchemist”);• String title= “Alchemist”;• String country = new

String(“Australia”);• Date today = new Date();

CRAZY CALCULATOR

System.out.println("Enter x");Scanner scanner = new Scanner(System.in);int x = scanner.nextInt();

System.out.println("Enter y");int y = scanner.nextInt();

System.out.println("x+y="+(x+y));

Homework: Update CC

• -• +• *• /• %

Questions?

• What did not you understand?

Review

• Chapter 4: Using Primitive Data Types

Thank you!

• Thank you for your attention!