Thursday, August 05, 2004

Static and Dynamic Data Typing in Java

The concept of types is crucial as you progress as a developer. I have actually written java code about 7 years back without fully grasping this concept.

It involves understanding that a class is a type by itself (the implementation type) and can also comply with other types. You then begin to not just appreciate but ensure a clear separation between specification and implementation between interface and class. Even in simple method signatures you begin to be interested and careful about method specification, pre-conditions, effects and dependencies.

I want to focus on something else today that @requires an understanding of types in OO.
The concept and use of static and dynamic data types in java.

Application programs have to remember data. The simple way is to store them in variables.
int i =2; or Customer cust = new Customer("262621");

Every piece of data (including objects) has a type. And the type defines what you can do with that element. So type defines behavior. Types have hierarchies (remember need not be a single hierarchy) but a forest of hierarchies. From this we understand the concept of a generic (higher in the hierarchy) and specific (lower in the hierarchy) types.

A static type is a simple java class that defines fields and methods. I am really interested on how java types encapsulate data. In static data typing each field is statically declared with a type and a name. The variable (the container for data) has a specific type attached to it. Then you expose this field on the public interface of the class by suitable accessory.

Dynamic typing does not define static members. In dynamic typing the container for data has a generic type that can accommodate any kind of data. The problem this creates is that whenever you store (try to remember) data in such a container you loose information as to what the actual type was. The type of data has to be remembered along with the data element as the type of the container is not the type of the data element (as in static typing). More aptly the container type has no relation to the type hierarchy of the date elements that are stored in it. At compile time you do not know what these names or types are. It immediately implies that in a dynamic data type you do not have type safety. Now let me explain what is type safety and more what is the benefit of having type safety.

Consider a static type as follows

class Customer
{
public String getCustomerId()

public Date getBirthDate()

public String getLocationId()

.. fields and method body left out
}

Now, the application dictates that a Customer is allowed a 5% discount but Agents do not qualify for this discount. If Agent is another static type in the Application then the signature of the method to calculateDiscount( Customer argcust) is defined with an argument of type Customer. So only Customer objects can be passed as parameters.

Now suppose the application is dealing primarily with dynamic types and assume both Customer and Agent classes do not exist instead we use a class called DataObject to encapsulate data. Now that same method will be defined as calculateDiscount( DataObject argcust) .
Both Customer objects and Agent Objects appear to the application developer as DataObjects(the generic container for data). Now it is upto the developer to ensure that he does not inject a dataObject with the state of an Agent and pass it into the calculateDiscount() method. Even if he does, the compiler will not complain and will result in an runtime application bug.

The important question that an arise in your mind at this point is why go through all the pain of using dynamic types when you acheive it with static typing. True - Dynamic typing introduces a problem of lack of type safety. But it does provide certain benefits and moreover there are situations where you do not need static types at all. Those situations are characterized by the use static java beans with only state and very little behavior. Classes in the domain model immediately do not fall into this category since domain models primarily are designed around behavior. But there are many layers within an application that are essentially best suited to dynamic types. The use of static type in these situations results is unwanted coupling and also more development effort in creation and maintenance.

Some of the layers are
a) edge data parsing - either from the network or from files. XML & fixed width parsing included does not require static types.

b) Application Config Information - is fundamentally dynamic in definition.

c) Relational Data Managers - That reads data from RDBMS.

Further, it is far more powerful to use codegen or bytecode manipulation to generate static types from Dynamic type objects based on some type definition (XML Config). This layer then becomes a data mediator service between the world of static and dynamic types.

17 Comments:

Anonymous Anonymous said...

We stumbled over here by a different web page and thought
I might as well check things out. I like what I see so now i'm following you. Look forward to checking out your web page yet again.

Also visit my page ... wordpress websites

2/24/2013 6:05 AM  
Anonymous Anonymous said...

Thanks a lot for sharing this with all people you actually realize what you're talking approximately! Bookmarked. Kindly also seek advice from my web site =). We may have a hyperlink exchange agreement between us

Also visit my site templates do wordpress

2/25/2013 12:25 AM  
Anonymous Anonymous said...

Great site you have here.. It's hard to find excellent writing like yours these days. I really appreciate individuals like you! Take care!!

Also visit my page ... aminosäuren zum fettabbau

2/25/2013 3:50 PM  
Anonymous Anonymous said...

Hello, I enjoy reading through your article. I wanted to write a little comment to support you.


My homepage ... kochen für gäste
my site > Glutenfreidiät

2/27/2013 10:58 AM  
Anonymous Anonymous said...

You really make it appear really easy together with your presentation but I in
finding this matter to be actually something that I believe I might never understand.

It sort of feels too complicated and very vast for me. I'm taking a look ahead to your next submit, I'll try
to get the dangle of it!

Feel free to surf to my web page koch dich schlank

2/27/2013 11:23 AM  
Anonymous Anonymous said...

For most up-to-date information you have to pay a visit world-wide-web
and on web I found this site as a most excellent site for hottest updates.


Look at my web site; aufwendungen für arbeitszimmer

3/02/2013 7:15 AM  
Anonymous Anonymous said...

Thank you for some other informative website. The place else may just I get that kind of information written in such a perfect manner?
I have a challenge that I'm just now operating on, and I've been at the look
out for such info.

my web-site: gewichtsmanschetten selber machen

3/02/2013 4:26 PM  
Anonymous Anonymous said...

This article will assist the internet visitors for building up new webpage or even
a weblog from start to end.

Here is my website paleo bohnen

3/07/2013 9:43 PM  
Anonymous Anonymous said...

This is the right site for anyone who really wants to understand this topic.
You realize a whole lot its almost tough to argue with you (not that I personally would want to…HaHa).
You certainly put a brand new spin on a subject which has been written about for many years.
Excellent stuff, just excellent!

Here is my web blog; low carb crackers

3/07/2013 10:12 PM  
Anonymous Anonymous said...

What's up, yup this paragraph is genuinely nice and I have learned lot of things from it concerning blogging. thanks.

My website ... orkan lindenstraße

3/08/2013 12:35 AM  
Anonymous Anonymous said...

Hello there! Do you know if they make any plugins
to help with SEO? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good gains.
If you know of any please share. Cheers!

Also visit my homepage: herrenröcke

3/08/2013 1:25 AM  
Anonymous Anonymous said...

I'm not sure why but this web site is loading extremely slow for me. Is anyone else having this problem or is it a problem on my end? I'll check back later on and see if the problem
still exists.

Also visit my blog glutenfreie rezepte
Also see my website > 4 - monatskur bei zöliakie

3/08/2013 2:13 AM  
Anonymous Anonymous said...

Hello Dear, are you in fact visiting this site daily,
if so afterward you will absolutely take good
knowledge.

Here is my blog post ... autoradio ausbauen

3/08/2013 3:10 AM  
Anonymous Anonymous said...

After I originally commented I seem to have clicked on the -Notify me when new comments are
added- checkbox and now every time a comment is added I get four emails with the exact same comment.
Perhaps there is a way you can remove me from that service?
Appreciate it!

Also visit my blog post :: test paraflex 2.1
My web site :: lenkmatten anleitung

3/08/2013 11:12 AM  
Anonymous Anonymous said...

Why visitors still use to read news papers when in this
technological globe all is presented on web?

my web site - low carb chicken recipes

3/09/2013 3:45 AM  
Anonymous Anonymous said...

Hello, Neat post. There is a problem along with your web site in web explorer,
may check this? IE still is the market chief and a good part
of other folks will miss your magnificent writing because of
this problem.

my blog :: wordpress video kurs

3/11/2013 5:49 AM  
Blogger Swathi said...

Hi,
Very nice article. Very useful information is provided on static and dynamic data typing in Java. Thanks for posting.

http://kosmiktechnologies.com/java-training-institutes-in-kukatpally/

6/30/2017 3:24 AM  

Post a Comment

<< Home