Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 115

Real World Scenaria var in the Real World

Оглавление

The var keyword is great for exam authors because it makes it easier to write tricky code. When you work on a real project, you want the code to be easy to read.

Once you start having code that looks like the following, it is time to consider using var:

PileOfPapersToFileInFilingCabinet pileOfPapersToFile = new PileOfPapersToFileInFilingCabinet();

You can see how shortening this would be an improvement without losing any information:

var pileOfPapersToFile = new PileOfPapersToFileInFilingCabinet();

If you are ever unsure whether it is appropriate to use var, we recommend “Local Variable Type Inference: Style Guidelines,” which is available at the following location.

https://openjdk.java.net/projects/amber/LVTIstyle.html

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

Подняться наверх