Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Generic parameterized type

 
Ranch Hand
Posts: 119
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I know the convention for declaring generic parameterized type should be single uppercase letter.

What if I purposely use the word "Integer" as my parameterized type in my class declaration, does that mean I cannot declare Integer object in my class?

 
Marshal
Posts: 26531
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's why the designers of Java chose to use single-character names like "T" for their type parameter names. When you do that there's no chance of reusing the names of actual types.
 
henry leu
Ranch Hand
Posts: 119
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
Paul Clapham
Marshal
Posts: 26531
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:When you do that there's no chance of reusing the names of actual types.



I say "no chance" but that's under the assumption that you didn't also create an actual class whose name is "T"; if you did then you would run into problems trying to use it in a context where "T" is also a type parameter. But you'd only do that if you were writing a certification exam and wanted another way to bamboozle the test-taker.
 
Saloon Keeper
Posts: 4380
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry leu wrote:


Strictly speaking you CAN specify a genuine Integer inside the body, but you would have to use the full name of it (was it: java.lang.Integer)? For instance:
 
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Paul C. Use a single letter name for your formal type parameters, so it will be different from the names of any “real” types We sometime see people gettting confused because they use the name of a “real” type as a formal type parameter and think the compiler will use that type. If you really want to use Integers, writeOf course, Integer is a final type, so it has no subtypes, so the above declaration is pointless. It will behave no differently from this:-You could try something like this,I tried it on JShell

jshell> Foo<BigInteger> f = new Foo<>(new BigInteger(new byte[]{(byte)0, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++, b++}));
f ==> Foo 390065088506505629166730488854071659607279725 ... 13156909304223548092050155

jshell> f
f ==> Foo 390065088506505629166730488854071659607279725441223082463377213156909304223548092050155

The ++ operator means I get a different result each time; it would probably come full circle after 64 repetitions.
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic