Static types and statically typed

I am from java but now i am learning golang i have seen static types in java but i could not understand what STATIC is can anyone help me in understanding this concept in easy words and how does it relate to “golang is statically typed language” ?
thanks

Golang being statically typed means that all variables you declare will have an unchanging type such as int, string, or some user defined type. Because of this there is no fuzzy type behavior where you can place an int value in code that expects a string hoping to get the string behavior of the int value. Can’t do it in go, but this makes the language much more error retardant.

Honestly to say you can’t do it isn’t true, you could use empty interface{} to design functions that could provide this behavior if you want it.

1 Like

Thanks @CurtGreen
and static in java is same or it is something else ?

In Java the static keyword has a number of uses depending on its context, see this page: https://www.geeksforgeeks.org/static-keyword-java/

1 Like

this article was very helpful thanks a lot @CurtGreen

Java and Go are both statically typed, but this has nothing to do with the static keyword in Java (which in fact is different from the same keyword in C, which again is still statically typed).

So static typing simply means, that the type of each piece of data is known statically at compile time and can’t change.

Usually you also can’t change the type of a variable afterwards, but at least in Go, sometimes it feels as if you could, due to shadowing.

Both, Java and Go have some type that is a “supertype” of all others. In both cases, you can’t do much with those without doing runtime typechecks. In go you use typeassertions, in java you use instanceof.

In contrast to static typing there is dynamic typing, which basically means, that no types are known at compiletime but only at runtime. Most scripting languages use this kind of typing.

Also there are weak and strong typing. Those words explain how types are converted/coerced from one to the other. In strong typing data doesn’t get converted without making it explicit, while with weak typing some typeconversion happens implicitely (eg JavaScript where 1 + [] is possible and results in "1").

Usually you classify a typesystem in a 2 dimensional grid using “weak/strong” on one axis, “static/dynamic” on the other.

2 Likes

Thanks @NobbZ
It was informative

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.