140 likes | 483 Views
FP301 OBJECT ORIENTED PROGRAMMING. FP301 Object Oriented Programming. 2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE . 2.2 Declaring, initializing and use variable. FP301 OBJECT ORIENTED PROGRAMMING. Learning Outcome Subtopic 2.2. FP301 OBJECT ORIENTED PROGRAMMING. VARIABLES.
E N D
FP301 OBJECT ORIENTED PROGRAMMING FP301 Object Oriented Programming
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE 2.2 Declaring, initializing and use variable
FP301 OBJECT ORIENTED PROGRAMMING Learning Outcome Subtopic 2.2
FP301 OBJECT ORIENTED PROGRAMMING VARIABLES • Is the name given to the memory location where a value is stored. • The name given to the variable is called as identifier.
FP301 OBJECT ORIENTED PROGRAMMING 4 CATEGORIES OF PRIMITIVE DATA TYPES
FP301 OBJECT ORIENTED PROGRAMMING 4 CATEGORIES OF PRIMITIVE DATA TYPES
FP301 OBJECT ORIENTED PROGRAMMING Choose an appropriate data type to represent variable noOfGoals; ----------------------------------- ----------------------------------- priceOfBall; ----------------------------------- averageAge; ----------------------------------- receivedStatus;
FP301 OBJECT ORIENTED PROGRAMMING Illustrate local variables and constant stored in memory • Local variable: • • Variables that are defined inside a method and are called local, automatic, temporary, or stack variables • • Variables that are created when the method is executed are destroyed when the method is exited • only visible to the methods in which they are declared • they are not accessible from the rest of the class • Constant: • A constant is a name for a memory location that stores a value that cannot • be changed from its initial assignment. • Syntax : final <type> <identifier> = <value>;
FP301 OBJECT ORIENTED PROGRAMMING Illustrate local variables and constant stored in memory • Represent with a rectangle. • Place value (if present) inside the rectangle. • Place type and name above or beside rectangle. • Example : int sum=23; int sum 23 23 int sum • If the variable is a named constant (that is, declared with the modifier final), then the border of the rectangle is made thicker. • This thicker border is meant to suggest that the value of the constant cannot be changed. • Example : final double PI=14.5; double PI 14.5
FP301 OBJECT ORIENTED PROGRAMMING Cont.. • When you declare a variable, Java reserves memory locations of sufficient size to store the variable type. • The actual data values will be stored in these memory locations. • a variable can store only one value at any one time x int x; x = 5; x = 10; the value of x is 10 because this was the last value assigned to x. x 5 x 10
FP301 OBJECT ORIENTED PROGRAMMING Declare variables and assign value to variable • To initialize a variable we use an assignment statement. • To give a variable a value, the left side is the name of the variable and • the right side is the value: • Example : • numPlayers = 12; //numPlayers is assigned 12 • numPlayers= numPlayers + 2; //numPlayers is now 14
FP301 OBJECT ORIENTED PROGRAMMING Identify the variable naming convention Rules: • Variable identifiers must start with either an uppercase or lowercase letter, an underscore (_), or a dollar sign ($). • Variable identifiers cannot contain punctuation ,spaces, or dashes. • Java technology keywords cannot be used. • Begin each variable with a lowercase letter; subsequent words should be capitalized, such as myVariable. • Choose names that are mnemonic and that indicate to the casual observer the intent of the variable.
FP301 OBJECT ORIENTED PROGRAMMING Assign a value to a variable • Example: double price = 12.99; • Example (boolean): booleanisOpen = false; • Assigning literal values: int ID = 0; float pi = 3.14F; char myChar = ’G’; booleanisOpen = false; • Assigning the value of one variable to another variable: int ID = 0; intsaleID = ID;
FP301 OBJECT ORIENTED PROGRAMMING Declare a constant • Variable (can change): double salesTax = 6.25; • Constant (cannot change): final double SALES_TAX = 6.25; • Constants should be capitalized with words separated by an underscore (_).