File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Static Types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Types" Watch "Static Types" New topic

Static Types

Charles Nodell

Joined: Apr 20, 2010
Posts: 6
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.


pete reisinger
Ranch Hand

Joined: Dec 30, 2009
Posts: 46
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

Joined: Aug 16, 2005
Posts: 15080

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Charles Nodell

Joined: Apr 20, 2010
Posts: 6
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.
I agree. Here's the link:
subject: Static Types
It's not a secret anymore!