File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Strongly typed and Weakly typed languages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Strongly typed and Weakly typed languages" Watch "Strongly typed and Weakly typed languages" New topic
Author

Strongly typed and Weakly typed languages

Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

Hi,

What is the difference between Strongly typed and Weakly typed languages? What is the type of Java?

Also what is the difference between Static typing and dynamic typing.

Confused if strongly typed is same as statically typed?

Any thoughts appreciated.


Java Experience
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Check out the Wikipedia article about that. Strong typing
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

In a statically typed language, you declare variables before you use them. In a dynamic one, you can use any variable at any time, as long as you initialize them before you use them.

In a strongly typed language, variables have a type, and they will always be that type. You can not assign a value of one type, to a variable of another type. In a weakly typed language, variables don't have a type. You can assign anything to them.

Java is a statically, strongly typed language. It is statically typed, because you always have to declare your variables before you use them. It is strongly typed, because a variable of type int will always hold int values. You can't assign booleans to them.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Stephan van Hulst wrote:It is strongly typed, because a variable of type int will always hold int values. You can't assign booleans to them.

But you can assign short and byte values to a variable of type int, so perhaps it isn't strongly typed after all?

That's why I posted the link to the Wikipedia article. It isn't just "yes" or "no" for strongly typed, it's (as usual) actually more complicated than that.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

I disagree in Java's case. short and byte values are cast to int implicitly before the actual assignment takes place.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

Perl is a weakly-typed language. I can declare a varible

my $money = "123";

Then, depending on the context, Perl will treat it as either a string or a number. I can do this:

or i can do this:

This may not be the best example, since Java will automatically call an object's .toString() method, mimicking what you see here...except for the fact that i initialized my variable with a string and then used it as an int.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Stephan van Hulst wrote:I disagree in Java's case. short and byte values are cast to int implicitly before the actual assignment takes place.

I don't want to take a position on that one way or the other, although it would be possible to disagree with your disagreement. I'm just pointing out that it's more complicated than a one-line explanation.
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

I have got the difference between strongly typed and weakely typed.
Some points that I have digested:
1) Strongly typed means variables have some data type associated with them.
2) In strongly typed language, compiler error is flashed if a variable doesn't has a type whereas weakely typed will try to interpret the type by using the value assigned to the variable.
3) We can't say that a language is strongly typed or not. We can compare languages to be more strongly/weakly typed.

Though I have also got some understanding of statically/dynamically typed in the sense that variables have been declared before use or not. But I have a question here.

In dynamically and strongly typed also we are using the variable a without declaring it.
int a = 2 + 3; //line 1
In a statically and strongly typed language we can declare a variable as:
int a = 2 + 3; //line 2
or
int a; //line3
a = 2 + 3; // line 3

So where is the difference.
Is line 2 above shows that the language in question is partially static and dynamic language.

Please clarify....
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

In a dynamically and strongly typed language you should be able to do something like this:

foo = 2 + 3; // foo has never been declared before
foo = "bar"; // runtime exception

In a dynamically typed language, you don't have to declare variables at all. They infer their type from their initialization, which in this case is 5, hence an int. Because the language is strongly typed, assigning a String reference to foo would then raise an exception. The type of the variable is determined at runtime, so the compiler can't do the checking for you.

In a weakly typed language, there is no such thing as static or dynamic typing, since variables don't have a type to start with. They don't care what you assign to them.
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

I have written a blog for all this at
Statically vs Dynamically typed and manifestly vs implicitly typed languages, Strongly vs Weakly typed

Those who may read at later stage, please concentrate on the words Static, Dynamic, Stong, Weak, Manifest and Implicit to get a clear picture.

Its really confusing to understand this stuff. Also, please use the following links:

http://en.wikipedia.org/wiki/Type_system
http://pythonconquerstheuniverse.wordpress.com/category/java-and-python/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strongly typed and Weakly typed languages