JAVA For Beginners: Main Program
JAVA For Beginners: Main Program
*/
void setAlpha(int a) {
alpha = a;
int getAlpha() {
return alpha;
Main Program:
class AccessDemo {
ob.setAlpha(-99);
ob.beta = 88;
ob.gamma = 99;
Riccardo Flask 97 | P a g e
JAVA for Beginners
class FailSoftArray {
a = new int[size];
errval = errv;
length = size;
return errval;
if(ok(index)) {
a[index] = val;
return true;
return false;
Riccardo Flask 98 | P a g e
JAVA for Beginners
return false;
Main Program:
class FSDemo {
int x;
System.out.println("Fail quietly.");
fs.put(i, i*10);
x = fs.get(i);
System.out.println("");
if(!fs.put(i, i*10))
x = fs.get(i);
else
Riccardo Flask 99 | P a g e
JAVA for Beginners
Predicted Output:
Index 5 out-of-bounds
Index 6 out-of-bounds
Index 7 out-of-bounds
Index 8 out-of-bounds
Index 9 out-of-bounds
0 10 20 30 40 Index 5 out-of-bounds
Index 6 out-of-bounds
Index 7 out-of-bounds
Index 8 out-of-bounds
Index 9 out-of-bounds
Arrays
An array can be defined as a collection of variables of the same type defined by a common name,
e.g. an array called Names which stores the names of your class mates:
Arrays in Java are different from arrays in other programming languages because they are
implemented as objects.
One-dimensional Arrays
The following code creates an array of ten integers, fills it up with numbers using a loop and then
prints the content of each location (index) of the array:
class ArrayDemo {
int i;
sample[i] = i;
sample[i]);
Predicted Output:
This is sample[0]: 0
This is sample[1]: 1
This is sample[2]: 2
This is sample[3]: 3
This is sample[4]: 4
This is sample[5]: 5
This is sample[6]: 6
This is sample[7]: 7
This is sample[8]: 8
This is sample[9]: 9
The following program contains two loops used to identify the smallest and the largest value stored
in the array:
class MinMax {
nums[0] = 99;
nums[1] = -10;
nums[2] = 100123;
nums[3] = 18;
nums[4] = -978;
nums[5] = 5623;
nums[6] = 463;
nums[7] = -9;
nums[8] = 287;
nums[9] = 49;
Predicted Output:
class Bubble {
int a, b, t;
int size;
System.out.println();
for(b=size-1; b >= a; b -) {
// exchange elements
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;
System.out.println();
Predicted Output:
Two-Dimensional Arrays:
This would create a table made up of 10 rows (index: 0 to 9) and 20 columns (index: 0 to 19). The
following code creates a table, 3 rows by 4 columns, and fills it up woth numbers from 1 to 12. Note
that at index [0][0] = 1, [0][1] = 2, [0][2] = 3, [0][3] = 4, [1][0] = 5, etc.
class TwoD {
int t, i;
table[t][i] = (t*4)+i+1;
System.out.println();
The square brackets follow the type specifier, not the name of the array variable. For example, the
following two declarations are equivalent:
This alternative declaration form offers convenience when declaring several arrays at the same time.
This creates three array variables of type int. It is the same as writing:
The alternative declaration form is also useful when specifying an array as a return type for a
method:
Array References:
Consider a particular array, nums1, and another array, nums2 and at one point in the code we assign
one array reference to the other, i.e. nums2 = nums1. Then every action on nums2 will be as if it
were on nums1 (nums2 reference is lost).
class AssignARef {
int i;
nums1[i] = i;
nums2[i] = -i;
System.out.println();
System.out.println();
System.out.println();
Riccardo Flask 106 |
Page
JAVA for Beginners
nums2[3] = 99;
System.out.println();
Predicted Output:
Here is nums1: 0 1 2 3 4 5 6 7 8 9
Here is nums2: 0 -1 -2 -3 -4 -5 -6 -7 -8 -9
This variable automatically keeps track of the number of items an array can hold and NOT the actual
items. When an array is declared to have ten items, even if one does not put actual data, the length
of the array is 10. Something to keep in mind is also when dealing with two dimensional arrays. As
already explained in the theory lessons, two dimensional arrays are considered to be an array of
arrays. Hence calling up the length of such array would return the number of sub-arrays. To access
the length of the particular sub-array, one has to write the ‘row’ index, e.g. arrayname [0].length
class LengthDemo {
int nums[] = { 1, 2, 3 };
Riccardo Flask 107 |
Page
JAVA for Beginners
};
System.out.println();
list[i] = i * i;
System.out.println();
Predicted Output: