Is there auto type inferring in Java?

JavaC++Auto

Java Problem Overview


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

An 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 there is an enhanced for loop in Java, but is there an auto? If not, is there a hack to doing this? I am referring to the new feature in C++11

Java Solutions


Solution 1 - Java

Might be Java 10 has what you (and I) want, through the var keyword.

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

From JDK Enhancement Proposals 286


Update: Yap, that feature made it into the Java 10 release!

Solution 2 - Java

Java 10 introduced a var identifier which is like C++ auto; see sorrymissjackson's answer.

Prior to Java 10, there was no equivalent to the auto keyword. The same loop can be achieved as:

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

Java has local variables, whose scope is within the block where they have been defined. Similar to C and C++, but there is no auto or register keyword. However, the Java compiler will not allow the usage of a not-explicitly-initialized local variable and will give a compilation error (unlike C and C++ where the compiler will usually only give a warning). Courtesy: Wikipedia.

There wasn't any mainstream type-inference in Java like C++ . There was an RFE but this was closed as "Will not fix". The given was:

>Humans benefit from the redundancy of the type declaration in two ways. First, the redundant type serves as valuable documentation - readers do not have to search for the declaration of getMap() to find out what type it returns. Second, the redundancy allows the programmer to declare the intended type, and thereby benefit from a cross check performed by the compiler.

Solution 3 - Java

Java 7 introduces the diamond syntax

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

As compared to old java

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

The critical reader will notice that this new syntax doesn't help with writing the for loops in the original question. That's correct and fully intentional it seems. See the other answer that cites Oracle's bug database.

Solution 4 - Java

In Java 8, you can use lambda type inference to avoid declaring the type. The analogue to the questioner's examples would be:

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

both of which 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);

Solution 5 - Java

It's not a pure Java solution, however adding a library called lombok will enable the magic below to compile and work very much similar to auto keyword in C++

List<String> strList = Arrays.asList("foo", "bar", "baz");
for (val s: strList){
	System.out.println(s.length());
}

Solution 6 - Java

In short, no, there is no auto type. If all you are doing is printing the value though, you could just refer to the value as an Object.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGames BrainiacView Question on Stackoverflow
Solution 1 - JavasorrymissjacksonView Answer on Stackoverflow
Solution 2 - JavaAllTooSirView Answer on Stackoverflow
Solution 3 - JavaTarraschView Answer on Stackoverflow
Solution 4 - JavaAjit GeorgeView Answer on Stackoverflow
Solution 5 - Javasamvel1024View Answer on Stackoverflow
Solution 6 - JavaSimonCView Answer on Stackoverflow