Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are curly braces after function call for?

Tags:

java

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 ); 
like image 929
shai Avatar asked Sep 23 '13 20:09

shai


People also ask

What is {} called in programming?

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.

What are curly braces used for?

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.

What does {} mean in react?

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.

Is it necessary to use curly braces after the for statement?

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.


2 Answers

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.)

like image 159
Joshua Taylor Avatar answered Oct 19 '22 14:10

Joshua Taylor


Type is a class.

new TypeToken<List<String>>() {
}.getType();

Is creating an anonymous inner class and invoking getType() on the object created.

like image 31
adarshr Avatar answered Oct 19 '22 15:10

adarshr