Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Identifiers and Variables Lecture 4 Based on Slides of Dr. Norazah Yusof 1 Identifiers All the Java components —classes, variables, and methods—need names. In Java these names are called identifiers, and, there are rules for what constitutes a legal Java identifier. Beyond what's legal, Java programmers (and Sun) have created conventions for naming methods, variables, and classes. 2 Rules for naming Identifiers Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number! After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers. Identifier can be any length. In practice, there is no limit to the number of characters an identifier can contain. You can't use a Java keyword as an identifier. (See Appendix A, “Java Keywords,” for a list of reserved words). Identifiers in Java are case-sensitive; utm and Utm are two different identifiers. Identifier cannot be true, false or null. 3 Sun's Java Code Conventions The naming standards that Sun recommends: Classes and interfaces The first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be uppercase (a format that's sometimes called "camelCase"). For classes, the names should typically be nouns. For example: Animal Account PrintWriter For interfaces, the names should typically be adjectives like Runnable Serializable 5 Sun's Java Code Conventions Methods The first letter should be lowercase, and then normal camelCase rules should be used. In addition, the names should typically be verb-noun pairs. For example: getBalance doCalculation setCustomerName calcArea 6 Sun's Java Code Conventions Variables Like methods, the camelCase format should be used, starting with a lowercase letter. Sun recommends short, meaningful names, which sounds good to us. Some examples: nama buttonWidth accountBalance myString 7 Sun's Java Code Conventions Constants Java constants are created by marking variables static and final. They should be named using uppercase letters with underscore characters as separators: MIN__HEIGHT 8 Programming Style and Documentation Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles 9 Appropriate Comments Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Include your name, class section, instructor, date, and a brief description at the beginning of the program. 10 Variables Variables are used to store data in a program. There are two types of variables in Java: 1. 2. Primitives Reference variables 11 Primitives Variables 1. 2. 3. 4. 5. A primitive can be one of eight types: char, boolean, byte, short, int, long, double, or float. Once a primitive has been declared, its primitive type can never change, although in most cases its value can change. Primitive variables can be declared as class variables (statics), instance variables, method parameters, or local variables. One or more primitives, of the same primitive type, can be declared in a single line. Examples of primitive variable declarations: i. char huruf; ii. boolean myBooleanPrimitive; iii. int x, y, z; // declare three int primitives iv. double area; 12 Declaring primitive type Example of declaring Java variables with primitive types int numberOfBeans; double oneWeight, totalWeight; The declaration of a variable can be combined with its initialization via an assignment statement int count = 0; double distance = 55 * .5; char grade = 'A'; Note that some variables can be initialized and others can remain uninitialized in the same declaration int initialCount = 50, finalCount; 13 14 Reference Variables 1. 2. 3. 4. 5. 6. A reference variable is used to refer to (or access) an object. A reference variable is declared to be of a specific type and that type can never be changed. A reference variable can be used to refer to any object of the declared type, or of a subtype of the declared type (a compatible type). Reference variables can be declared as static variables, instance variables, method parameters, or local variables. One or more reference variables, of the same type, can be declared in a single line. Examples of reference variable declarations: i. Object o; ii. Animal myNewPetReferenceVariable; iii. String s1, s2, s3; //declare three String vars. 15 Instance Variables 1. 2. 3. Instance variables are defined inside the class, but outside of any method, and are only initialized when the class is instantiated. Instance variables are the fields that belong to each unique object. For example, the following code defines fields (instance variables) for the radius of circle objects: public class Circle extends Object //Class header { private double radius; 4. 5. Each circle instance will know its own radius. In other words, each instance can have its own unique values for this field. The term "field," "instance variable," "property," or "attribute," mean virtually the same thing. 16 Final Variables Declaring a variable with the final keyword makes it a constant. 2. Constant represent permanent data that never changes. 3. For primitives, this means that once the variable is assigned a value, the value can't be altered. 4. Syntax to declare a constant: 1. final datatype CONSTANTNAME = VALUE; 5. Example: final double PI = 3.14159; //PI as constant final int SIZE = 3; 17 Final Variables (Is it legal?) class Roo { final int size = 42; public void changeSize() { size = 16; } } 18 Final Variables 1. A reference variable marked final can't ever be reassigned to refer to a different object. 2. The data within the object can be modified, but the reference variable cannot be changed. 3. In other words, a final reference still allows you to modify the state of the object it refers to, but you can't modify the reference variable to make it refer to a different object. 19 Variable scoping There are four basic scopes: 1. Static variables have the longest scope; they are created when the class is loaded, and they survive as long as the class stays loaded in the JVM. 2. Instance variables are the next most long-lived; they are created when a new instance is created, and they live until the instance is removed. 3. Local variables are next; they live as long as their method remains on the stack. As we'll soon see, however, local variables can be alive, and still be "out of scope". 4. Block variables live only as long as the code block is executing. 20