Java Flashcards
What is an identifier?
A letter followed by any number of letters and numbers, case sensitive, that are used to identify classes, variables, methods, etc.
What is a name?
A series of identifiers separated by a dot (.) such as, “System.out”.
Java bytecode
A Java compiler translates Java source code into Java bytecode to be used by the Java interpreter to execute it on a specific machine. (Unlike machine code, Java bytecode is not tied to a particular processor.)
Problem Solving Steps
- Understand the problem.
- Design a solution.
- Consider alternatives to the solution and refine the solution.
- Implement the solution.
- Test the solution and fix any problems that exist.
Software Development Activities
- Establish the requirements.
- Creating a design.
- Implementing the design.
- Testing.
object
A fundmental element in a program. Every object has:
- A state or state of being - as in the fundamental characteristics that currently define the object.
- A set of behaviors - The activities associated with the object.
attributes
The values the object stores internally, which may represent primitive data or other objects. Collectively, the values of an object’s attributes define its current state.
method
A group of programing statements that have been given a name.
class
An object is defined by a class
encapsulation
An object that protects and manages its own information. An object that is self-governing.
inheritance
The definition of one class that is based on another class that already exists.
Common characteristics are defined in high-level classes, and specific differences are defined in derived classes.
Polymorphism
We can refer to multiple types of related objects over time in consistent ways
System.out.print(“ “);
Prints the information between the parentheses to the screen and does not advance to the next line.
System.out.println(“ “);
Prints the information between the parentheses to the screen and advance to the next line.
string concatenation
The use of an operator to append one string to the end of another, such as:
“The only stupid question is “ + “the one that’s not asked.”
Escape Sequences
\b
backspace
Escape Sequences
\t
tab
Escape Sequences
\n
newline
Escape Sequences
\r
carriage return
Escape Sequences
"
double quote
Escape Sequences
'
single quote
Escape Sequences
\
backslash
variable
A name for a location in memory used to hold a data value.
Local Variable Declaration
Consists of a Type followed by a list of variable names.
If the final modifier preceeds the declaration, the identifiers are declared as named constants whose values cannot be changed once they are set.
assignment statement
Identifier = Expression;
total = 57;
Primitive Data Types
- four subsets of integers
- byte
- short
- int
- long
- two floating point data types
- float
- double
- character
- boolean
byte
Storage: 8 bits
Min value: -128
Max value: 127
short
Storage: 16 bits
Min value: -32,768
Max value: 32,767
int
Storage: 32 bits
Min value: -2,147,483,648
Max value: 2,147,483,647
long
Storage: 64 bits
Min value: -9,223,372,036,854,775,808
Max value: 9,223,372,036,854,775,807
float
Storage: 32 bits
Min value: Approximately -3.4E+38
with 7 significant digits
Max value: Approximately -3.4E+38
with 7 significant digits
constants
Identifiers similar to variables except that they hold a particular value for the duration of their existence. They should be in the form of:
final int MAX_OCCUPANCY = 427;
characters
A character literal is expressed in Java with single quotation characters, as in ‘b’ or ‘J’
Each character in a character set, such as ASCII has a number value that can be used in if statements.
control characters
Nonprinting or invisible characters. Examples:
carriage return, null, and end-of-text marks
boolean
Only has two values: true and false
expression
A combination of one or more opperators and operands that usually perform a calculation. The value calculated does not have to be a number but often is.
Arithmetic operations
addition (+)
subtraction (-)
multiplication (*)
division (/)
remainder(%) - The result is the remainder of n/m in the form of n % m and has the sign of the numerator.
Operator Precedence
multiplication, division, and remainder have equal precidence and are performed before addition and subtraction.
Addition and subtraction have equal precidence.
Increment Operator
++
Adds 1 to any integer or floating point value
count++;
Decrement Operator
–
Subtracts 1 from any integer or floating point value
count–;
Assignment Operators
Examples:
total += 5; is the same as total = total + 5;
total -= 5; is the same as total = total - 5;
total *= 5; is the same as total = total * 5;
total /= 5; is the same as total = total / 5;
total %= 5; is the same as total = total % 5;
Data Conversion
Converting a data value from one type to another type. However, the safest way is to go from a smaller data type to a larger one. If not, some bits are lost in the conversion.
Casting
A Java operator that is specified by a type name in parentheses:
dollars = (int) money;
Scanner class
Part of the standard Java class library, provides convenient methods for reading input values of various types.
You must first create a Scanner** **object in order to invoke its methods.
Example:
Scanner scan = new Scanner (System.in);
Instantiation
(Objects)
Using the new opperator which returns the address of the new object.
aliases
Two object designators that point to the same object.
String name1 = “Ada”;
String name2 = “Grace”;
name2 = name1;
Now name2 points to the same object as name1, which is “Ada”.
immutable
In reference to String object - Once created, the value cannot be lengthened or shortened, nor can any of its chararcters change.
When change, that object is no longer references and a new object is created. If not referenece by another designator, the object is no longer useable and is eventually elliminated by the automatic Java garbage collector.
String (String str)
Constructor: creates a new string object with the same characteristics as str.
char charAt (int index)
Returns the character at the specified index.
int compareTo (String str)
Returns an integer indicating if this string is lexically before (a negative return value) equal to (a zero return value), or lexically after (a positive return value), the string str.
String concat (String str)
Returns a new string consisting of this string concatenated with str.
boolean equals (String str)
Returns true if the string contains the same characters as str.
int lenght ()
Returns the # of characters in this string.
String replace (char oldChar, char newChar)
Returns a new string that is identical with the string except that every occurance of oldChar is replaced by newChar.
String substring (int offset, int endIndex)
Returns a new string that is a subset of this string starting at index offset and extending through endIndex-1.
String toLowerCase ()
Returns a new string identical to this string except all uppercase letters are converted to their lowercase equivalent.
String toUpperCase ()
Returns a new string identical to this string except all lowercase letters are converted to their uppcase equivalent.
import
Java keyword used to allow the program to use another class from within a particular class.
This can be done by package (import java.util.*;) or by a particular class within the package (import java.util.Random;).
static method
Also called class methods - They can be invoked through the name of the class in which they are defined, without having to instantiate an object of the class first.
NumberFormat Class (ordinal value)
The first value in an enumerated type has an ordinal value of 0, the second has a vaule of 1, and so on.
The ordinal method returns the numeric value associated with a particular enumerated type value.
enumerated type
Establishes all possible values or a variable of that type by listing, ore enumerating, them.
Example:
enum Season {winter, spring, summer, fall};
Season time;
time = winter; (correct)
time = 10; (incorrect)
enumerated type (ordinal value)
The first value in an enumerated type has an ordinal value of 0, the second value has an ordinal value of 1, and so on. To get the ordinal value of an enumerated type, you can use the ordinal() method.
Wrapper Class
A class that represents a primitive type when you want to be able to treat a primitive data type as though it is an object.
Autoboxing
The automatic conversion between a primitive value and a corresponding wraper object.
Interger obj1;
int num1 = 69;
obj1 = num1; // automatically creates an Integer object
Unboxing
The reverse of autoboxing, automatically converts a wrapper object into a corresponding primitive value.
Integer obj2 = new Integer(69);
int num2;
num2 = obj2; // automatically extracts the int value
constructor
A special method that has the same name as the class and is called when an object is created to set up the object initially.
equality operators
== Test whether two values are equal
!= Test whether two values are not equal
relational operators
**> **greater than
< less than
>= greater than or equal to
<= less than or equal to
logical operators
! logical NOT
&& logical AND
|| logical OR
precidence for arithmetic and logical opperators
arithmetice operators have a higher precidence than relational operators and therefore are performed before the logical operators.
if-else statement
Do one thing if, else do another.
if (height<= MAX)
adjust = 0;
else
adjust = MAX - height;
conditional operator
(total > MAX) ? total +1 : total * 2;
The entire conditional expression returns the value of the first expression if the condition is true, and returns the value of the second expression if the condition is false.
Nested if Statements
An if statement that is exicuted when another if statement is true.
if (x = 10)
if (y \>2)
Comparing Floats
Rarely is == used.
A better way to check for a floating point equality is to compute the absolute value of the difference between the values and compare the result to some tolerace level.
if(Math.abs(f1-f2) < TOLERANCE)
System.out.println("Essentially equal.");
Comparing Characters
characters can be compared using equality and rational operators as their “value” is based on their Unicode character position and value.
Comparing Strings
use .equals() to compare strings
if(name1.equals(name2);
Note:
(name1 == name2) tests to see if both references refer to the same object or in other words, have the same reference location.
Used to determine the relative ordering of two strings
compareTo()
name1.compareTo(name2);
Returns a negative value if name1 comes before name2.
Returns zero if name1 is the same as name2.
Returns a positive value if name1 comes after name2.
The switch Statement
Causes executing program to follow one of several paths based on a single value (Must be of type char, byte, short, or int).
switch (idChar)
{
case 'A': aCount++; break; case 'B': aCount++; break;
and so on. If no case is match then continues on to the default case. Such as:
default: noCaps++;
}
If no default is used, the program continues on with executing any of the switch statements.
The while Statement
A loop that eveluates a boolean condition just as an if statment does and executes a statment repeatedly until the statement is nolonger true.
int count =1;
while (count <= 5)
{
System.out.println(count); count++;
}
infinite loop
A conditional loop that does not become false and therefore continues to execute forever or until it is stopped.