1 / 14

FP301 Object Oriented Programming

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.

ban
Download Presentation

FP301 Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FP301 OBJECT ORIENTED PROGRAMMING FP301 Object Oriented Programming

  2. 2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE 2.2 Declaring, initializing and use variable

  3. FP301 OBJECT ORIENTED PROGRAMMING Learning Outcome Subtopic 2.2

  4. 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.

  5. FP301 OBJECT ORIENTED PROGRAMMING 4 CATEGORIES OF PRIMITIVE DATA TYPES

  6. FP301 OBJECT ORIENTED PROGRAMMING 4 CATEGORIES OF PRIMITIVE DATA TYPES

  7. FP301 OBJECT ORIENTED PROGRAMMING Choose an appropriate data type to represent variable noOfGoals; ----------------------------------- ----------------------------------- priceOfBall; ----------------------------------- averageAge; ----------------------------------- receivedStatus;

  8. 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>;

  9. 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

  10. 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

  11. 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

  12. 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.

  13. 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;

  14. 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 (_).

More Related