Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6910583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:50:00+00:00 2026-05-27T08:50:00+00:00

how can I write Java 1.7 code for a Java 1.6 compiler, where the

  • 0

how can I write Java 1.7 code for a Java 1.6 compiler, where the diamond can not be used?

Example:

private ReplacableTree<E> convertToIntended(Tree<? extends E> from,ReplacableTree<E> to) {

    TreeIterator<? extends E> it = new TreeIterator<>(from.getRoot());

    while(it.hasNext()) {
        E e = it.next().getElem();
        to.add(e);
    }
    return to;
}


public class TreeIterator<E> implements TreeIter<Node<E>> {
....
}

It is not allowed to write…

TreeIterator<? extends E> it = new TreeIterator<?>(from.getRoot());
TreeIterator<? extends E> it = new TreeIterator<E>(from.getRoot());
TreeIterator<? extends E> it = new TreeIterator<? extends E>(from.getRoot());

Especially the third one is confusing for me. Why doesn’t it work? I just want to read Elements from a Tree (which could be a subtype tree), and when puch each of it in a new Tree with Elements of type E.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:50:00+00:00Added an answer on May 27, 2026 at 8:50 am

    Wildcard types are not permitted as type arguments in class instance creation expressions:

    It is a compile-time error if any of the type arguments used in a class instance creation expression are wildcard type arguments (§4.5.1).

    so the first and third variants are not valid.

    Variant 2 is invalid because the TreeIterator<E> constructor wants a Node<E>, but you give it a Node<? extends E>.

    As for the solution, Java 5 and 6 did not have type inference for constructors, but do have type inference for methods, and in particular capture conversion. The following ought to compile:

    TreeIterator<? extends E> it = makeIterator(from.getRoot());
    

    where

    private <E> TreeIterator<E> makeIterator(Node<E> node) {
        return new TreeIterator<E>(node);
    }
    

    Edit: You asked in the comment:

    The contstructor parameter type for TreeIterator is Node<E>. The constructor parameter of Node<E> therefore is E. When writing variant two, eclipse says the following: The constructor TreeIterator<E>(Node<capture#2-of ? extends E> ) is undefined What does that mean?

    Being a wildcard type, the type Node<? extends E> represents a family of types. Node<capture#2-of ? extends E> refers to a specific type in that family. That distinction is irrelevant in this case. What matters is that Node<? extends E> is not a subtype of Node<E>, and hence you can’t pass an instance of Node<? extends E> to a constructor expecting a Node<E>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write from Java to the Windows Event Log?
How come, in Java, I can write List<?> list = new LinkedList<Double>(); but not
I've heard that the advantage of java is that people can write code, compile
Can I write to the end of a 5GB file in Java? This question
I need to write a java application which can merge docx files. Any suggestions?
Newbie here...can I write one program which incorporates .NET LINQ and also various Java
I'm trying to follow Java's JDBC tutorials to write a Java program that can
How can I read/write serializable object instances to a RandomAccessFile in Java? I want
java experts can you please help me write detached queries as a part of
With iText I can use Java to open a pdf and write it. If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.