3

I have a piece of code like this:

@RequestMapping(value = "/find/{id}")
@GetMapping
public ResponseEntity<QuestionModel> find(@PathVariable("id") Long id) {
    Question question = questionService.find(id);
    QuestionModel questionModel = new QuestionModel(question);
    **return new ResponseEntity<>(questionModel, HttpStatus.OK);**
}

I wanna know that what's differences between that & this:

@RequestMapping(value = "/find/{id}")
@GetMapping
public ResponseEntity<QuestionModel> find(@PathVariable("id") Long id) {
    Question question = questionService.find(id);
    QuestionModel questionModel = new QuestionModel(question);
    **return new ResponseEntity(questionModel, HttpStatus.OK);**
}
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • 1
    If you don't use any `<...>` you will use **raw-types**. You should **never** use raw-types. Java only still supports it for backwards compatibility reasons with Java < 5. Using `<>` (diamond operator) instead of `` (writing it out) is just syntax sugar for convenience. The compiler replaces the diamond operator with the fully-written out type. – Zabuzard Jul 17 '18 at 12:07
  • The difference in the above code would be a warning at compile time in the latter case. – Kayaman Jul 17 '18 at 12:09

1 Answers1

1

If you don't use any <...> you will use raw-types. You should never use raw-types, generics are way safer to use and prevent way more bugs due to increased compiler knowledge. Java only still supports it for backwards compatibility reasons < Java 5. See What is a raw type and why shouldn't we use it?

Using <> (diamond operator) instead of <Foo> (writing it out) is just syntactic sugar for convenience. The compiler replaces the diamond operator with the fully-written out type (same for var in Java 10). See What is the point of the diamond operator in Java 7?

// Are the same
List<Integer> values = new ArrayList<Integer>();
List<Integer> values = new ArrayList<>();

// Raw types, don't use if > Java 5
List values = new ArrayList();
// Assigning a raw-type to a generic variable, mixing both, don't use
List<Integer> values = new ArrayList();
Zabuzard
  • 25,064
  • 8
  • 58
  • 82