Sunday, August 5, 2012

Primitive Data Types

I don't even see the code.
All I see is integer, double, String.
Before we can continue on to doing more interesting things with Java, we first need to take a look at the types of information that Java can work with. The vast majority of the operations in Java depend on a small set of basic data types, from which everything else can be built.

Integers, declared as int, can deal with positive and negative numbers with no fractional part. When doing math with them, if you would get anything after the decimal place, it is ignored completely. No rounding occurs. The int type is 4-bytes, and can store any number from -2,147,483,648 to 2,147,483,647. There are also variants of this type that can deal with a smaller or wider range of numbers, namely byte, short, and long.

Floating-point numbers, declared as double, can deal with positive or negative numbers, and can include a fractional part. Unlike the integer types, however, their math is inexact. Due to their inner workings, there are some numbers within their range that they simply can't represent. Unless you're doing something strange, though, they should behave very close to how you would expect. The double type is 8-bytes, and can store an extremely wide range of values - the minimum and maximum both have 308 digits, and for obvious reasons, I won't list them here. There is also a smaller variant of this type, namely float.

Boolean values, declared as boolean, can hold one of two different values: true or false. While that may not sound terribly useful at first glance, almost every decision-making structure in Java is based off of boolean values and comparisons between them. Because of this, you'll surely become very familiar with them throughout these lessons. For obvious reasons, there are no larger or smaller variants of this type.

Characters, declared as char, can hold a single Unicode character. They make up the basis of Strings in Java, which is the data type that holds text, such as "Hello world!". The good news is, you probably won't have to use them much or maybe even at all, because Java's String support is so good. A char is 2-bytes, and can hold any value from 0 to 65535, but is displayed as the corresponding letter or symbol.

Lastly character strings, declared as String, can hold any amount of text. They're quite ubiquitous, and are highly useful. While they're not technically a primitive type, I'm including them here because they're awesome (and they behave like primitives in a lot of ways).

End of Lesson Quiz

What type would you use to represent the number of times a user would like the program to do something?

[Show Answer]


What type would you use to represent whether or not the program should display error messages or simply ignore them?

[Show Answer]


What data type did we use in the previous lesson as part of the program to display Hello world! ?

[Show Answer]


What data type would you use to represent the amount of money in a bank account?

[Show Answer]


If you're working with the int type, what would the result be of 14 divided by 5?

[Show Answer]


Next Lesson: Variables

No comments:

Post a Comment