tags:

views:

224

answers:

5

In php/perl we can simply say $a='hi" and then $a=1 without needing to declare its type. But there are type casting errors in java for the same.

Why this difference?

+11  A: 

PHP is dynamically typed, while Java and C are statically typed.

With static typing, type checking can be done at compile time and this can catch many errors, so it's not necessarily a bad thing. This also allows them to be much faster than dynamic languages.

See: http://en.wikipedia.org/wiki/Type_system

NullUserException
@Downvoter: Explain
NullUserException
While I don't agree with the down vote, the answer lacks some explicit details. Specifically that type declarations are not required in a statically typed language, but in rare cases, depending on the language. Many modern static languages (like Haskell) and even many MLs don't require type declarations but in extremely rare cases.
jer
Thanks for your precise explanation and for posting the link.
Karthik Kottapalli
Go is a compiled language that is statically typed that doesn't require type declaration when a variable is declared. Most of this is just historical (what the language designers thought was right). If a variable is initialized to something, it should be easy for the compiler to automatically set the variable's type to be the same as what it is initialized as. For example in `a="hello"` it is obvious that `a` cannot be an integer or float or anything else but a string.
slebetman
@slebetman: Though in C there are multiple choices for the type of `a`. It could be an array of chars (of any size from 5 upwards); a pointer-to-char; a pointer-to-void; or any of those types with any combination of `volatile` or `const` qualifiers. If the initialiser was `a = {"hello"};` then the choices expand significantly.
caf
@jer, you're right, on the other side we cannot expect @Null to tell us the _whole_ story.
stereofrog
A: 

Java uses a so-called static type system. This means that every variable is defined to have a specific type at compile time, and this cannot be changed. Like any features, there are pluses and minuses to this. In general, the compiler can catch many more programming errors if the types are fixed, and generally produce more robust code.

Mike Axiak
Static type does not necessarily mean that type need to be explicitly declared. It could be inferred like in the Go programming language.
slebetman
Look at Haskell. It's a statically typed language with one of the most sophisticated type systems known to man, and yet hardly requires any explicit type annotations.
missingfaktor
A: 

Many (but not all) compiled languages use static typing because it helps the compiler make faster machine (or byte) code. It also helps the compiler find errors in your code.

mkoistinen
And in dynamically typed languages type errors are found at runtime,which is big headache.
Srinivas Reddy Thatiparthy
@Srinivas: In my 11 years of experience I've only ever encountered around half a dozen bugs caused by type errors. In my experience at least type errors are too insignificant (once every two years) to care about. But as in everything in life YMMV. My code tend to have fewer bugs if I write in a typeless or dynamic typed language compared to a typed language.
slebetman
+3  A: 

Just because a language is statically typed like the other answers here are saying, it doesn't mean it needs explicit type declarations. There are many type inference algorithms out there that work extremely well, needing only rare type declarations.

It just happens that in dynamic languages, they tend to (more often than not) not care so much about the type of an object, but rather that it respond to a specific set of behaviour (duck typing), so it doesn't typically matter for them what the explicit type is.

The type declaration hinting is helpful to the compiler, though not explicitly required for the common case (and depending on inference algorithm, only required in complex cases).

jer
+3  A: 

As others mentioned Java, C, C++ are statically typed. PHP, Perl, Ruby, Boo and so on are dynamically typed.

However some statically typed languages such as for instance C#4 supports dynamically typed programming as well.

dynamic x = 3;

Functional languages, C++, C# and others also supports type-inference which means it's still uses static types but the compiler infers the type.

auto x = 3; // C++0x

var x = 3; // C#

let x = 3 // F#

Why do statically typed languages suffer from type casting errors as well? Because they support inheritance and downcasting from superclasses to subclass. Downcasts can't in general be verified in compile-time but can be detected in run-time and will generate an exception.

FuleSnabel
Note : `auto` (that you are referring to) is not a part of C++-03(which is the current C++ Standard) but would be a part of C++0x.
Prasoon Saurav
Also simply `x = 3` in Go. It's statically typed but inferred.
slebetman
@Prasoon - Yes, changed the c++ comment to reflect that. Thanks for pointing it out.
FuleSnabel