mizech mizech - 11 months ago 51
Java Question

How does Java check if an assigned value is correct (in regard to the type)?

In Java you have to specify of which type a variable has to be.

It then makes sure that one can't assign values of a different type to the variable.
Trying to assign a different value results in an error: "String can not be converted to int." for example.

How does the checking work?

Where are the information about the variable-type stored?

In a weakly typed language like JavaScript assigning a string to a current number wouldn't result in an error.

How does it work there?

As far as I know determines the type the way values are stored in memory?
Consequently: How can a variable change it's in weakly typed languages? The way of storage wouldn't match anymore.

Answer Source

Java goes through a process of compilation. During this process, the compiler verifies that the type of the variable is consistent. Javascript is not a compiled language. There's no pre-runtime process to verify the type of the variable. More over, javascript is a dynamically typed language, so by design you don't have to worry about the type of the variable. You can read more about it here: Dynamic type languages versus static type languages