Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

~ Operator in Java

What does the ~ mean in the function below when the words variable is declared?

After googling it sounds like ~ is the unary bitwise operator for the "NOT" operator and differs from the ! operator in that ~ is applicable only to long, int, short, char, and bit types. That makes some sense, but why is the ~ listed as the ArrayList type?

public List<String> promptForWords(Template tmpl) throws IOException {
        List<String> words = new ArrayList<~>();
        for (String phrase : tmpl.getPlaceHolders()) {
            String word = promptForWord(phrase);
            words.add(word);
        }
        return words;
    }

Thanks!

I'm not entirely sure what they're doing with it in the above. In the real world, thanks to Java 8, you don't have to put anything in the ArrayList side of the declaration. Used to be List<String> words = new ArrayList<String>(). Now it's List<String> words = new ArrayList<>() ). So I wouldn't worry to much about a ~. I've never used one in Java coding.

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Thanks, Lukas. Did you mean that the new way to declare an ArrayList in Java 8 is:

List<String> words = new ArrayList<>()

I just wanted to double check because I've noticed IntelliJ telling me that I don't need the data type on the ArrayList side of the declaration.

I did notice that when I hovered over the ~ in IntelliJ, it wanted to change the ~ to String. Either way, I'm glad you haven't had to use it - I'll let it go:).

Thanks for taking the time to respond!

1 Answer

That's right, no declaration on the right. Kind of sad that I screwed it up, but I've fixed the original comment.

And you're welcome. Surprised no one had mentioned it before.