Primitive variables Android Club 2015. 2 types of variables: Primitive single value (starts with...
-
Upload
roderick-tyler -
Category
Documents
-
view
220 -
download
1
Transcript of Primitive variables Android Club 2015. 2 types of variables: Primitive single value (starts with...
2 types of variables:
• Primitive single value (starts with uppercase letter)
• Complex multiple value (starts with lowercase letter)
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: 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);
Upward convert: example
short myShort = 256;System.out.println("myShort:"+myShort);
float myFloat = myShort;System.out.println("myFloat:"+myFloat);
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
+ practice
• Create x which equals to 100• Create y which equals to 200• Create z which is sum of x and y• Print
- practice
• Create x which equals to 100• Create y which equals to 200• Create z which is y minus x• Print
* practice
• Create x which equals to 100• Create y which equals to 2• Create z which is x multiplied by y• Print
/ practice
• Create x which equals to 100• Create y which equals to 2• Create z which is x divided by y• Print
% practice
• Create x which equals to 100• Create y which equals to reminder of
100 divided by 7• Print
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");
}
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));