1. Exam Points
Assign a valueto a variablebased on its data type.
- int x = 10;
- double y = 2.3;
- boolean z = true;
- String name =
"Annabelle";- You’d better write clearly the
valuebased on its typeto avoid mistakes.
- Ex. double a = 10/4; (
a = 2.0)- Ex. int b = 10 / 2; (
b = 5)
2. Knowledge Points
(1) Assignment Operator
- The
assignment operator =is used to initialize or change the value stored in a variable.- int x = 10;
- double y = 2.3;
- String name = "Annabelle";
- boolean isEven = false;
Assigna propervalueto each variablebased on its data type.- Note:
- A variable must be
assigneda valuebeforebeingused. Reference typescan be assigned a new object or null if there is no object.- Dog d1 = new Dog(); // d1 refers to a newly created object
- Dog d2 = null; // d2 refers to null, not refering to any object
nullcannotbe assignedto a primitive data type variable.
- A variable must be
final variables: final variables can not be changed once initialized.- The
finalkeyword is used to declare aconstant(常量)that can not be changed.int x = 10; // x is a variable, so its value can be changed x = 20; // Y is a constant, its value can not be changed final int Y = 20; // we normally use all uppercase to name a constant final int ERROR_CODE = 404;
(2) Input
Inputcan come in a variety of forms, such as tactile, audio, visual, or text.- The
Scannerclass is one way to obtain text input from the keyboard. - Since any specific form of input from the user is
outside the scopeof the AP CSA exam, so we will not talk about it in details.