Intro To Java: Section 2.3 - Primitive Data Types
So far we have learned how to use the data types integers to store numbers and strings to store text. Now we will learn about all the other primitive data types in Java.
Primitive Data Types
Primitive data types are built in elements of Java which allow you to store data. Integers are a type of primitive data type, but there are seven more. They are called "primitive" because these are the basic types of variables which we use to create all other objects in Java. The table below outlines the purpose of each primitive data type.
Data Type | Keyword | Min/Max Value | Description |
Byte |
byte |
-128 & 127 |
Used to store whole numbers. 8 bit - Smaller than an integer - useful for saving memory when working with small values. |
Short |
short |
-32,768 & 32,767 |
Used to store whole numbers. 16 bit - Smaller than an integer but larger than a byte. |
Integer |
int |
-(2^31) & (2^31)-1 |
Used to store whole numbers. 32 bit. |
Long |
long |
-(2^63) & (2^63)-1 |
Used to store whole value numbers. 64 bit. |
Float |
float |
Out of scope |
Used to store numbers including decimals. 32 bit. Due to rounding issues, this should not be used to store currency. When assigning values must end in f. |
Double |
double |
Out of scope |
Used to store numbers including decimals. 64 bit. Due to rounding issues, this should not be used to store currency. |
Boolean |
boolean |
True & False |
A boolean is a simple type of variable that can only be either true or false. |
Character |
char |
0/65,535 |
Represents a 16 bit unicode character. You may simply enter in the character instead of a number. |
The first four primitive data types in the table above are all used to store whole value negative and positive numbers. The one you use simply depends on how large of a number you need to store. You should always use the smallest type possible to achieve your goals in order to conserve memory. The following example uses all of the first four primitive data types.
public class HelloWorld{
public static void main(String []args){
//Create variables
byte myByte = 1;
short myShort = 10;
int myInt = 100;
long myLong = 1000;
//Add them all up
long result = myByte + myShort + myInt + myLong;
//Print out the result
System.out.printf("The result is %d",result);
}
}
The result is 1111
Floats and Doubles
When you need to perform operations that involve decimal places you will most often use floats and doubles. The example below calculates the area of a circle. Note that you can get the value of Pi using Math.PI. Instead of using the format specifier %d, %f was used instead. %f is used for floating point values and can contain decimals.
public class FloatsAndDoubles{
public static void main(String []args){
//Initialize Variables
float radius = 1.5f;
//Calculate area (pi*r^2)
double circleArea = Math.PI*radius*radius;
System.out.printf("The area of the circle is %f units squared.", circleArea);
}
}
The area of the circle is 7.068583 units squared.
It is possible to declare the level of precision you would like in your float output. The example above output to 6 decimal places. If, for example, you only wanted to go to the second decimal place, you add a ".2" to the format specifier.
System.out.printf("The area of the circle is %.2f units squared.", circleArea);
The area of the circle is 7.07 units squared.
Booleans
Booleans are a simple primitive data type that can only store the values true and false.
public class BooleanExample{
public static void main(String []args){
boolean someValue = false;
System.out.println("someValue is: " + someValue);
}
}
someValue is: false
Characters
Character primitive data types are defined using the keyword
char. Chars are stored as numbers which correspond to a 16bit unicode character. Note in line 3 how aChar was set to a by wrapping the a in two single quotation marks rather than in double quotation marks.
public class CharExample{
public static void main(String []args){
char aChar = 'a';
System.out.println("The value of aChar is: " + aChar);
}
}
The value of aChar is: a
0 comments:
Post a Comment