Java basics Part 3 – Different data types

This part is based different data types that are basically used in basic codes. For the beginners with these tools to use you are able to make richer codes and different variations.

Basic theory

Why there is a different data types you may fonder. Well the answer is simple, different data types serves in different purposes for example “String” -data type allows you to type numbers and characters of the alphabet when “int” allows you to type natural numbers. Of course there different variations from int because not all types use natural numbers such as prices. Let’s say that something costs 2,43€ and if your data type is int your program could crash. To fix this solution using double/float as your data type, you shouldn’t have any worries.

Here is a little list about different data types, they are also primitive types of data.

  • Byte
  • Short
  • Int
  • Long
  • Float
  • Double
  • Boolean

And then there is “String” -type of data that isn’t primitive data. With string you are able to type text and numbers.

Why to consider using different data types

Well there are couple good reasons, first using short takes less memory than long. Secondly sometimes it’s much more easier to makes calculations when data type is already in correct form, for example giving numbers in string data type makes them non-mathematical numbers.

Using different types of data types also can make your code richer or dramatically confuse you, when trying to find errors.

Conclusion

Different data types are made to make your code richer and more flexible. Sometimes using different data types can also be mandatory especially when getting data from user.

– Tuomas Törmä

Sources

  •  Studies in Haaga-Helia Autumn 2013 course Introduction to Programing Teacher Lahtinen Seija
  • Try and err

EDITED:19.11.2013,

Leave a comment