Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
229,608 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.