0% found this document useful (0 votes)
104 views

Anonymous Array

Anonymous arrays allow the creation and initialization of arrays without an explicit name in one line of code. They can be passed as arguments to methods when a local variable is not needed to store the array. Anonymous arrays are created using the syntax new type[] {values} and can only be used once since they do not have a name.

Uploaded by

sathiyavijayan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Anonymous Array

Anonymous arrays allow the creation and initialization of arrays without an explicit name in one line of code. They can be passed as arguments to methods when a local variable is not needed to store the array. Anonymous arrays are created using the syntax new type[] {values} and can only be used once since they do not have a name.

Uploaded by

sathiyavijayan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Anonymous Array

An array without name is known as anonymous array in java.


As the array do not have any name so it can be used only once.
Anonymous array is passed as an argument of method.
Anonymous array is created and initialized in the same line.

In Java 1.1 the concept of anonymous array was introduced. While it is easy to initialize
an array when it is declared , you couldnt reinitialize the array later with a comma-
delimited list unless you declared another variable to store the new array in .
In this place anonymous array is used
Can reinitialize an array to a new set of values or pass unnamed arrays into methods when
you dont want to define a local variable to store said array.
Syntax
new type[] { comma-delimited-list};

int[] a= {1,2,3,4};
a=new int[]{5,6,7,8};
or
int[] a2={5,6,7,8};
a1=a2;

eg.,

int[] a1={5,6,7,8};
for(int e:a1)
System.out.println(e);

int[] a2={1,2,3,4,5};
a1=a2;

// or a1=new int[]{1,2,3,4};
for(int e:a1)
System.out.println(e);

class AnonymousArray
{
static void print(int a[])

{
for(int i=0;i<a.length;++i)

System.out.print(a[i]+" ");

static void print(int a[][])

{
for(int i=0;i<a.length;++i)

for(int j=0;j<a[i].length;++j)

System.out.print(a[i][j]+" ");

System.out.println("");

public static void main(String...s)

//1d anonymous array

print(new int[]{10,20,30,40});

System.out.println("\n");

//2d anonymous array


print(new int[][]{{10,20},{30,40},{50,60}});

}
}

Ragged array

class raggedarray
{
public static void main(String ...s)
{
/* int[][] p=new int[5][];

for(int i=1;i<=5;i++)
p[i-1]=new int[i];

for(int j=0;j<p.length;j++)
System.out.println(p[j].length);
int number = 0;
int[][] pyramid = new int[4][];
for (int i = 0; i < pyramid.length; i++)
{
pyramid[i] = new int[i+1];
for (int j = 0; j < pyramid[i].length; j++)
pyramid[i][j] = number++;
}

/*for (int i = 0; i < pyramid.length; i++) {


for (int j = 0; j < pyramid[i].length; j++) {
System.out.print(pyramid[i][j] + " ");
}
System.out.print("\n");
}*/

for (int[] row : pyramid) {


for (int col : row) {
System.out.print(col + " ");
}
System.out.print("\n");
}

/*
output
0
12
345
6 7 8 9*/
}
}

CLASS
A Class is a template for an object.
It helps to encapsulate entire set of data and code that operate on data into a single user
defined data type
When a class is created, we create a new data type.
General form of a class is
class classname {
// instance field declarationa
type instance-variable-1;
type instance-variable-2;

type instance-variable-N;
// method definition
type methodname-1(parameter-list) { //body of method }
type methodname-2(parameter-list) { //body of method }

type methodname-N(parameter-list) { //body of method }
//constructor definition
classname(parameter-list) { //body of constructor }
.
}
The class body (the area between the braces) contains following item :
- constructors for initializing new objects
- field declarations
that provide the state of the class and its objects
called as instance field
Syntax
datatype variablename[=value];

- methods
the procedure /code that operates on the data .
Syntax
accessspecifier returntype methodname(parameter list){}.
Example
class Box {
// instance field
double width, height, depth;
// constructor of 3 parameters
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// method to compute and return volume
double volume() {
return width * height * depth;
}
}

OBJECTS

Defn:
Called as instance of a class
or
An object in Java is a block of memory that contains space to store all the instance
variables.

Declaring objects or creating objects


Creating an object is referred as instantiating an object.
Once class is defined, you can create new data-type of that class.
Obtaining object of the class is a two-step process:
1. Declare reference variable of class type.(i.e., the data type of a variable is a class
name).
Syntax
classname reference-variable; // only declaration
Note: This declaration does not define object, instead it is indicating that b is reference
variable of object Box, presently it is point to null object
2. Acquire an actual, physical copy of the object and assign its address to the reference
variable This process is done by new operator
new operator dymamically allocate memory for an object and return reference(i.e.,
address in memory allocate to it) to it(i.e. to the object)
This could be done using following syntax:
reference-variable = new classname([argument-list]);
Syntax for creating object
classname objectname; // declare
objectname = new classname(); // memory allocated to the object
or
classname objectname = new classname(); // two process in single step

e.g., for the above class Box , the object is created as


Box b; //declare
b =new Box ();//memory allocated to the object
or
Box b = new Box (); // two process in single step
attempt to access the variable b after declaration statement , gets compile-time
error.
Pictorial representation for the above object creation(the two-step process)
Action Statement Effect or result

Declare Box b; b null (address is not


allocated for object b

Object b=new Box(); baddress is allocated


reference
b
width=10,height=12.5,depth=9.0
Box(float,float,float){}
double volume(){}

METHODS

Procedure or code that operates on instance fields


Methods of the class has all rights to access, set, modify instance field of class
Methods could have any access specifier such as public, private, protected, and package-
protected
It may have return type, and can have zero on more arguments. It general signature is
return-type methodname-1(parameter-list)
{ //body of method }
return-type and parameter-list can be of any valid java primitive data-type or type of
user-defined class. If the method does not return a value, its return-type must be void
Only non-void methods require to have return-statement within its body.
Syntax of return statement
return(value);
Different types of methods are
1. Mutator Method : Methods that change the instance fields
2. Accessor Method: Methods that only access instance fields without modifying it

E.g., for mutator and accessor method


class student
{
String name;
void setname(String s) // Mutator method
{ name=s; }
String getname() // accessor method
{ return name; }
public static void main(String args[])
{
student stud=new student();
stud.setname(Sarveswaran);
System.out.println(Name of the Student is + stud.getname());
}
}

You might also like