-
Notifications
You must be signed in to change notification settings - Fork 2
2) Primitive Types and Strings
In Java, everything has a type. Java recognizes several primitive types. These types are stored using bits, which isn't terribly important right now. What's more important is knowing what these different types are and what they mean. Declaration and initialization are covered in the next section.
A boolean is a primitive type that can be either true or false. Booleans are used to describe characteristics that are one or another state, not more than two. For example, a boolean is good for the variable amISick because there are only two states, either sick or not sick.
An integer is just what it sounds like: an integer, meaning that it is a whole number either positive or negative. Integers can take any value from -(2^31) to (2^31 - 1). Integers are useful when counting things, but they can also be used as a replacement for booleans when there are three or more options. For example, the weather can be held as an integer, where CLOUDY = 0
, SUNNY = 1
, RAINY = 2
, SNOWING = 3
, etc.
A double is a 64-bit decimal number that can take on a wide range of values.
A character is a value that holds a single character and it can be anywhere from \u0000 to \uffff, or 65,535 values.
Beyond this, slightly more advanced primitive types lie...
An 8-bit number that can be from -128 to 127. Primarily used to save memory.
A 16-bit number that goes from -32,768 to 32,767. Also used to save memory instead of an int.
A 64-bit number that is used when you need more digits than an int can provide.
A 32-bit decimal number that is used to save memory instead of using a double.
Strings are just that: chains of characters. Because of this, they are NOT primitive types, but Strings often feel like primitive types due to the wide range of functionality.