click below
click below
Normal Size Small Size show me how
Chapter 2
Using Data
Term | Definition |
---|---|
Constant Data Item | When a data item's value cannot be changed while a program is running. For example, when you include the following statement in a Java class, the number 459 is a constant: System.out.println(459); |
Literal Constant | In the code, System.out.println(459);, every time an application containing the constant 459 is executed, the value 459 is displayed. Programmers refer to the number 459 as a literal constant because its value is taken literally at each use |
Numeric Constant | In the code, System.out.println(459);, the number 459, as opposed to being a character or string constant, is a numeric constant. |
Unnamed Constant | In the code, System.out.println(459);, the number 459 is an unnamed constant as opposed to being a named one, because no identifier is associated with it |
Variable | A named memory location that can store a value. Can hold only one value at a time, but the value it holds can change. Can be used instead of using constant data. |
Data Type | Describes the type of data that can be stored there, how much memory the item occupies, and what types of operations can be performed on the data. Every data item in Java, whether variable or constant, has a data type |
Primitive Type | A simple data type. Java provides eight primitive types of data. Simple and uncomplicated |
1. Primitive Data Types in Java | 1. Keyword(byte)..Description(Byte-length integer)... 2. Keyword(short)..Description(Short integer)... 3. Keyword(int)..Description(Integer)... 4. Keyword(long)..Description(Long integer) |
2. Primitive Data Types in Java | 5. Keyword(float)..Description(Single-precision floating point)... 6. Keyword(double)..Description(Double-precision floating point)... 7. Keyword(char)..Description(A single character)... 8. Keyword(boolean)..Description(A Boolean value(true or false)) |
Reference Types | More complex data types which hold memory addresses. Primitive data types serve as the building blocks for reference types |
1. Variable Declaration | A statement that reserves a named memory location and includes the following... 1. A data type that identifies the type of data that the variable will hold.. 2. An identifier that is the variable's name.. |
2. Variable Declaration | 3. An optional assignment operator and assigned value, if you want a variable to contain an initial value.. 4. An ending semicolon |
Strongly Typed Language | A language in which each variable has a well-defined data type that limits the operations you can perform with it; strong typing implies that all variable must be declared before they can be used. Java is a strongly typed language |
Naming Variables | Variable names conventionally begin with lowercase letters to distinguish them from class names. You name variables using same naming rules as you do for legal class identifiers. Variables names must start with a letter and cannot be a reserved keyword |
Camel Casing | Beginning an identifier with a lowercase letter and capitalizing subsequent words within the identifier |
Assignment Operator | The equal sign(=). Has right to left associativity. Any value to the right of the equal sign is assigned to the variable on the left of the equal sign. Ex: int myAge = 25; |
Initialization and Assignment | An assignment made when you declare a variable is an initialization; an assignment made later is simply an assignment. In following example, the first statement that follows is initialization, the second is an assignment. Ex: int myAge = 25 myAge = 42; |
Associativity | The order in which values are used with operators. The associativity of every operator is either right-to-left of left-to-right. |
lvalue | An identifier that can appear on the left side of an assignment operator. A variable can be used as an lvalue or an rvalue |
rvalue | An item that can appear only on the right side of an assignment operator. A literal number can only be an rvalue. A numeric constant such as 25 is not an lvalue, only an rvalue |
Uninitialized Variable | When you declare a variable within a method but do not assign a value to it. Ex: int myAge; |
Garbage Value | The unknown value that an uninitialized variable contains. If you attempt to display garbage or use it as part of a calculation, you receive an error message stating that the variable might not have been initialized |
1. Named Constant | A variable is a named memory location for which the contents can change. If a named location's value should not change during the execution of a program, you can create it to be a name constant. Also called a symbolic constant |
2. Named Constant | Similar to a variable in that is has a data type, a name, and a value. A named constant differs from a variable in the following ways... 1. In its declaration statement, the data type of a named constant is preceded by the keyword final. |
3. Named Constant | 2. A named constant can be assigned a value only once, and then can never be changed. Usually you initialize a named constant when you declare it; if you do not, it is known as a blank final, and you can assign a value later. |
4. Named Constant | You can assign a value to final only once, this must be assigned before the constant is used.. 3. Although not a requirement, named constants conventionally are given identifiers using all uppercase letters, using underscores as need to separate words |
Magic Number | A value that does not have immediate, intuitive meaning or a number that cannot be explained without additional knowledge. Avoiding magic number helps you provide internal documentation for your programs |
Data Item's Scope | The area in which it is visible to a program and in which you can refer to it using its simple identifier. A variable or a constant is in a scope from the point it is declared until the end of the block of code in which the declaration lies |
Block of Code | The code contained between a set of curly braces. If you declare a variable or constant within a method, it can be used from its declaration until the end of the method |
Concatenation | In Java, when a numeric variable is concatenated to a String using the plus sign, the entire expression becomes a String. Ex: System.out.println("Next bill: October " + billingDate); |
Integer | A whole number without decimal places. In Java, you can use variables of types byte, short, int, and long to store(or hold) integers |
int Data Type | The most commonly used integer type. A variable of type int,can hold any whole number value from -2,147,483,648 to +2,147,483,647 |
Types byte, short, and long | All variations of the integer type. The byte and short types occupy less memory and can hold only smaller values; the long type occupies more memory and can hold larger values |
1. Limits on Integer Values by Type | 1. Type(byte)..Min Value(-128)..Max Value(127)..Size in Bytes(1)... 2. Type(short)..Min Value(-32,768)..Max Value(32,767)..Size in Bytes(2)... 3. Type(int)..Min Value(-2,147,483,648)..Max Value(2,147,483,647)..Size in Bytes(4) |
2. Limits on Integer Values by Type | 4. Type(long).. Min Value( -9,223,372,036,854,775,808).. Max Value( 9,223,372,036,854,775,807).. Size in Bytes(8) |
Using Type long | If a constant higher than 2,147,483,647 is needed, you must follow the number with the letter L or l to indicate long. Ex: long mosquitosInTheNorthWoods = 2444555888L;. No special notation is needed to store a numeric constant in an int, byte, or a short |
boolean Variable | Boolean logic is based on true-or-false comparisons. Whereas an int variable can hold millions of different values, a boolean variable can hold only one of two values - true or false. Ex: boolean isItPayday = false;...boolean areYouBroke = true; |
Relational Operators(Comparison Operators) | Compares two items. Java supports six relational operators that are used to make comparisons |
1. Java's Relational Operators | 1. Operator(<)..Description(less than)..True(3<8)..False(8<3)... 2. Operator(>)..Description(greater than)..True(4>2)..False(2>4)... 3. Operator(==)..Description(equal to)..True(7==7)..False(3==9)... |
2. Java's Relational Operators | 4. Operator(<=)..Description(less than or equal to)..True(5<=5)..False(8<=6)... 5. Operator(>=)..Description(greater than or equal to)..True(7>=3)..False(1>=2)... 6. Operator(!=)..Description(not equal to)..True(5!=6)..False(3!=3) |
Floating-Point Numbers | Contain decimal positions. Java supports two floating-point data types: float and double. Floating point numbers can be imprecise |
Float | A data type that can hold floating-point value of up to six or seven significant digits of accuracy. A value in a float is a single-precision floating-point number. |
Double | A data type that requires more memory than a float and can hold 14 to 15 significant digits of accuracy. A value stored in a double is a double-precision floating-point number |
Significant Digits | Refers to the mathematical accuracy of a value |
Limits on Floating-Point Values | 1. Type(float).. Minimum( -3.4*10^38).. Maximum( 3.4*10^38).. Size in Bytes(4).. 2. Type(double).. Minimum( -1.7*10^308).. Maximum( 1.7*10^308)..Size in Bytes(8)... |
Indicating a float | To indicate that a floating-point nummeric constant is a float, you can type the letter F or f after the number. Ex: float pocketChange = 4.87F; |
Indicating a double | You can type the letter D or d after a floating-point constant to indicate it is a double, but even without the D, the value is stored as a double by default. |
char Data Types | Used to hold single characters. You place constant character values within single quotation marks because the computer stores characters and integers differently. Ex: char middleInitial = 'M';..char gradeInChemistry = 'A';...char aStar = '*'; |
Characters | A character can be any letter - upper or lowercase. It might also be a punctuation mark or digit. A character that is a digit is represented in computer memory differently than a numeric value digit. Ex: char aCharValue = '9';...int aNumValue = 9; |
String | A data structure that is used to store a string of characters. In Java, String is a built-in class that provides you with the means for storing and manipulating character strings. Written between double quotation marks. Ex: String firstName = "Audrey"; |
1. Common Escape Sequence | 1. Escape Sequence(\b)..Description(Backspace; moves the cursor one space to the left)... 2. Escape Sequence(\t)..Description(Tab; moves the cursor to the next tab stop)... |
2. Common Escape Sequence | 3. Escape Sequence(\n)..Description(Newline or linefeed; moves the cursor to the beginning of the next line) 4. Escape Sequence(\r)..Description(Carriage return; moves the cursor to the beginning of the current line) |
3. Common Escape Sequence | 5. Escape Sequence(\")..Description(Double quotation mark; displays a double quotation mark)... 6. Escape Sequence(\')..Description(Single quotation mark: displays a single quotation mark) |
4. Common Escape Sequence | 7. Escape Sequence(\\)..Description( Backslash; displays a backslash character) |
Standard Input Device | To create interactive programs that accept input from a user, you can use System.in, which refers to the standard input device(normally the keyboard) |
Creating a Scanner Class | To create a Scanner object and connect it to the System.in object, you write a statement similar to the following... Scanner inputDevice = new Scanner(System.in); |
Assignment Operator in Scanner Declaration Statement | Assigns a value to the new object - that is, its memory address - to the inputDevice object in the program |
Token | A set of characters that is separated from the next set by whitespace. The Scanner class contains methods that retrieve values from the input device. Each retrieved value is a token |
1. Selected Scanner Class Methods | 1. Method(nextDouble())..Description(Retrieves input as a double)... 2. Method(nextInt())..Description(Retrieves input as an int)... 3. Method(nextLine())..Description(Retrieves the next line of data and returns is as a String)... |
2. Selected Scanner Class Methods | 4. Method(next())..Description(Retrieves the next complete token as a string)... 5. Method(nextShort())..Description(Retrieves input as a short)... 6. Method(nextByte())..Description(Retrieves input as a byte)... |
3. Selected Scanner Class Methods | 7. Method(nextFloat())..Description(Retrieves input as a float. Note that when you enter an input value that will be stored as a float, you do not type an F. The F is used only with constants coded within a program)... |
4. Selected Scanner Class Methods | 8. Method(nextLong())..Description(Retrieves input as a long. Note that when you enter an input value that will be stored as a long, you do not type an L. The L is used only with constants coded within a program) |
Echoing The Input | Repeating as output what a user has entered as input. Echoing input is a good programming practice; it helps eliminate misunderstandings when the user can visually confirm what was entered |
Prompt | A message displayed for the user that requests and describes input |
1. Using nextLine() Following One of the Other Scanner Input Methods | You can encounter a problem when you use a numeric Scanner class retrieval method or the next() method before you use the nextLine() method. If you accept numeric input prior to string input, the string input is ignored unless you take special action |
2. Using nextLine() Following One of the Other Scanner Input Methods | The solution is simple. After any next(), nextInt(), or nextDouble() call, you can add an extra nextLine() method call that will retireve the abandoned Enter key character. Then, on matter what type of input follows, the program will execute smoothly |
Keyboard Buffer | A location in memory in which characters you type using the keyboard are temporarily stored. This is also sometime called the type-ahead buffer. All keystrokes are stored in the keyboard buffer, including the Enter key |
Consumed Entry | When you accept an entry and discard it, programmers say that the entry is consumed |
Using the JOptionPane Class to Accept GUI Input | Two GUI dialog boxes from JOptionPane class that can be used to accept user input are: 1. InputDialog - Prompts the user for text input.. 2. ConfirmDialog - Asks the user a question, providing buttons that the user can click for Yes, No, and Cancel. |
Input Dialog Box | Asks a question and provides a text field in which the user can enter a response. You can create an input dialog box using the showInputDialog() method. Six versions of this method are available. |
1. The Four Arguments to showInputDialog() Boxes | Example; JOptionPane.showInputDialog(null, "What is you area code?", "Area code information", JOptionPane.QUESTION_MESSAGE);. 1. (1st part)The parent component, which is the screen component, such as a frame, in front of which the dialog box will appear. |
2. The Four Arguments to showInputDialog() Boxes | If this argument is null, the dialog box is centered on the screen.. 2. (2nd part)The message the user will see before entering a value. Usually this message is a String, but it actually can be any type of object... |
3. The Four Arguments to showInputDialog() Boxes | 3. (3rd part) The title to be displayed in the titile bar of the input dialog box.. |
4. The Four Arguments to showInputDialog() Boxes | 4. (4th part) A class field describing the type of dialog box; it can be one of the following: ERROR_MESSAGE, IINFORMATION_MESSAGE, PLAIN_MESSAGE, QUESTION_MESSAGE, Or WARNING_MESSAGE |
1. Type-Wrapper Clases | Each primitive type in Java(double, int, char, byte, short, long, float, boolean) has a corresponding class contained in the java.lang. package; like most classes, the names of these classes begin with uppercase letters. |
2. Type-Wrapper Classes | These classes are called type-wrapper classes. They include methods that can process primitive type values |
Parse | Means to break into component parts. Parsing a String converts it to its numeric alphabet. |
Converting Strings to Numeric Values | The double value is converted using the Double.parseDouble() method, and the integer is converted using the Integer.parseInt() method. Ex: Double.parseDouble(wageString);... Integer.parseInt(dependentString); |
Confirm Dialog Box | Displays the options Yes, No, and Cancel; you can create one using the showConfirmDialog() method in the JOptionPane class. Four versions of the method are available. |
showConfirmDialog() Method | Returns an integer containing one of three possible values; JOptionPane.YES_OPTION, JOptionPance.NO_OPTION, or JOptionPane.CANCEL_OPTION. |
1. Five Arguments to Use for Confirm Dialog Boxes | 1. (1st part)The parent component, which can be null.. 2. (2nd part)The prompt message.. 3. (3rd part)The title to be displayed in the title bar.. |
2. Five Arguments to Use for Confirm Dialog Boxes | 4. (4th part)An integer that indicated which option button will be shown(It should be one of the class variables YES_NO_CANCEL_OPTION or YES_NO_OPTION.).. |
3. Five Arguments to Use for Confirm Dialog Boxes | 5. (5th part)An integer that describes the kind of dialog box( It should be one of the class variables ERROR_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE, QUESTION_MESSAGE, Or WARNING_MESSAGE) |
4. Five Arguments to Use for Confirm Dialog Boxes | Example: JOptionPane.showConfirmDialog(null, "A data input error has occurred. Continue?", "Data input error", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE); |
Standard Arithmetic Operators | Used to perform calculations with values in your programs |
Operand | A value used on either side of an operator. For example, in the expression 45 + 2, the number 45 and 2 are operands |
Binary Operators | The arithmetic operators are called binary operators because they require to operands |
1. Arithmetic Operators List | 1. Operator(+)..Description(Addition).. Example(45+2, result is 47)... 2. Operator(-)..Description(Subtraction).. Example(45-2, result is 43)... 3. Operator(*)..Description(Multiplication).. Example(45*2, result is 90).... |
2. Arithmetic Operators List | 4. Operator(/)..Description(Division).. Example(45.0/2, result is 22.5 and 45/2, result is 22(not 22.5))... 5. Operator(%)..Description (Remainder(Modulus)).. Example(45%2, result is 1) |
Two Types of Division | Java supports two types of division: 1. Floating-Point Division... 2. Integer Division |
Floating-Point Division | Occurs when either or both of the operands are floating-point values. For example, 45.0 / 2 is 22.5 |
Integer Division | Occurs when both of the operands are integers. The result is an integer, and any fractional part of the result is lost. For example, the result of 45/2 is 22. |
Remainder Operator(%) | The remainder operator is most often used with two integers, and the result is an integer with the value of the remainder after division takes place |
Operator Precedence | Refers to the rules for the order in which parts of a mathematical expression are evaluated. The multiplication, division, and remainder operators have the same precedence, but a higher precedence than addition and subtraction, which have same precedence |
1. Imprecision in Floating Point Numbers | Integer values are exact, but floating-point number frequently are only approximations. Imprecision leads to several problems... 1. When you produce a floating-point output, it might not look like what you expect or want... |
2. Imprecision in Floating Point Numbers | 2. When you make comparisons with floating-point numbers, the comparisons might not be what you expect or want |
Type Conversion | The process of converting one data type to another |
Unifying Type | The type to which all operands in an expression are converted so that they are compatible with each other. When you perform arithmetic operations with operands of unlike types, Java chooses a unifying type for the result |
Implicit Conversion(Promotion) | Java performs an implicit conversion; that is, it automatically converts noncomforming operands to the unifying type when you perform arithmetic operations with operands of unlike types, |
1. Order for Establishing Unifying Data Types | When two unlike types are used in an expression, the unifying type is that one that is higher in the list. In other words, when an operand that is a type lower on the list is combined with a type that is higher; the lower is converted to the higher one |
2. Order for Establishing Unifying Data Types | Lowest(int --> long --> float --> double)Highest... Short and byte are automatically converted to int when used in expressions |
Type Casting | Forces a value of one data type to be used as a value of another type. To perform a type cast, you use a cast operator, which is created by placing the desired result type in parentheses. Can be used to purposely override the unifying type imposed by Java |
Cast Operator | Created by placing the desired result type in parentheses and used to perform a type cast. Using a cast operator is an explicit conversion. Cast operator is followed by the variable or constant to be cast |
Cast Operator Example | double bankBalance = 189.66; float weeklyBudget = (float) (bankBalance / 4); //weeklyBudget is 47.415, one-fourth of bankBalance |
Unary Operator | Unlike a binary operator that requires two operands, a unary operator uses only one operand. The cast operator is more completely called the unary cast operator. The unary cast operator is followed by its operand |
When Performing a Cast, What is Not Required | You do not need to perform a cast when assigning a value to a higher unifying type. For example, when you write a statement such as the following, java automatically promotes the integer 10 to be a double... Ex: double payRate = 10 |