java notes unit 1
java notes unit 1
UNIT 1
ARRAYS
An array is a group of similar-typed variables that are referred to by a
common name. Arrays of any type can be created and may have one or
more dimensions. A specific element in an array is accessed by its index.
Arrays offer a convenient means of grouping related information.
One-Dimensional Arrays
A one-dimensional array is a list of like-typed variables. To create an
array, you first must create an array variable of the desired type. The
general form of a one -dimensional array declaration is
type var-name[ ];
Here, type declares the base type of the array. For example, the following
declares an array named month with the type ―array of int‖:
int month [];
Although this declaration establishes the fact that month is an array
variable, no array actually exists. In fact, the value of month is set to null,
which represents an array with no value. To link month with an actual,
physical array of integers, you must allocate one using new and assign it
to month. new is a special operator that allocates memory. The general
form of new as it applies to one-dimensional arrays appears as follows:
array-var = new type[size];
Here, type specifies the type of data being allocated, size specifies the
number of elements in the array, and array-var is the array variable that is
linked to the array. That is, to use new to allocate an array, you must
specify the type and number of elements to allocate. The elements in the
array allocated by new will automatically be initialized to zero.
This example allocates a 12-element array of integers and links them to
month
month = new int[12];
After this statement executes, month will refer to an array of 12 integers.
Further, all elements in the array will be initialized to zero. Another way to
declare an array in single step is
type arr-name=new type[size];
Arrays can be initialized when they are declared. The process is much the
same as that used to initialize the simple types. An array initializer is a list
of comma-separated expressions surrounded by curly braces. The
commas separate the values of the array elements. The array will
automatically be created large enough to hold the number of elements
you specify in the array initializer. There is no need to use new.
For example, to store the number of days in each month, we do as follow
// An improved version of the previous program.
class AutoArray
{
public static void main(String args[])
{
int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,30, 31 };
System.out.println("April has " + month[3] + " days.");
}
}
When you run this program, in the output it prints the number of days in
April. As mentioned, Java array indexes start with zero, so the number of
days in April is month[3] or 30.
Here is one more example that uses a one-dimensional array. It finds the
average of a set of numbers.
// Average an array of values.
class Average
{
public static void main(String args[])
{
double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
int i;
for(i=0; i<5; i++)
result = result + nums[i];
System.out.println("Average is " + result / 5);
}
}
Output: Average is:12.3
Multidimensional Arrays
In Java, multidimensional arrays are actually arrays of arrays. To declare a
multidimensional array variable, specify each additional index using
another set of square brackets. For example, the following declares a two-
dimensional array variable called twoD.
int twoD[][] = new int[4][5];
This allocates a 4 by 5 array and assigns it to twoD.
program :
// Demonstrate a two-dimensional array.
class TwoDArray
{
public static void main(String args[])
{
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++)
{
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++)
{
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Output:
01234
56789
10 11 12 13 14
15 16 17 18 19
When you allocate memory for a multidimensional array, you need only
specify the memory for the first (leftmost) dimension. You can allocate the
remaining dimensions separately. We can allocates the second dimension
manually.
int twoD[][] = new int[4][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
twoD[3] = new int[5];
we can creates a two dimensional array in which the sizes of the second
dimension are unequal.
// Manually allocate differing size second dimensions.
class TwoDAgain
{
public static void main(String args[])
{
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<i+1; j++)
{
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++)
{
for(j=0; j<i+1; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Output:
0
12
345
678
Operators:
Operators are special symbols that perform specific operations on one,
two, or three operands, and then return a result. Operator in java is a
symbol that is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in java which are given below:
o Unary Operator,
o Arithmetic Operator,
o shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator