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.
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
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.
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
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.
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.