Download presentation
Presentation is loading. Please wait.
Published byRoderick Tyler Modified over 9 years ago
1
Primitive variables Android Club 2015
2
2 types of variables: Primitive single value (starts with uppercase letter) Complex multiple value (starts with lowercase letter)
3
Primitive data type int (0, 5, 87) boolean (true/false) char (‘a’, ‘b’, ‘c’)
4
Primitive: declaration [DATA_TYPE] [NAME]; int amount; boolean isOpen; char grade;
5
Primitive: initialization [DATA_TYPE] [NAME] = [INITIAL VALUE] int amount = 5; boolean isOpen = true; char grade = ‘a’;
6
Primitive: practice Example: int myNum; myNum = 5; System.out.println(myNum); Do this for char and boolean
7
Primitive: practice 2 Example: int myNum = 5; System.out.println(myNum); Do this for char and boolean
8
Primitive: practice 3 Name: Joe Richard Score: 30 Grade: D Passed: false
9
Primitive: numeric Data TypeBitsMinimumMaximumExample byte8-1281271 short16-32 7683276710 int32-2 147 483 6482 147 483 64710 long64-9,22E+189,22E+18100L float322 -149 (2-2 -23 )·2 127 150.5f double642 -1074 (2-2 -52 )·2 1023 150.5d
10
Numeric: practice 6: byte, short, int, long, float, double A. Declare: int myInt; B. Initialize: myInt = 5; C. Print: System.out.println(myInt);
11
Convert numeric Upward – SAFE (byte -> double) Downward – UNSAFE (double -> byte)
12
Upward convert: example short myShort = 256; System.out.println("myShort:"+myShort); float myFloat = myShort; System.out.println("myFloat:"+myFloat);
13
Upward convert: practice Create one short: 256 Convert it to float Print result
14
Downward convert: example double myDouble = 25.99; System.out.println("myDouble:"+myDouble); int myInt = (int)myDouble; System.out.println("myFloat:"+myInt);
15
Downward convert: practice Declare and initialize double: 8888,88d Convert double to int Print result
16
Operators Assignment Equality Mathematical Conditional
17
Assigning operator = assign
18
= example int x = 60; x = 50; int y=40; x=y;
19
= practice Create x which equals to 100 Create y which equals to 200 Then x equals to y Print
20
Mathematical operators + add - minus * multiply / divide % reminder
21
+ example int x = 60; int y=40; int z = x+y+10;
22
+ practice Create x which equals to 100 Create y which equals to 200 Create z which is sum of x and y Print
23
++ example int x = 5; x++;
24
++ practice Create x which equals to 8 Increment it by 1 Print
25
- example int x = 60; int y=40; int z = x-y-10;
26
- practice Create x which equals to 100 Create y which equals to 200 Create z which is y minus x Print
27
-- example int x = 5; x--;
28
-- practice Create x which equals to 17 Decrement it by 1 Print
29
Prefix and postfix: ++-- int x = 5; System.out.println(++x); int y = 5; System.out.println(y++);
30
* example int x = 100; int y = 2; int z = x*y;
31
* practice Create x which equals to 100 Create y which equals to 2 Create z which is x multiplied by y Print
32
/ example int x = 100; int y = 2; int z = x/y;
33
/ practice Create x which equals to 100 Create y which equals to 2 Create z which is x divided by y Print
34
% example int x = 13; int y = x % 10;
35
% practice Create x which equals to 100 Create y which equals to reminder of 100 divided by 7 Print
36
=[SOME OPERATOR] =+ =- =* =/
37
Complex data type String (“Joe Richard”, “Android”) Date Any object
38
Comparing operators == equal (do not hesitate with = assign) != not equal > more than >= more or equals < less than <= less or equals instanceof
39
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"); }
40
Comparing: practice Create x = 100; Create y = 200; Find which is less
41
Conditional operators && AND || OR
42
&& vs || true && true = true true && false = false true || true = true true || false = true
43
Complex: declaration [DATA_TYPE] [NAME]; String name; String title; Date today; Date expireDate;
44
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();
45
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));
46
Homework: Update CC - + * / %
47
Questions? What did not you understand?
48
Review Chapter 4: Using Primitive Data Types
49
Thank you! Thank you for your attention!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.