dan1st | Daniel
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/24/2024 in #❓︱qotw-answers
Week 101 — How can a directory be deleted from Java code including subdirectories and files?
Files and directories can be deleted using the
Files.delete
method, e.g. using Files.delete(Path.of("/path/to/file"));
.
However, this only works for files and empty directories. To delete directories with contents, one first needs to delete all contents of that directory. Since a directory might have non-empty subdirectories, these need to be deleted recursively.
To do that, one can use Files.list()
to get a list of all files in the directory to delete and then call the method recursively for all found directories. After processing the content of a directory, it can be deleted:
Alternatively, it is possible to use Files.walk
which recursively traverses the directory. Since that method finds the parent directories first, it is necessary to reverse the elements before iterating which can be done using the List#reversed
method that has been added in JDK 21.
2 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/17/2024 in #❓︱qotw-answers
Week 100 — What's the purpose of the `transient` keyword?
This keyword, along with Java's entire built-in serialization mechanism, is far less important today than when it was introduced to the language in JDK 1.1. The awkward semantics were the result of the limited language mechanisms that were available in the language at the time. Nowadays, there are much better tools for accomplishing the same thing. That said, the ecosystem does tend to respect the
transient
keyword or even adopt it for its own purposes. For instance, JPA will recognize transient
fields in persistent classes and omit them from the persisted data. Lombok will ignore transient
fields in its generated equals()
& hashCode()
implementations. Make sure to check the documentation of the frameworks and libraries you are using to see how transient
fields are handled.7 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/17/2024 in #❓︱qotw-answers
Week 100 — What's the purpose of the `transient` keyword?
On the other side of the process, when objects of that class are deserialized (e.g. read with
ObjectInputStream#readObject()
), the transient
fields will not be populated. The developer must account for these uninitialized fields by either setting them in the special readObject()
method of the class, or by otherwise working around the empty value.7 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/17/2024 in #❓︱qotw-answers
Week 100 — What's the purpose of the `transient` keyword?
The
transient
keyword marks fields of a class as being excluded from the serialized state of an object. When a class is marked as serializable (implements the Serializable
interface), the JVM will write the object's state during a serialization operation, such as with the ObjectOutputStream#writeObject(Object obj)
method. Fields marked as transient
will be excluded from that mechanism.7 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/17/2024 in #❓︱qotw-answers
Week 100 — What's the purpose of the `transient` keyword?
Some third-party serialization libraries (like gson) also consider this keyword on fields and ignore
transient
fields during (de)serialization.7 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/17/2024 in #❓︱qotw-answers
Week 100 — What's the purpose of the `transient` keyword?
The
transient
keyword can be used to exclude some fields from serialization. For example, if someInt
is marked transient
, it will not be included in the serialized form and will be 0
when deserialized.
7 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/17/2024 in #❓︱qotw-answers
Week 100 — What's the purpose of the `transient` keyword?
Serialization can be used to convert an object to a different format that can be stored or transferred and then be deserialized to a different format.
For example, binary serialization (using
ObjectOutputStream
/ObjectInputStream
) can do that automatically for classes implementing Serializable
:
It should be noted that (de)serialization can lead to many issues including compatibility, construction of invalid objects (e.g. deserialized data is typically not validated on construction) and deserialization of untrusted data can lead to security vulnerabilities (from denial of service to remote code execution, see also the Javadoc of Serializable
). Because of these issues, binary (de)serialization using ObjectInputStream
/ObjectOutputStream
should be avoided in most cases.7 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/10/2024 in #❓︱qotw-answers
Week 99 — What is the `ReentrantReadWriteLock` class used for and how can it be used?
A
ReadWriteLock
consists of two locks: One for reading and one for writing. The goal of it is that multiple threads can hold the read-lock concurrently but only one can hold the write-lock and only while no thread is holding the read-lock. If a a thread owns the read lock, the write lock cannot be obtained by any other thread and vice-versa.
ReentrantReadWriteLock
is an implementation of the ReadWriteLock
interface that allows threads to obtain the lock even if they already have it.
3 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/3/2024 in #❓︱qotw-answers
Week 98 — What is an enum?
Because of these guarantees, enum instances can be safely compared with
==
instead of .equals()
. They can also be used in switch
statements, just like integer values. As of Java 14, they can also be used in switch expressions. Because the number of values is fixed and known at compile time, the cases can be exhaustive, meaning a default
block is not required.12 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/3/2024 in #❓︱qotw-answers
Week 98 — What is an enum?
Finally, the JVM will ensure that no additional instances of the class exist beyond the declared values. In the "regular"
DaysOfTheWeek
class above, serialization can be used to circumvent the visibility restrictions on the constructor, which would result in instances that are not one of the 7 static final values. However, in the enum class, the JVM treats enum deserialization specially to ensure that the existing instances are returned.
Note that the JVM/JDK also blocks most reflective mechanisms from creating new enum instances. Be warned, though, that there are a couple of arcane mechanisms (notably, the soon-to-be-retired Unsafe
class) that can actually break the normal guarantees around enum instances. These should never be used.
12 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/3/2024 in #❓︱qotw-answers
Week 98 — What is an enum?
Second, enum classes implicitly extend an
Enum
base class, which provides methods for retrieving the enum value's name and ordinal. The Enum
class also implements Comparable
and Serializable
, so all values of an enum can be compared, sorted, and serialized. Enum
also exposes a static method to lookup any enum value by class and name. In addition to that, the compiler will generate a similar static method on each enum. To illustrate that last point, these two method calls are equivalent:
12 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/3/2024 in #❓︱qotw-answers
Week 98 — What is an enum?
First, the enum is more compact, requiring less code to achieve a similar result. The enum's constants must be the first declarations in the class. The last one can be terminated with a semicolon, and then additional members -- including constructors, fields, and methods -- can be added to the enum, just like with any class. The only restriction is that constructors must be private; if the
private
access modifier is omitted, it will still be implied.12 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/3/2024 in #❓︱qotw-answers
Week 98 — What is an enum?
As an example, compare the following enum....
.... with the following regular class....
12 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/3/2024 in #❓︱qotw-answers
Week 98 — What is an enum?
An enum is a special kind of Java class with a fixed set of values. They are similar in function to a regular class with a private constructor and a fixed number of
public static final
instances. However, enums have some advantages.12 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/3/2024 in #❓︱qotw-answers
Week 98 — What is an enum?
An Enum is a Type of document, like a class or an interface.
In this document you can write a number of values that can be use in the code.
A code example can be:
public enum numbers {
ONE,
TWO,
THREE
}
And then you can call de enum numbers in youre code and be sure that the only numbers you can use are ONE, TWO and THREE thanks to this enum....
A practical example could be putting the extensions of files that your code accepts...
12 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/3/2024 in #❓︱qotw-answers
Week 98 — What is an enum?
Just like classes, enums can have fields and constructors but the constructors cannot be called except using the enum constants but a constructor must be called in the enum declaration:
12 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/3/2024 in #❓︱qotw-answers
Week 98 — What is an enum?
Enums are classes where all instances of the class are known at compile-time. An enum can be declared using the
enum
keyword with a list of all enum constants (list of the objects of the enum):
After an enum is declared, its instances can be obtained like regular constants and used like other objects:
All enums have an ordinal()
method that can be used to get the index of the enum value in the declaration:
Similarly, the static
method called values
(which is present in every enum) returns an array of all values of that enum in the order of declaration. The value returned by ordinal()
is the the index of that enum value in the array:
Apart from that, there is an implicit static
method named valueOf
in each enum class that can be used to get an enum value by name:
Enums always extend java.lang.Enum
and cannot have any subclasses.12 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/3/2024 in #❓︱qotw-answers
Week 98 — What is an enum?
Sometimes, it's also necessary to define some sort of order between objects. This can be done using the
Comparable
interface.
A class can mark itself to be comparable to another class by implementing that interface and overriding the compareTo
method.
If firstObject
and secondObject
are equal (by the equals(Object)
method), firstObject.compareTo(secondObject)
as well as secondObject.compareTo(firstObject)
should return 0
.
If firstObject
is considered to be greater than secondObject
, then firstObject.compareTo(secondObject)
should return a value greater than 0
and secondObject.compareTo(firstObject)
should return a value smaller than 0
.
12 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 11/3/2024 in #❓︱qotw-answers
Week 98 — What is an enum?
The
Object
class provides an equals(Object)
method that can be used to test for equality. To do that, one can override that method and write code for equality checks.
If two objects are considered equal, firstObject.equals(secondObject)
should return true
, else it should return false
.
When overriding equals
, one should make sure that a.equals(b)
gives the same result as b.equals(a)
(assuming that neither a
nor b
are null
) and always also override hashCode
and make sure that the hashCode()
result is the same for all objects a
and b
where a.equals(b)
is true.
The methods equals(Object)
and hashCode()
are used by many classes in the JDK for comparing objects, e.g. HashMap
.12 replies
JCHJava Community | Help. Code. Learn.
•Created by JavaBot on 6/30/2024 in #❓︱qotw-answers
Week 80 — Which mechanisms does Java provide for comparing objects?
Sometimes, it's also necessary to define some sort of order between objects. This can be done using the
Comparable
interface.
A class can mark itself to be comparable to another class by implementing that interface and overriding the compareTo
method.
If firstObject
and secondObject
are equal (by the equals(Object)
method), firstObject.compareTo(secondObject)
as well as secondObject.compareTo(firstObject)
should return 0
.
If firstObject
is considered to be greater than secondObject
, then firstObject.compareTo(secondObject)
should return a value greater than 0
and secondObject.compareTo(firstObject)
should return a value smaller than 0
.
15 replies