2

I came across this section in some class notes:

enter image description here

Java programs are what is known as statically and strongly typed.

I'm familiar with the concept of static or dynamic types, but haven't come across strong/weak types, and in looking them up there is always some wishy-washy definition.

In the above, what would be an example of how Java is strongly typed? Does it just mean that you cannot cast one type to another (but what about casting an int to a long or something of the same conceptual type)? How does the concept of strong/weak types relate to Java?

halfer
  • 19,824
  • 17
  • 99
  • 186
David542
  • 104,438
  • 178
  • 489
  • 842
  • *Example of how Java is strongly typed:* `x = "Foo"` will fail to compile because a string cannot be assigned to a variable of **type** `int`, i.e. the type of variable `x` is enforced, aka strong. – Andreas Mar 23 '21 at 03:14
  • Did you see the answers here? [Seeking clarification on apparent contradictions regarding weakly typed languages](https://stackoverflow.com/q/9929585/5221149) – Andreas Mar 23 '21 at 03:17
  • 1
    It's not about 'strong/weak *types*'. It is about strong or weak *typing,* i.e. whether the type system is strict or porous (like C for example). – user207421 Mar 23 '21 at 03:18
  • Isn't it due to usage, not the nature of the language? Both strings and integers can be assigned to the variable `Object x;`. –  Mar 23 '21 at 03:19
  • The duplink explains the strong/weak and the static/dynamic axes of type systems. – Stephen C Mar 23 '21 at 03:34
  • 2
    Another example that illustrates strong typing in Java would be `Object o = "42"; Integer i = (Integer) o;` which gives a runtime exception because an `String` is not an `Integer`. And no matter what you do using (pure) Java you can't make the typecast work. It is the "no matter what you do" that means that the typing is strong. – Stephen C Mar 23 '21 at 03:38
  • Your `int` <-> `long` example isn't apropos. The type casts there actually involve *conversion* of values from one type to another type. (That's illustrative of neither strong or weak typing. Though at any point, the compiler and the runtime will know what the notional type of the value currently is.) – Stephen C Mar 23 '21 at 03:47
  • The duplink is https://stackoverflow.com/questions/2690544/what-is-the-difference-between-a-strongly-typed-language-and-a-statically-typed. I've reopened because I can see value in answering the question specifically in the Java context. (But there may be a better duplink that makes this redundant ... if someone can find it.) – Stephen C Mar 23 '21 at 03:48
  • This may help: https://en.wikipedia.org/wiki/Strong_and_weak_typing – Slaw Mar 23 '21 at 05:03

2 Answers2

0

In a Weakly Typed language conversion between unrelated data types is implicit.

For e.g JavaScript is a Weakly Typed language :

var x = 22;
var y = x + "example";
console.log(y);  # prints 22example as an instance of String object
# Here the value of x got converted to string without any explicit conversion by the programmer.

A Strongly Typed language is exact opposite. You can do type conversion in Strongly Typed languages, but they are not implicit. You have to do them explicitly.

For e.g Java is a Strongly Typed language :

int x = 2;
Long y = x; # Will throw compilation error
# However you can do something like below example
Long y = new Long(x);
# There are other ways to do type conversion in java for different data types, but they are not implicit, you gotta do them explicitly.

I hope this answers your question.

Rakesh Swain
  • 58
  • 1
  • 6
-2

Strongly typed means, variable's data type has to be defined in program. This data type will be fixed at compile time. Unlike in loosely typed, where don't declare data type for variable in program. Data types are assigned in these cases at runtime.

Javascript is a loosely and dynamically typed language. Here we declare like : var age="old"; var income= 4000000; Here, age will be made string type and income will numbers type at runtime in JS.

Hope this contrast between Java and JavaScript helps in understanding this concept.

Rajnish
  • 58
  • 5
  • 4
    I don't think that's correct: "This data type will be fixed at compile time. " That's the definition of static (vs. dynamic) types, not strong vs. weak. – David542 Mar 23 '21 at 04:39