Primitive and Reference Variables

Primitive and Reference Variables

PRIMITIVE AND REFERENCE VARIABLES Java divides all variables into two categories: primitive and reference variables. A primitive variable has as its declared data type one of the eight primitive data types. These are data types that are built into the Java language and identified by Java keywords. Java Built-In (“Primitive”) Data Types Keyword Meaning byte short Holds an integer value – that is, a whole number with no int fractional part. long char Holds a single keyboard character. boolean Holds either true or false. float Holds a floating-point value – that is, one that may have a double fractional part. A primitive variable is a direct reference to its datum, meaning that the datum is stored immediately within the variable’s associated memory location. Example double radius = 6.25; radius 6.25 double diameter = 2 * radius; diameter 12.50 int topScore = 100; topScore 100 A reference variable has as its declared data type a Java class. Example java.util.Scanner in; // reference to a Scanner object String name; // reference to a String object Die bone; // reference to a Die object Primitive and Reference Variables Page 1 A reference variable is an indirect reference to an object, meaning that it holds the address of where the object is stored in the computer’s internal memory. The JVM retrieves the object in two steps: (1) it uses the reference variable to retrieve the object’s address and (2) uses the address to retrieve the object. A reference variable is often called a pointer because it points to another memory address. For this reason, programmers often draw it as a box with an arrow (representing the address) to another box representing the object. Example This picture depicts reference variables holding the addresses of objects that are located elsewhere in memory. Die object faceUp 3 roll( ) String object Herb Green bone name Scanner object in Exercises These exercises refer to the Die and Timer classes found in the topic Objects and Classes. For each of the following, indicate whether the variable is a primitive or a reference variable. 1. Timer clock; 2. int apples; 3. byte score; 4. Spinner wheel; 5. long gnp; 6. Double temperature; 7. double taxRate; Primitive and Reference Variables Page 2 For each of the following, indicate whether the variable is a primitive or a reference variable. 8. Die bones; 9. boolean married; 10. Scanner input; 11. char symbol; 12. float price; 13. Character letter; 14. short numChildren; 15. String name; For each of the following, draw a conceptual picture of the resulting memory such as is shown in this topic. 16. double taxRate = 0.06; double tax = taxRate * 1_000; 17. int yearsInOffice = 4; 18. boolean married = true; 19. char letter = 'A'; 20. int quantity = 5; double price = quantity * 22.50; 21. Timer clock = new Timer( ); 22. Die bone = new Die( ); Primitive and Reference Variables Page 3.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    3 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us