Chapter 2 Flashcards
(35 cards)
Escape sequence for a backspace
\b
Escape sequence for tab
\t
Escape sequence that creates a new line
\n
Escape sequence that is a carriage return
\r
Escape sequence for double quotes
"
Escape sequence for a single quote
'
Escape sequence for a backslash
\
Values considered an int
Integers
Values considered a double
Decimal values
A type of data with only 2 values: true and false.
Boolean
What are the three primitive types the AP test requires you to know?
int, double, and boolean
A class that is already defined in Java. Objects are assigned using double quotes.
String class
Once an object is created their values cannot be changed.
Immutable
Ex. Strings
Object and variable name rules
- should begin with a lower case letter
- if 2 words 2nd word capitalized ex. firstName
- or underscore ex. harry_potter
- numbers only after 1st letter ex. r2d2
- no other symbols
What index is the 1st letter of a String?
0
String Method that returns a number indicating whether this string comes before (negative return value), is equal to (a zero return value), or comes after (positive return value) the string str
int compareTo(String str) int val = word.compareTo("Goodbye"); //returns 1
String Method that returns a new string made up of this string added to (concatenated with) str.
String concat(String str) Ex. String phrase = word.concat(", how are you"); // returns Hello, how are you
String Method that returns the position of the first character in the first occurrence of str in this string.
int indexOf(String str) Ex. int index = word.indexOf("lo"); //returns 3
String Method that returns the number of characters in a string.
int length() Ex. word.length(); //returns 5
String Method that returns a new string that is subset of their string starting at index offset and ending with the character at position endIndex -1
String substring(int offset, int endIndex) Ex. String shorter = word.substring(1,4); //returns ell
String Method that returns a new string that starts at index offset and extends to the end of the string
String substring(int offset) Ex. String temp = word.substring(3); // returns lo
Class that formats decimals
DecimalFormat Class
Import for the DecimalFormat Class
import java.text.DecimalFormat;
How to format a decimal (ex.)
DecimalFormat fmt = new DecimalFormat("0.###"); double total = 1.2345 System.out.print(fmt.format(total)); // prints 1.234