From the course: Java: Generic Classes

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Rules and naming conventions

Rules and naming conventions - Java Tutorial

From the course: Java: Generic Classes

Rules and naming conventions

- [Instructor] There are a couple of rules that you must know at this point in order to apply or use generics in your code. Let's look at them. The first rule is that generics works only with reference types. This essentially means that you can never use primitives with generics. Let's go to the IDE and write some code for this. So let's create another class here on the same package as the previous one, and let's name this class as Rules. In this, I'll write the main method. And now, let's create a list of ints, and this would be primitive ints, so List, that is java.util, the angle brackets, and this time, the type argument is the primitive int. Let's name this variable as integers, and let's create new ArrayList. Similarly, let's also label the type argument as int here, but since now, we have the diamond operator, there is no need to do that, so we can eliminate this. Now, if you see, you have compilishing problem…

Contents