Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

David Smith
David Smith
10,577 Points

In Java's variable declaration deep dive.

When declaring a variable in Java you declare the type before the variable name however in a language like Python or Ruby you would simply declare the name and then put something inside of it.

My questions are, why do we do this in Java? and what advantages/disadvantages does it have to declare the type first?

2 Answers

Steven Parker
Steven Parker
215,939 Points

Java is an example of a statically-typed language, in which type checking is done at compile-time; whereas Python is a dynamically-typed language in which type checking is done at run-time.

Advantages commonly cited of statically-typed languages:

  • Better code completion.
  • Better performance (more opportunities for compiler optimizations).
  • Easier for IDE's to provide hints and documentation while you code.
  • It’s easier for the IDE to find where things are declared and referenced in the code.
  • It's easier to work with relational databases and other systems which also rely on static types .
  • It can help reduce the likelihood of some kinds of errors.
David Smith
David Smith
10,577 Points

Thank you Steven, that was very helpful.