Is there an automatic output type in Java?

Is there an auto variable type in Java like you have in C ++?

Example:

 for ( auto var : object_array) std::cout << var << std::endl; for( auto var : object_array) var.do_something_that_only_this_particular_obj_can_do(); 

I know that in Java there is an extended for loop, but is there an auto? If not, is there a hack for this? I mean a new function in C ++ 11

+61
java c ++ auto
Apr 21 '13 at 15:23
source share
6 answers

The answer to the question was EDITED :

There is no type of auto variable in Java. The same cycle can be achieved as:

 for ( Object var : object_array) System.out.println(var); 

Java has local variables, the scope of which is inside the block where they were defined. Like C and C ++, but there is no auto or register keyword. However, the Java compiler will not allow the use of an implicitly initialized local variable and will give a compilation error (unlike C and C ++, where the compiler usually only warns). Courtesy: Wikipedia .

No, Java does not have any basic output type, such as C ++. There was an RFE , but it was closed as "Do not fix", the reason was:

People benefit from type redundancy in two ways. First, the redundant type serves as valuable documentation - readers should not look for the getMap () declaration to find out which type is being returned. Secondly, redundancy allows the programmer to declare the type and, therefore, take advantage of the cross-validation performed by the compiler.

+22
Apr 21 '13 at 15:28
source share

Java 7 Introduces Diamond Syntax

 Box<Integer> integerBox = new Box<>(); // Java 7 

Compared to old java

 Box<Integer> integerBox = new Box<Integer>(); // Before Java 7 



A critical reader will notice that this new syntax does not help write for loops in the original question. This is correct and completely deliberate. See Another answer that refers to an Oracle database.

+20
Jul 31 '13 at 22:48
source share

In Java 8, you can use lambda type inference to avoid type declaration. An analogue to the survey examples will be:

 object_array.forEach(var -> System.out.println(var)); object_array.forEach(var -> var.do_something_that_only_this_particular_obj_can_do()); 

both of them can also be simplified using method references:

 object_array.forEach(System.out::println); object_array.forEach(ObjectType::do_something_that_only_this_particular_obj_can_do); 
+13
May 09 '16 at 16:37
source share

In short, no, no automatic type. If all you do is print the value, you can simply refer to the value as an Object .

+8
Apr 21 '13 at 15:25
source share

This is not a pure Java solution, but adding a library called lombok will allow the magic below to compile and work very similar to an auto keyword in C ++

 List<String> strList = Arrays.asList("foo", "bar", "baz"); for (val s: strList){ System.out.println(s.length()); } 
+1
Oct 22 '17 at 21:22
source share

Maybe Java 10 has what you (and I) want with the var keyword.

 var list = new ArrayList<String>(); // infers ArrayList<String> var stream = list.stream(); // infers Stream<String> 

From the JDK Enhancement 286 Offer

0
Oct 31 '17 at 14:15
source share



All Articles