📜 ⬆️ ⬇️

First contact with “var” in Java 10

I present to you the translation of the article First Contact With 'var' In Java 10 by Nicolai Parlog .

image

Java 10 will be released on March 20, 2018 , and all the features that should be in this release are already merged into the main development branch. One of the most interesting innovations of Java 10 is the inference of the type of a local variable ( JEP 286 ). This gives you the opportunity to shorten variable declarations using the new var keyword:
')
var users = new ArrayList<User>();

, !

, , . , var, , val.

var


Java , :

URL codefx = new URL("http://codefx.org");

, :

URL codefx = new URL("http://codefx.org");
URLConnection connection = codefx.openConnection();
Reader reader = new BufferedReader(
    new InputStreamReader(connection.getInputStream()));

, . IDE , , -, , , .

Java 10 — var:

var codefx = new URL("http://codefx.org");
var connection = codefx.openConnection();
var reader = new BufferedReader(
    new InputStreamReader(connection.getInputStream()));

var, , . , -.

, , , , , . : , connection, . IDE , , , (, — stackoverflow — . .).

, var: . , var — , , , , . , , var, , .

, . , :


.

, JavaScript


, var Java . , .

, IntelliJ ( Fernflower) URL:

URL codefx = new URL("http://codefx.org");
URLConnection connection = codefx.openConnection();
BufferedReader reader = new BufferedReader(
    new InputStreamReader(connection.getInputStream()));

, . -, - . , Javascript, 0 .

, , : - -, ?

rhetoricalQuestion.answer(yes -> " ?");

var ( )


JEP 286, « », , var : . , « », :

// 
var foo;
foo = "Foo";

: var foo = ''Foo''. , var «poly expressions», - , :

//     
var ints = {0, 1, 2};
var appendSpace = a -> a + " ";
var compareString = String::compareTo

, — for:

var numbers = List.of("a", "b", "c");
for (var nr : numbers)
    System.out.print(nr + " ");
for (var i = 0; i < numbers.size(); i++)
    System.out.print(numbers.get(i) + " ");

, , catch .

// 
private var getFoo() {
    return "foo";
}

« »


, var , , . , , :

//    ,    List<User>
var users = new ArrayList<User>();
//  ,   :
users = new LinkedList<>();

, , . JDK « », , , , .

:

//   `int`
var id = 123;
if (id < 100) {
    //   
    //      
} else {
    // ,   ...
}

… «», , . , . :

id = "124"

? , .

, if- , id int 100. , . , .

, .

?


, , . , , . .

, , . , , .

, «» , , .

var


, var, val ( let), . , JEP 286, Project Amber.

?!


Java , , Java. Project Amber, var, « , Java-», , , Java.

. , , , IDE, , , .

. ? , .

InternationalCustomerOrderProcessor<AnonymousCustomer, SimpleOrder<Book>> orderProcessor = createInternationalOrderProcessor(customer, order);

, 150 , . , .

var orderProcessor = createInternationalOrderProcessor(customer, order);

var , , . , , . var .

, var — , .

?


. , , , ? , . , , . IDE , , , .

, var . — :

//   
No no = new No();
AmountIncrease<BigDecimal> more = new BigDecimalAmountIncrease();
HorizontalConnection<LinePosition, LinePosition> jumping =
    new HorizontalLinePositionConnection();
Variable variable = new Constant(5);
List<String> names = List.of("Max", "Maria");
 
//   
var no = new No();
var more = new BigDecimalAmountIncrease();
var jumping = new HorizontalLinePositionConnection();
var variable = new Constant(5);
var names = List.of("Max", "Maria");

, . Java ( JDK), ( ) - () , , . , , , .

var , , , , . - , Kotlin, . .

, - , , .


, var . , , , .

(Brian Goetz), Java Oracle Project Amber, :
var, , .
, IDE , var. , -.

val/let?


var . val, let, Java 10 , , final var. :


, . val let var , final , , .

, … final var.


var , . , , , var s = "". var. , , -, — Java .

, , var . , « » , , .

var , Java- , / .

Source: https://habr.com/ru/post/346214/


All Articles