keywords:
Bookmark and Share



Front Back
How to you keep an instance variable from being serialized?
Use the transient modifier.
Give an example of why you'd want to keep an instance variable from being serialized along with it's owning object?
If that ivar is of a type that is not serializable a NotSerializableException will be thrown unless it's maked transient.
Let's say an object has to mark some of its ivars transient? How might it restore those ivars when it gets unserialized?
"An object marked Serializable can implement it's own special writeObject() and readObject() methods
"If a class is not marked Serializable
can it be serialized?"
"What must a Serializable object
which inherits from non-serializable classes
"If a class is not marked Serializable
can it be serialized?"
What must a Serializable object which inherits from non-serializable classes do to serialize without throwing runtime errors?
It's ivars might cause NotSerializableExceptions-s but it's ancestors will cause no problem. The first ancestor (and all ancestors above that) will simply be rebuilt by the calling of constructors. The values that come from these ancestor classes will be just as were at the point of the original object creation All classes that are serializable in the ancestral tree do NOT have their constructors called - and instead state is restored accordingly.
Which are marked serializable: the collection interfaces or the concrete collection classes?
The classes.
What must be true of an array and a collection before either can be serialized?
All their elements must be serializable.
What packages are Dates Numbers Currencies found in?
java.util.* -Date -Calendar -Locale java.text.* -DateFormat -NumberFormat
Get current date and time.
Date d = new Date(); System.out.println(d.toString()); OR Calendar c = Calendar.getInstance(); Date d = c.getTime(); System.out.println(d.toString());
Get an object that lets you perform date and time calculations in a different locale.
Locale l = new Locale(language); or Locale l = new Locale(language country); Calendar c = Calendar.getInstance(l); c.add(...) & c.roll(...) to add and subtract time to the existing date and time. c.set(...) & c.setTime(...) to set the date and time anew.
Get an object that lets you perform data and time calcs and then format it for output in different locales with different date styles.
Calendar c = Calendar.getInstance(); Locale l = new Locale(language); or Locale l = new Locale(language country); Date d = c.getTime(); DateFormat df = DateFormat.getInstance(style l); String s = df.format(d);
Get an object that lets you format numbers or currencies accross many different locales.
Locale l = new Locale(...); NumberFormat nf = NumberFormat.getInstance(l); or NumberFormat nf = NumberFormat.getCurrencyInstance(l); String s = nf.format(someNumber);
What's the relationship between the Date and Calendar classes?
Date has for the most part been depricated and replaced by Calendar. There are times when Date bridges gaps between Calendar and other classes (specifically DateFormat class)
What is notable about instantiating Calendar objects vs. Date objects?
While new Date() works fine Calendar is ABSTRACT! Thus you cannot directly create a Calendar object. You have to do it indirectly using a static method: Calendar c = Calendar.getInstance();
What does a Date represent?
The number of milliseconds(internally as a long) since January 1st 1970 12am. I sniff a unix timestamp! ;P
Besides toString() what are the 2 methods we want to know about in the Date class?
getTime() <-- returns milliseconds setTime(long milliseconds) NOTE: working in milliseconds scale and NOT the seconds scale! BTW: 1000 milliseconds = 1 second
What Calendar methods should you be familiar with?
Assume: Calendar c = Calendar.getInstance(); getTime(); // Returns a Date obj setTime(Date d); set(...); // numeric args to set up a date & time getFirstDayOfWeek() -returns value from Calendar.SUNDAY to Calendar.SATURDAY get(c.DAY_OF_WEEK) add(field amount) where field = Calendar.SECOND Calendar.MINUTE Calendar.HOUR Calendar.YEAR Calendar.DAY_OF_WEEK etc. roll() acts like add() except when a part of Date get incremented or decrmented larger parts of the Date will not change
What Class do you ideally use to create a date so that it can be manipulated?
Calendar Calendar c = Calendar.getInstance(); // date AND time Calendar c = Calendar.getDateInstance(); // date only
DateFormat df = new DateFormat(); Compiles? If not how would you make it compile?
Fails to compile. DateFormat is abstract. Need to call one of the following static methods: getInstance(); - unlike getDateInstance() includes time as well - is the default and uses a SHORT formatter for both date and time - a SHORT time formatter means that there will be no milliseconds getDateInstance(); getDateInstance(style); -where style is DateFormat.SHORT DateFormat.MEDIUM DateFormat.LONG DateFormat.FULL getDateInstance(field locale);
Methods of a DateFormat object?
getInstance() getDateInstance() getDateInstance(SHORT/MEDIUM/LONG/FULL) getDateInstance(SHORT/MEDIUM/LONG/FULL Locale) format(Date d) parse(String dateAsString) -might throw a ParseException!
How does Date connect Calendar and DateFormat?
DateFormat method format() does not take a Calendar object. It only takes a Date or in some cases a Number object. So if you convert your Calendar to a Date then you can use DateFormat.
What happens when you call Calendar's toString() method?
You get lots of crud! Better to output as a Date (getTime() will output Date object) and either use Date's toString() or use Date as argument of DateFormat's format() method.
When you send a bad string to a DateFormat object's parse() method what happens?
a CHECKED exception gets thrown (meaning you MUST do a parse inside a try block or declare that the current method throws the exception) This is what might be thrown: ParseException
Calendar c = Calendar.getInstance(); Date d = c.getTime(); System.out.println(d); DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); String s = df.format(d); d = df.parse(s); System.out.println(d); Compile? If so will the printed outputs match?
This will NOT compile if if the DateFormat parse() method call is not inside a try statement or part of a method that declares it throws the checked ParseException. If the above condition is met then NO the 2 strings printed will not be the same (probably ;P ). When the Date is formatted with the DateFormat.SHORT field the formatted string doesn't include any time information. When that string get's parsed back into a Date object it now is missing any time info. So unless this code is run EXACTLY at midnight the 2 strings output will be different.
If the Locale class is abstract how do you instantiate a Locale object?
The Locale class IS NOT abstract (so you directly instantiate) new Locale(String language); new Locale(String language String country); ISO 639 = language codes ISO ? = country codes (you don't need to know any codes for the exam)
What happens when you try to format with a locale language or country that's not supported on the local machine? Compiles? Runs?
Yes and yes. But the output will be question marks. I.E. ?????? ?? ?????? ????
When you want to change locales in a DateFormat or NumberFormat object what method to you call on the formatting object?
None. You must create a new formatting object if you want to change locales. Once a formatting object is built with a Locale object that Locale object is immutably set.
What are the primary methods you'll call on a locale object?
Locale l1 = new Locale(String language String country); l1.getDisplayCountry(); or l1.getDisplayCountry(Locale l2) // The actual instace of the locale will be displayed in default locale format if no arg version called and will display in the locale format of the argument if a Locale argument is provided. l1.getDisplayLanguage() l1.getDisplayLanguage(Locale l2)
How do you get an instance of the NumberFormat class?
NumberFormat.getInstance(); NumberFormat.getInstance(Locale l); NumberFormat.getNumberInstance(); NumberFormat.getNumberInstance(Locale l); NumberFormat.getCurrencyInstance(); NumberFormat.getCurrencyInstance(Locale l);
What are the important methods of the NumberFormat class?
NumberFormat nf = NumberFormat.getInstance(); nf.getMaximumFractionDigits() nf.setMaximumFractionDigits(int i) // fractions higher than max will be ROUNDED (not truncated) nf.setParseIntegerOnly(boolean b) - if set to true then any decimals in parse string when parse() converts a string to a primitive number nf.parse(String s) - throws ParseException nf.format(primitive number)
What package is regex found in?
java.util.regex.*
How do you set up a pattern in Java's regex?
Pattern p = Pattern.compile(String s); NOTE: You don't directly instantiate a pattern object! Instead you call the static method compile(String s) which outputs a Pattern object
How do you build an object that looks for matches?
Pattern p = Pattern.compile(String lookForThis); Matcher m = p.matcher(String lookInHere);
How might you iterate through a source string to find all the matches of certain pattern?
Pattern p = Pattern.compile(""\\d*""); Matcher m = p.matcher(""123abc456def""); boolean b; System.out.println(p.pattern()); while (b = m.find()) { System.out.println(m.start() + "": "" + m.group()); }
In regex what do the following quantifiers mean? + ? *
+ = 1 or more occurrences ? = zero or 1 * = 0 or more
What are the 3 behavioral qualities of quantifiers in regex?
greedy (default) - eats entire source string and works backwards reluctant (add a ?) - eats as little of string as it can and works forward possessive (add a +) - eats entire string like greedy but it does not work backwards. EXAMPLES: Current REGEX is: .*foo // greedy quantifier Current INPUT is: xfooxxxxxxfoo I found the text ""xfooxxxxxxfoo"" starting at index 0 and ending at index 13. Current REGEX is: .*?foo // reluctant quantifier Current INPUT is: xfooxxxxxxfoo I found the text ""xfoo"" starting at index 0 and ending at index 4. I found the text ""xxxxxxfoo"" starting at index 4 and ending at index 13. Current REGEX is: .*+foo // possessive quantifier Current INPUT is: xfooxxxxxxfoo No match found.
String ps = ""\d\d""; String source = ""123cat346""; Pattern p = Pattern.compile(ps); Matcher m = p.matcher(source); boolean b; while (b = m.find()) { System.out.println(m.start()); } What prints?
Compiler error. Compiler will read \d as an illegal escape. String literal needs to be \\d\\d
Pattern p = Pattern.compile(""\\d*""); Matcher m = p.matcher(""123abc456def""); boolean b; System.out.println(p.pattern()); while (b = m.find()) { System.out.println(m.start() + "": "" + m.group()); } What prints?
\d* 0: 123 3: 4: 5: 6: 456 9: 10: 11: 12: NOTE: The star (""*"") is greedy AND it will match 0 characters. At the 4th index... it first eats entire string and works backwards. When it finally works back to the ""a"" and that doesn't match it DOES match on the empty string. If you changed the * to a + you'd get what you might expect from this: \d+ 0: 123 6: 456
What package is Scanner found in?
java.util.*;
What does Scanner do?
It scans a File InputStream Readable ReadableByteChannel or String. Often used for tokenizing it's source.
How do you use Scanner for searching?
Scanner s = new Scanner(System.in); String token = """"; do { token = s.findInLine(""a""); } while (token != null);
Name 2 classes you can use for tokenizing?
String and Scanner
What's special about the String classes split method?
It takes String that is a regex expression.
What's the drawback of tokenizing with String's split(String regexPattern)?
It tokenizes it's target all at once. You can't do stuff after finding each token.
What is Scanner's default delimiter?
whitespace
List the Scanner methods you need to know():
findInLine(String regexPattern); hasNext() - see if there is another token present (often use this in while loop along with next() - outputs boolean value next() - outputs the next token hasNextInt() hasNextBoolean() hasNextFloat() etc. - is there another token of this particular type? nextInt() nextBoolean() nextFloat() etc.. - get next token a particular type useDelimiter(String regexPattern) NOTE: there is NO hasNextXxx() nextXxx() for type char.
What is the most important thing to remember about the relationship between java.io.PrintStream method format() and printf()?
They are aliases of each other!
What class are methods format() an printf() a part of?
java.io.PrintStream
|< Previous x of y cards Next >|