Chapter 16: 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.
What happens when assertions are enabled and the boolean expression of an assertion is true?
Nothing happens and Java will continue execution.
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
By default, assertions are enabled or disabled?
disabled.
Given the package com.demos, how do we run the application and only enable assertions for that package?
java -ea:com.demos… MyMainClass
Given the class Test in package com.demos, how do we run the application and only enable assertions for that class?
java -ea:com.demos.Test MyMainClass
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
Why should assertions never alter outcomes?
example:
int x = 10;
assert ++x > 10;
This is not a good use of assertions because the outcome of the code will be different depending on wether assertions are turned on.
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(6, 15);
Create a LocalTime object of 6 hours, 15 minutes, 0 seconds and 200 nanoseconds
LocalTime time = LocalTime(6, 15, 0, 200);
Given the following objects, create a LocalDateTime object.
LocalDate d1 = LocalDate.of(2020, 10, 20);
LocalTime t1 = LocalTime(6, 15);
LocalDateTime dt1 = new LocalDateTime(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
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);