3

I don't have an actual problem, I was just playing around with Java Generics and I realized that if I create a class with Generics, I can use any name for the generic part of the class (so inside the <>), it will be treated as a non-existing class, as a variable.

So if I use String as the generic part, it won't be String anymore, so for example you can't use its charAt() method.

I guess, this is a feature, because normally, if I know that I will want to use a String than I simply use it, and I don't create a generic class.

Am I right?

7
  • the meaning of a name in java highly depends on the context. In the same .java file, the same name can be used to mean different things. e.g. see stackoverflow.com/q/17690135/2158288 Commented Sep 25, 2015 at 17:10
  • 2
    you can limit a generic type parameter to extend an existing type, in which case you could use methods for that type. is that what you mean? Commented Sep 25, 2015 at 17:12
  • 1
    That's because the name between the <> is just a name - the name of a type variable. Just like you can give regular variables the same name of a class. It's not a good idea to do that, because it's confusing. Commented Sep 25, 2015 at 17:13
  • yes, I know that, but from this answer I see that I understood the thing correctly, thanks Commented Sep 25, 2015 at 17:13
  • 1
    It's a good question, unfortunately the JLS seems to be a bit unclear on this issue. What is important to know though is the concepts of shadowing and obscuring, which is what you're witnessing in your example. And a type variable counts as a variable according to these rules. Commented Sep 25, 2015 at 17:16

2 Answers 2

4

You can still use any classes with the same name if you use the full name including the package, e.g. java.lang.String.

The issue is actually the same as if you create a class named String in your package. You can do that, but then you also need do live with the consequences that classes in the same package will use this class instead of the standard String class.

Sign up to request clarification or add additional context in comments.

2 Comments

The meaning of java.lang.String is also very ambiguous :) It could also mean variable access for example
@bayou.io Yes, it is relatively easy to create an example where there's no way to access something by either simple or qualified name.
1

The generic allow us to use the single implementation with different type.

For example, consider the collection framework from java. The List<T> is a generic type you can create an List of any type in java. It provide you different useful operation which you can perform on different types like wrapper classes of primitive type.

If you use the generic for your custom type, then you might need to specify some implementation to support the generic functionality. You can still use the general behavior of types as well.

List<String> list = new ArrayList<String>();

This is a list of type String. You can create any List like wise. Now you can use many functionality provided by java easily. Now if you need the same functionality for Integer, then you need to re-write the same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.