Data Types: JAVA For Beginners
Data Types: JAVA For Beginners
Data Types: JAVA For Beginners
Predicted Output:
Data Types
The following is a list of Java’s primitive data types:
The ‘String’ type has not been left out by mistake. It is not a primitive data type, but strings (a
sequence of characters) in Java are treated as Objects.
class Example8 {
Riccardo Flask 13 | P a g e
JAVA for Beginners
var = var / 4;
x = x / 4;
Predicted output:
One here has to note the difference in precision of the different data types. The following example
uses the character data type. Characters in Java are encoded using Unicode giving a 16-bit range, or
a total of 65,537 different codes.
class Example9 {
char ch;
ch = 'X';
ch++; // increment ch
Riccardo Flask 14 | P a g e
JAVA for Beginners
Predicted Output:
ch is now X
ch is now Y
ch is now Z
The character ‘X’ is encoded as the number , hence when we increment ‘ch’, we get character
number , or ‘Y’.
The Boolean data type can be either TRUE or FALSE. It can be useful when controlling flow of a
program by assigning the Boolean data type to variables which function as flags. Thus program flow
would depend on the condition of these variables at the particular instance. Remember that the
output of a condition is always Boolean.
class Example10 {
boolean b;
b = false;
b = true;
b = false;
Predicted output:
b is false
b is true
This is executed
10 > 9 is true
Riccardo Flask 15 | P a g e
JAVA for Beginners
class Example11 {
int a,b,c;
a = 2;
b = 3;
c = a - b;
System.out.println();
c = b - a;
Predicted output:
c is a negative number
c is a positive number
The ‘if’ statement evaluates a condition and if the result is true, then the following statement/s are
executed, else they are just skipped (refer to program output). The line System.out.println() simply
inserts a blank line. Conditions use the following comparison operators:
Operator Description
< Smaller than
> Greater than
<= Smaller or equal to, (a<=3) : if a is 2 or 3, then result of comparison is TRUE
>= Greater or equal to, (a>=3) : if a is 3 or 4, then result of comparison is TRUE
== Equal to
!= Not equal
Riccardo Flask 16 | P a g e
JAVA for Beginners
The for loop is an example of an iterative code, i.e. this statement will cause the program to repeat a
particular set of code for a particular number of times. In the following example we will be using a
counter which starts at 0 and ends when it is smaller than 5, i.e. 4. Therefore the code following the
for loop will iterate for 5 times.
class Example12 {
int count;
System.out.println("Done!");
Predicted Output:
This is count: 0
This is count: 1
This is count: 2
This is count: 3
This is count: 4
Done!
Instead of count = count+1, this increments the counter, we can use count++
Riccardo Flask 17 | P a g e
JAVA for Beginners
Blocks of Code
Whenever we write an IF statement or a loop, if there is more than one statement of code which has
to be executed, this has to be enclosed in braces, i.e. ‘, …. ’
class Example13 {
double i, j, d;
i = 5;
j = 10;
if(i != 0) {
d = j / i; Block of
Code
System.out.print("j / i is " + d);
System.out.println();
Predicted Output:
j/i is 2
Hints:
The Euro Converter has been provided for you for guidance. Note loop starts at 1 and finishes at 100
(<101). In this case since the conversion rate does not change we did not use a variable, but assigned
it directly in the print statement.
class EuroConv {
Riccardo Flask 18 | P a g e
JAVA for Beginners
double eu;
System.out.println();
for (eu=1;eu<101;eu++)
class Example15 {
double x, y, z;
x = 3;
y = 4;
z = Math.sqrt(x*x + y*y);
Predicted Output:
Hypotenuse is 5.0
Please note that whenever a method is called, a particular nomenclature is used where we first
specify the class that the particular method belongs to, e.g. Math.round( ); where Math is the class
name and round is the method name. If a particular method accepts parameters, these are placed in
brackets, e.g. Math.max(2.8, 12.9) – in this case it would return 12.9 as being the larger number. A
Riccardo Flask 19 | P a g e
JAVA for Beginners
useful method is the Math.random( ) which would return a random number ranging between 0.0
and 1.0.
class Example16 {
x = 10;
x = y * 2;
Predicted Output:
x and y: 10 20
x is 40
If we had to remove the comment marks from the line, y = 100; we would get an error during
compilation as y is not known since it only exists within the block of code following the ‘if’
statement.
The next program shows that y is initialized each time the code belonging to the looping sequence is
executed; therefore y is reset to -1 each time and then set to 100. This operation is repeated for
three (3) times.
Riccardo Flask 20 | P a g e
JAVA for Beginners
class Example17 {
int x;
y = 100;
Predicted Output:
y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100
long L;
double D;
L = 100123285L;
D = L; // L = D is impossible
Riccardo Flask 21 | P a g e
JAVA for Beginners
Predicted Output:
The general formula used in casting is as follows: (target type) expression, where target type could
be int, float, or short, e.g. (int) (x/y)
double x, y;
byte b;
int i;
char ch;
x = 10.0;
y = 3.0;
i = 100;
b = (byte) i;
i = 257;
b = (byte) i;
ch = (char) b;
Riccardo Flask 22 | P a g e
JAVA for Beginners
Predicted Output:
Integer outcome of x / y: 3
Value of b: 100
Value of b: 1
ch: X
In the above program, x and y are doubles and so we have loss of precision when converting to
integer. We have no loss when converting the integer 100 to byte, but when trying to convert 257 to
byte we have loss of precision as 257 exceeds the size which can hold byte. Finally we have casting
from byte to char.
class Example20 {
byte b;
int i;
b = 10;
b = 10;
Predicted Output:
The above program illustrates the difference between automatic conversion and casting. When we
are assigning a byte to integer, i = b * b, the conversion is automatic. When performing an arithmetic
operation the byte type are promoted to integer automatically, but if we want the result as byte, we
have to cast it back to byte. This explains why there is the statement: b = (byte) (b * b). Casting has
to be applied also if adding variables of type char, as result would else be integer.
Riccardo Flask 23 | P a g e
JAVA for Beginners
Console Input
Most students at this point would be wondering how to enter data while a program is executing.
This would definitely make programs more interesting as it adds an element of interactivity at run-
time. This is not that straight forward in Java, since Java was not designed to handle console input.
The following are the three most commonly used methods to cater for input:
import java.io.*;
import java.util.*;
return errorCount;
}
errorCount = 0;
return printErrors;
Riccardo Flask 24 | P a g e