Java Bootcamp: Learn the Basics of Java Programming in 2 Weeks (2014)
Chapter 3. Data Types and Variables
In this chapter, you will learn about the different data types that you will work on, as well as assigning value to a variable. You will also learn the reserved words that you should not use as variable name.
The Integer (int)
An integer is either a positive or negative number. Zero is also considered an integer.
int is short for integer in Java programming, and you will know its use later. You don’t need to enclose the integer in quotes if you want to print it.
Take a look at this program:
|
1 |
public class IntPrinting { |
||||||||
|
2 |
public static void main(String []args) { |
||||||||
|
3 |
System.out.println(10); |
||||||||
|
4 |
} |
||||||||
|
5 |
} |
||||||||
It will yield this result:
|
10 |
|||||||||
The int data type can only accept the values between -2,147,483,648 and 2,147,483,647.
Any number that you put inside the parentheses of ‘System.out.println();’ will be printed, as long as you follow the correct syntax like the given example.
The Boolean (trueorfalse)
A boolean data type can only be either true or false. It won’t yield any other answer. You don’t need to put it in quotes because the program recognizes true and false as the legitimate value for boolean.
Take a look at the sample program:
|
1 |
public class BooleanPrinting { |
||||||||
|
2 |
public static void main(String []args) { |
||||||||
|
3 |
System.out.println(true); |
||||||||
|
4 |
} |
||||||||
|
5 |
} |
||||||||
It will yield this result:
|
True |
|||||||||
Try replacing true inside the parentheses with ‘me’, like this:
|
1 |
public class BooleanPrinting { |
||||||||
|
2 |
public static void main(String []args) { |
||||||||
|
3 |
System.out.println(me); |
||||||||
|
4 |
} |
||||||||
|
5 |
} |
||||||||
It will yield this result:
|
/tmp/java_FlX6ZW/BooleanPrinting.java:3: error: cannot find symbol |
|||||||||
|
System.out.println(me); |
|||||||||
|
^ |
|||||||||
|
symbol: variable me |
|||||||||
|
location: class BooleanPrinting |
|||||||||
|
1 error |
|||||||||
It does not recognize the word ‘me’ because it is a string. When printing strings, you need to write it within the quotes.
The Character (char)
The character or char data type represents a single character. All values must be enclosed in single quotes; otherwise you will get an error message.
Take a look at the sample below:
|
1 |
public class BooleanPrinting { |
||||||||
|
2 |
public static void main(String []args) { |
||||||||
|
3 |
System.out.println('r'); |
||||||||
|
4 |
} |
||||||||
|
5 |
} |
||||||||
It will give this result:
|
r |
|||||||||
The int, boolean, and char are Java’s basic data types. You will encounter more of them later.
Using Variables
You can store a value to a variable in Java programming, and other programming languages have such feature as well. When you use a variable in Java, you need to specify the data type.
Here is the list of the Java reserved words that you should not use as variables:
|
abstract |
assert |
boolean |
break |
long |
|
byte |
case |
catch |
char |
private |
|
class |
const |
continue |
default |
short |
|
do |
double |
else |
enum |
switch |
|
extends |
final |
finally |
float |
native |
|
for |
goto |
if |
implements |
new |
|
import |
instanceof |
int |
interface |
package |
|
transient |
protected |
public |
return |
super |
|
synchronized |
static |
strictfp |
throws |
this |
|
volatile |
while |
try |
throw |
void |
Remember that you should not begin your variable in upper case, although you can use upper case within the variable name like this:
int myFavoriteNumber = 10;
You need to write int in the beginning to identify the data type, ‘myFavoriteNumber’ is the variable name, and ‘10’ is the value assigned to it.
Try this on your own:
1. The int variable mySampleNumber must be equal to 6.
2. The boolean variable mySampleAnswer must be equal to true.
3. The char variable mySampleLetter must be equal to L.
Just assign each variable the value that the instruction tells you to assign. When you run the program you won’t see the values printed onscreen. If you want to print the values, then you need to add System.out.println() in your program, how will you do it? Hint – each variable that you need to print must have its own System.out.println(), make sure to write the correct one and it should not be the real value of the variable.
Just this once, you need to add these lines to your program:
|
8 |
System.out.println(mySampleNumber); |
|||
|
9 |
System.out.println(mySampleAnswer); |
|||
|
10 |
System.out.println(mySampleLetter); |
You will get this output:
|
6 |
|||||||||
|
true |
|||||||||
|
L |
|||||||||
Now you’re ready for a more challenging, yet fulfilling lessons.
All materials on the site are licensed Creative Commons Attribution-Sharealike 3.0 Unported CC BY-SA 3.0 & GNU Free Documentation License (GFDL)
If you are the copyright holder of any material contained on our site and intend to remove it, please contact our site administrator for approval.
© 2016-2026 All site design rights belong to S.Y.A.