DEV Community

Cover image for Effective Java: Avoid Strings When Other Types Are More Appropriate
Kyle Carter
Kyle Carter

Posted on • Edited on • Originally published at blog.scaledcode.com

Effective Java: Avoid Strings When Other Types Are More Appropriate

Often when receiving data from files, HTTP requests, keyboard input, etc. we receive that data as a String. Often we just keep these variables as a String even if a different variable type is more appropriate. The most appropriate type depends on what we are trying to represent; if we are trying to represent a finite set of items we may use a enum, true/false means boolean, numbers could be an int, long, or even BigInteger. The benefits of using the appropriate type are as follows:

  • Performance improvements with not having to convert back and forth between the String type.
  • The built in functions of the type work correctly for that type such as equals, hashcode, etc.
  • Less error prone.

It can sometimes take a little more effort to save our variables as the correct type, especially if they are provided as a String initially. The payoff is worth it and will keep your code much cleaner.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay