Hi everybody… Hope you are all okay.
In this tutorial, I’m going to teach you about the
fundamental data types and operators in Java. Now we start learning the lingo
so that when we are thrown on planet Java, we know how to tell the natives what
we want… Cool?
When I was learning Java, I found it helpful to think of datatypes
as buckets or containers that hold data. So you need to know the best container
for the data you want to work with. The data type you use should be of the
right type and size. For example, if you have 110 liters of water, you wouldn’t
think of putting all the water in a 10-liter container. Also, you wouldn’t put
clean drinking water in a petroleum tank.
When it comes to whole numbers, we have three Java data types
that can hold them: short, int and long. So these data types can hold
numbers such as 0, 25, 1,000,000 etc.
So in Java, we can declare integers as follows:
short i = 15; We read this as: a short integer labeled i with a value 15.
int j = 986; We read this as: an integer labeled j with a value 986.
long l = 169; We read this as: a long integer labeled l with a value 169.
You may be thinking, “what is the difference between short, int and long?
The difference is the size of the value it can hold:
Minimum value
|
Maximum
value
|
Size
|
|
short
|
-215
|
+215-1
|
16 bits
|
int
|
-231
|
+231-1
|
32 bits
|
long
|
-263
|
+263-1
|
64 bits
|
When it comes to decimals or numbers that have fractions, we
have two Java data types that can hold them: float and double.
Examples of decimal numbers are 3.14159, 22.6, 0.5 etc.
So in Java, we can declare decimals or numbers with
fractional values as follows:
float f = 22.4; We read this as: a floating point number labeled f with a value 22.4.
double d = 2.5879; We read this as: a double number labeled d with a value 2.5879.
You may be thinking, “what is the difference between float and double?
The difference is the size of the value it can hold:
Minimum
Value
|
Maximum
Value
|
Size
|
|
float
|
1.4E-45
|
3.4028235E38
|
32 bits
|
double
|
4.9E-324
|
1.7976931348623157E308
|
64 bits
|
When it comes to textual data, we have two data types that
can hold them: String and char. Examples of strings are “Brian”, “Kenya”,
“Europe” etc. Examples of characters or char
are ‘x’, ‘z’, ‘p’ etc.
So in Java, we can declare strings and char objects as
follows:
String name = “Brian”; We read this as a string labeled name that has the value “Brian.”
char c = ‘r’; We read this as a character labeled c that has the value ‘r.’
You may be thinking, “what is the difference between String and char?
Minimum
Value
|
Maximum
Value
|
Size
|
|
char
|
Unicode 0
|
Unicode 216- 1
|
16 bits
|
Boolean values can be either true or false but not
both. For example, a switch can either be on or off, but not both. We declare a Boolean value as follows:
boolean isChecked = true; We read this as a boolean type labeled isChecked whose value is true.
boolean isChecked = false; We read this as a boolean type labeled isChecked whose value is true.
A byte type value has the following characteristics:
Minimum
Value
|
Maximum
Value
|
Size
|
|
byte
|
-128
|
+127
|
8 bits
|
In my experience, I’ve not seen the byte being used a lot in programs.
So there you have it: Above are the fundamental datatypes in
Java. Consider them containers or buckets that can hold data. When you write int j = 5; you are telling Java to
create a ‘bucket’ that can hold an integer value and put 5 into that ‘bucket’.
Please note that in the above examples, I use the word labeled a lot. This shows that you can
give the datatypes you use any name. If you want to store an integer value,
first decide whether a short, int or long suffices. The name you give it is up
to you. However, labels shouldn’t start with numbers. They should also start
with small letters by convention. Class names should start with capital
letters. I will explain to you in a future tutorial why doing this is important.
Figure 1: The program that shows different datatypes and the values I’ve assigned to them.
From the last tutorial, we learnt that System.out.println(); prints whatever is between the parentheses to the console. So we place the labels of the data we created between the parentheses, e.g. System.out.println(charValue);
Figure 2: Results as shown on the console after running the
program.
No comments:
Post a Comment