|
|
|
|
|
|
|
| Variables |
|
As it was learned in the previous topic, an object stores its state in fields.
|
int currentPedalCadence = 0;
int currentSpeed = 0;
int currentGear = 1;
|
|
In the Java programming language, the terms "field" and "variable" are both used; this is a common source of confusion among new developers, since both often seem to refer to the same thing.
|
|
The Java programming language defines the following kinds of variables:
|
- Instance Variables(Non-Static Fields): Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.
- Class Variables(Static Fields): A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.
- Local Variables: Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.
- Parameters: You've already seen examples of parameters, both in the Bicycle class and in the main method of the "Hello World!" application. Recall that the signature for the main method is public static void main(String[] args). Here, the args variable is the parameter to this method. The important thing to remember is that parameters are always classified as "variables" not "fields".
|
|
Details...
|
| Naming |
| Every programming language has its own set of rules and conventions for the kinds of names that they allow to use, and the Java programming language is no different. The rules and conventions for naming your variables can be summarized as follows: |
- Variable names are case-sensitive. White space is not permitted. A variable's name can be any legal identifier - an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_". The convention, however, is to always begin variable names with a letter, not "$" or "_". Additionally, the dollar sign character, by convention, is never used at all.
- Subsequent characters may be letters, digits, dollar signs, or underscore characters. Conventions (and common sense) apply to this rule as well. When choosing a name for variables, full words should be used instead of cryptic abbreviations. Doing so will make code easier to read and understand. In many cases it will also make code self-documenting; fields named currentPedalCadence, currentSpeed, and currentGear, for example, are much more intuitive than abbreviated versions, such as s, c, and g. Also keep in mind that the name choosen must not be a keyword or reserved word.
- If the name choosen consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names currentPedalCadence and currentGear are prime examples of this convention. If variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.
|
| Java Language Keywords |
| Here's a list of keywords in the Java programming language. You cannot use any of the following as identifiers in your programs. The keywords const and goto are reserved, even though they are not currently used. true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs. |
| Keywords |
| abstract | continue | for | new | switch |
| assert*** | default | goto* | package | synchronized |
| boolean | do | if | private | this |
| break | double | implements | protected | throw |
| byte | else | import | public | throws |
| case | enum**** | instanceof | return | transient |
| catch | extends | int | short | try |
| char | final | interface | static | void |
| class | finally | long | strictfp** | volatile |
| const* | float | native | super | while |
| **not used **added in 1.2 ***added in 1.4 ****added in 5.0 |
|
| Primitive Data Types |
| The Java programming language is strongly-typed, which means that all variables must first be declared before they can be used. This involves stating the variable's type and name, as it has already been seen: |
|
int currentGear = 1;
|
| Doing so tells the program that a field named "currentGear" exists, holds numerical data, and has an initial value of "1". A variable's data type determines the values it may contain, plus the operations that may be performed on it. |
| In addition to int, the Java programming language supports seven other primitive data types, 8 in total. A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are: |
| The following class, PrimitiveDataTypesApp, creates fields with 8 primitive data types, puts default values in them, and encapsulate them thru get/set methods for each field. |
- byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).
- short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
- int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason, like where memory savings actually matters and above two types can serve the purpose, to choose something else.
- long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.
- float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section IEEE Standard for Binary Floating-Point Arithmetic. As with the recommendations for byte and short, use a float (instead of double) if memory saving is of primary concern in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, java.math.BigDecimal class should be used. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.
- double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section IEEE Standard for Binary Floating-Point Arithmetic. For decimal values, this data type is generally the default choice. As mentioned in case of float, this data type should never be used for precise values, such as currency.
- boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.
- char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65535 inclusive).
|
|
In addition to the eight primitive data types listed above, the Java programming language also provides special support for following data types:
|
- String: It is a character string. Enclosing character string within double quotes will automatically create a new String object; for example, String s = "this is a string";. String objects are immutable, which means that once created, their values cannot be changed. The String class is not technically a primitive data type, but considering the special support given to it by the language, it will probably be thought as one of the above. The more about the String class will be learnt in Simple Data Objects. Hierarchy for String Class is given below:
java.lang
java.lang.Object
java.lang.String
- Numbers: The more about the Numbers class will be learnt in Simple Data Objects.
java.lang
java.lang.Object
java.lang.Number
java.lang.Byte
java.lang.Short
java.lang.Integer
java.lang.Long
java.lang.Float
java.lang.Double
java.math.BigDecimal
java.math.BigInteger
java.util.concurrent.atomic.AtomicInteger
java.util.concurrent.atomic.AtomicLong
Package java.util.concurrent.atomic - A small toolkit of classes that support lock-free thread-safe programming on single variables.
|
|
|
|
Comments/Feedback:
Please login to participate |
OneInfoPlace server is currently busy. Please try later. Sorry for the inconvenience | | |