Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Types

 
Charles Nodell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to programming in general and am now trying to learn Java. I keep reading about how Stat Typing makes a program safer/more secure. I am having a hard time understanding why. Could some one explain to me how Static Typing makes things safer/more secure? I come from a python background and have never really dealt with Static Types before and am eager to learn.

Thanks!
 
pete reisinger
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think that safer means that everything is strict, so if I specify method:
public boolean setAge(int age) { ... }

Everybody knows that method returns boolean (true/false) and the parameter is integer. And this is for 100%.
now you don't have to check the value if it's int - like you would have to do in python, using some function etc.
but you can just work with it. It is safe to use the value as integer because it cannot be anyghing else.
And you have to return true or false (no 0 or 1, or null. just true or false). So other people that use your method
will know for sure that it returns boolean.

if you want to know exact meaning of static typing, check on wikipedia
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In statically typed programming languages such as Java, the compiler does a lot more checks on the source code than in dynamically typed languages such as Python (in dynamically typed languages, those checks are done at runtime).

That's good, because it means type errors (using the wrong datatypes, for example trying to put a string in a variable that's supposed to hold a number) are caught when you compile the program. With dynamically typed languages, such errors are caught much later, when you run the program - and it might not be you who finds the error, but the users of your program, which is ofcourse bad.
 
Charles Nodell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jasper. I read the compile time vs. run time thing, but I didn't really see the importance. I can see how it is good to know about the issue before it gets to the end users. I guess it is an extra level of testing done by the compiler that would not happen with a language like python.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic