In the following code, what does Type type
mean, and what are the curly brackets are used for?
Type type = new TypeToken<List<String>>(){}.getType();
List<String> list = converter.fromJson(jsonStringArray, type );
Brackets, or braces, are a syntactic construct in many programming languages. They take the forms of "[]", "()", "{}" or "<>." They are typically used to denote programming language constructs such as blocks, function calls or array subscripts. Brackets are also known as braces.
Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.
You put curly braces when you want to use the value of a variable inside "html" (so inside the render part). It's just a way of telling the app to take the value of the variable and put it there, as opposed to a word.
If the number of statements following the for/if is single you don't have to use curly braces. But if the number of statements is more than one, then you need to use curly braces.
That's not after a function call, but after a constructor call. The line
Type type = new TypeToken<List<String>>(){}.getType();
is creating an instance of an anonymous subclass of TypeToken
, and then calling its getType()
method. You could do the same in two lines:
TypeToken<List<String>> typeToken = new TypeToken<List<String>>(){};
Type type = typeToken.getType();
The Java Tutorial Anonymous Subclasses has more examples of this. This is a somewhat peculiar usage, since no methods are being overridden, and no instance initialization block is being used. (See Initializing Fields for more about instance initialization blocks.)
Type
is a class.
new TypeToken<List<String>>() {
}.getType();
Is creating an anonymous inner class and invoking getType()
on the object created.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With