20 October 2010

0 Multidimensional Arrays in Java

Multidimensional Arrays in Java


  • Multidimensional arrays are arrays of arrays.
  • To declare a multidimensional array variable, specify each additional index using another set of square brackets.

int [][] x;
//x is a reference to an array of int arrays
x = new int[3][4];
/*Create 3 new int arrays, each having 4 elements
x[0] refers to the first int array, x[1] to the second etc
x[0][0] is the first element of the first array
x.length will be 3
x[0].length, x[1].length and x[2].length will be 4 */
Read More...

0 What is Reference variables in Java?

Reference variables in Java


Reference variables are used in Java to store the references of the objects created by the operator new
Any one of the following syntax can be used to create a reference to an int array.
int x[];
int [] x;

The reference x can be used for referring to any int array.
//Declare a reference to an int array
int [] x;
//Create a new int array and make x refer to it
x = new int[5];

The following statement also creates a new int array and assigns its reference to x
int [] x = new int[5];
 In simple terms, reference can be seen as the name of the array. Arrays can be created only using dynamic memory allocation in Java. The memory is allocated dynamically using the keyword new.

A reference type can be assigned ‘null’ to show that it is not referring to any object. ‘null’ is a keyword in Java
int [] x = null;
Read More...

19 October 2010

0 Arrays in Java

array in Java


In Java, all arrays are created dynamically. The operator "new" is used for dynamic memory allocation.
The following statement creates an array of 5 integers.

new int[5]

The above statement returns a reference to the newly created array. References in Java are very similar to pointers in C

Initializing an array in Java
An array can be initialized while it is created as follows

int [] x = {1, 2, 3, 4};
char [] c = {‘a’, ‘b’, ‘c’};

The length of an array

Unlike C, Java checks the boundary of an array while accessing an element in it
Java will not allow the programmer to exceed its boundary
If x is a reference to an array, x.length will give you the length of the array
The for loops can be set up as follows

for(int i = 0; i < x.length; ++i){ x[i] = 5; }
Read More...

0 What is Methods in Java?

Methods in Java


Methods in Java is nothing but functions (As in C) the syntax of writing methods in Java is similar to that of functions in C.
Unlike C, all methods in Java should be written inside a class.
Unlike C, there is no default return type for a Java method.
In C language, a function whose return type is not specified is assumed to return an int. In Java there is no default return type for a method. Specifying the return type of a method is mandatory in Java.
Read More...

0 What are the Operators in Java?

Operators in Java


Operators in Java are very similar to operators in C or other such languages
  • Assignment Operators
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
Read More...