473,411 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

little bit confused with generic class

dmjpro
2,476 2GB
today i start learning generic.
i m from c and C++ background so i have some knowledge about template and in java the generics is similar to template in c++.
right?

look at my code carefully .....

Expand|Select|Wrap|Line Numbers
  1. public class MyGenericClass<e>
  2. {
  3.   private e data;
  4.   public MyGenericClass(e d){data = d;}
  5.   public e getData()
  6.  {
  7.     return e;
  8.  }
  9. }
  10.  
  11. //With this code no problem
  12. //but the problem is when i access this class.
  13. //this is the code part
  14.  
  15. MyGenericClass<int> c = new MyGenericClass<int>(100);
  16.  
  17. //Here the compiler tells that the type is not found.
  18. //I understand primitive type is not supported, only class or interface type is //suppoerted.
  19.  
could anyone tell me why sun did this?

kind regards,
dmjpro.
Jun 13 '07 #1
1 1127
JosAH
11,448 Expert 8TB
today i start learning generic.
i m from c and C++ background so i have some knowledge about template and in java the generics is similar to template in c++.
right?

look at my code carefully .....

Expand|Select|Wrap|Line Numbers
  1. public class MyGenericClass<e>
  2. {
  3.   private e data;
  4.   public MyGenericClass(e d){data = d;}
  5.   public e getData()
  6.  {
  7.     return e;
  8.  }
  9. }
  10.  
  11. //With this code no problem
  12. //but the problem is when i access this class.
  13. //this is the code part
  14.  
  15. MyGenericClass<int> c = new MyGenericClass<int>(100);
  16.  
  17. //Here the compiler tells that the type is not found.
  18. //I understand primitive type is not supported, only class or interface type is //suppoerted.
  19.  
could anyone tell me why sun did this?

kind regards,
dmjpro.
Generic types are types, not primitives; you can't use 'int' for the puropse, you
have to use the 'Integer' wrapper class for that. Note that autoboxing takes away
most of the notational burden. Generic classes are very much unlike class templates,
i.e. a class template is instantiated to a templatized class as soon as you 'use'
(parts of) that class; it's named the 'POI' (Point of Instantiatiion); due to C++'s
separate compilation the same (parts of a) class can be instantiated over and
over again if you're not careful ("code bloat").

Generic classes don't instantiate anything at all: it's just the compiler that uses
a primitive class as if it were coupled to the type parameters and it checks for
it. After compilation is over and done, the type parameter(s) is/are said to be
'erased' from the class and all that is left is the same primitive class, e.g. List<E>
and List<F> are/is the same List and no code is generated or instantiated.

kind regards,

Jos
Jun 13 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: CMM | last post by:
Do the developers of Visual 2005 actuall use it??? There's lots of great things in VS2005 (mostly related to the outstanding work done on the CLR)... but in general the LITTLE THINGS totally drag...
11
by: iTISTIC | last post by:
Developing a new app and am trying to make this my first truly OOP/3-Tier app. I understand the principles of the presentation, business, and data layers. I do, however, have some questions on...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
0
by: shapper | last post by:
Hello, I followed some examples in internet and in MSDN web site to create a serializable class. Basically, I need to serialize a class to binary so I can save it in an SQL 2005 table using a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.