Ch16 Exceptions, Assertions, and Localization Flashcards
What is an assertion?
An assertion is a boolean expression that you can place at a point in your code where you expect something to be true.
What is an assert statement?
An assert statement is used to declare an expected boolean condition in a program. If the program is running with assertions enabled, then the condition is checked at runtime. If the condition is false, the Java runtime system throws an AssertionError .
What is the syntax of an assert statement?
assert keyword
boolean expression
optional message
example:
assert 1 == age;
assert(2 == height);
assert 100.0 == length : “Problem with length”;
Is the following assert statement valid?
assert (“Cecilia”.equals(name)): “Failed to verify user data”;
Yes.
Is the following assert statement valid?
assert (1);
No. it expects a boolean value.
Is the following assert statement valid?
assert x -> true;
No. it expects a boolean value.
Is the following assert statement valid?
assert.test(5 > age);
No. The syntax is invalid
What happens when assertions are enabled and the boolean expression of an assertion is false?
Java will throw an AssertionError.
How can we enable assertions using the java command?
// single-file source-code
java -ea SomeFile.java
java -enableassertions SomeFile.java
// normal
java -ea SomeFile
java -enableassertions SomeFile
Given the package com.demos, how do we run the application and only enable assertions for that package?
- java -ea:com.demos… MyMainClass
- With one argument ending in “…”, assertions are enabled in the specified package and any subpackages by default.
- With one argument not ending in “…”, assertions are enabled in the specified class.
What flag can we use to exclude a package/class from enabling assertions?
-da or -disableassertions
Given the class Test in package com.demos, how do we run the application and enable assertions for that package, but exclude the class Test?
java -ea:com.demos… -da:com.demos.Test MyMainClass
What is the difference between:
java.time.LocalDate
java.time.LocalTime
java.time.LocalDateTime
java.time.ZonedDateTime
- java.time.LocalDate: Date with day, month, year
- java.time.LocalTime: Time of day
- java.time.LocalDateTime: Date and time with no time zone
- java.time.ZonedDateTime: Date and time with a specific time zone
What static method exists to get the current value for:
java.time.LocalDate
java.time.LocalTime
java.time.LocalDateTime
java.time.ZonedDateTime
now()
What static method can we use to get a specific date?
of()
example:
LocalDate d1 = LocalDate.of(2020, 10, 20);
LocalTime t1 = LocalTime.of(6, 15, 30);
…
Create a LocalTime object of 6 hours and 15 minutes.
LocalTime time = LocalTime.of(6, 15);
Create a LocalTime object of 6 hours, 15 minutes, 0 seconds and 200 nanoseconds
LocalTime time = LocalTime.of(6, 15, 0, 200);
Given the following objects, create a LocalDateTime object.
LocalDate d1 = LocalDate.of(2020, 10, 20);
LocalTime t1 = LocalTime.of(6, 15);
LocalDateTime dt1 = LocalDateTime.of(d1, t1);
What class can we use to format a date/time object to display standard formats?
DateTimeFormatter
example:
DateTimeFormatter.ISO_LOCAL_DATE // 2020-10-20
DateTimeFormatter.ISO_LOCAL_TIME // 11:22:34
DateTimeFormatter.ISO_LOCAL_DATE_TIME // 2011-12-03T10:15:30
How can we print a date/time object in a custom format?
example: October 20, 2020 at 11:12
using DateTimeFormatter.ofPattern() and use it with the format() method.
example:
var f = DateTimeFormatter.ofPattern(“MMMM dd, yyyy ‘at’ hh:mm”);
somedateobject.format(f);
What does the date/time symbol ‘M’ print?
‘M’ = month and prints the index of the month, starting with 1.
What does the date/time symbol ‘MM’ print?
‘M’ = month and prints the index of the month, starting with 01.