Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to declare, create and access a one-dimensional Array in Java

Introduction :-
Today I will be explaining you some of the basic concepts related to an array. I will be teaching you how to declare an array ?. , how to create an array ?. and how to assign values to array elements?.Generally an array is group of variables holding values which have same data types. It means an array can store many values in them which are of same data types. Generally arrays are objects in Java, so they come under category of reference types. An array variable provides reference to an array object in a memory which can either hold primitives data types or reference data , based upon the declaration of an array. To retrieve elements of an array we use concepts of index.We provide the name of array variable and position number of array element which we want to retrieve in a square bracket. Let us go further and see how this whole concept works.


How to declare a One-Dimensional arrays ?.
A one-dimensional array is a linear list of elements of the same type. Let us see how to declare an One-Dimensional array:

Syntax : 
type array-name [ ]; 
Or 
type [ ] array-name;

Here, type declares the base type of the array and array-name is the name of the array.
Let us look into few of examples :

int [ ] intArray ; 
byte [ ] byteArray ;
Object [ ] objectArray;

In first example, we have declared an array which can hold int values. The array name is intArray. However, the array does not exist. The object intArray is set to null which represents an empty array.

How to create a One-Dimensional array ?.
In order to create a One-Dimensional array, we use new operator. The new operator creates an object of array in heap memory. Let us look into the syntax :

Syntax :
array-name = new type [ size ] ; 

Here array-name is the name of the array, type is the datatype of elements in the array and size is the number of elements.

Let us look into few of the examples :

intArray = new int [ 10 ] ;
byteArray = new byte [ 10 ];
objectArray = new Object [ 10 ] ;

In first example, we have created an integer array of 10 elements and assigns it to the variable intArray. After writing this syntax an array object is created in heap which can hold 10 integer values.


How to assign values to array through example ?.
Values can be assigned to an array by couple of ways. Let us look into syntax of some of those ways below :

Syntax :
array-name [ index ] = value ;

The value can be assign to each array element through their respective index number. As arrays are linear so they have sequential indexing. The indexing of an array starts with number 0.

Let us look into an example below :

for( int i = 0; i < 10 ; i++ )
intArray[ i ] = i + 1 ;

After for loop gets executed, values from 1 to 10 are assigned to array elements intArray[ 0 ] to intArray[ 9 ]. There is also an another way of assigning values to an array at the time of declaration. This assigning of value to an array is called as initialization. If we initialize an array, the size of an array need not be given. Let us look into syntax of initialization of an array.

syntax : 
type array-name [ ] = { value1, value2, value3 .... .... , valueN }
Let us look into the example:

int [ ] intArray = { 1,2,3,4,5,6,7,8,9,10} ;


Accessing Array Elements 

Generally, Arrays in Java are implemented as objects. Arrays in Java have a attribute called as length which stores the size of an array. Each and every array created has this attribute. It always store size of an array. This attribute or field can be accessed by using syntax :

array-name.length

Let us look into an example :

int [ ] intArray = { 4,5,6,7,8,9,1,2,3} ; 
for (int i = 0; i < intArray.length; i++ )
System.out.println( "Value of element" + i + " = " + intArray[ i ] );
Above code will print the values of an array element using length field. If we make an attempt to access array elements beyond the legal index, it throws an exception called ArrayIndexOutOfBoundsException.

Java Applets Life-Cycle Methods

Introduction 
This blog post will guide you about the basics of Java Applet. This tutorial will teach you how to create small applets. So, What is an Applet in Java ? An Applet is a small java program that runs in a Java enabled web browser. Java Applet is a small piece of java code that is embedded into HTML page, which gets executed when the HTML page loads into the browser. Applet runs on the Client machine, while Servlet runs on Server side. They are rarely used to make website dynamic. Its secure and platform independent. They require JVM and Java enabled web browser such as IE, Mozilla etc, to get execute on client machine. Applets are embedded into HTML documents and has various methods in java.applet.Applet, which governs its life-cycle. Let us look into few of those :

Applet Life-Cycle Methods

1. public void init()

It is the first method that is invoked or called for an applet. Any initialization needed for an applet is performed in this method. It is launched by the system automatically when Java executes an applet. It performs small tasks such as loading images and reading parameters etc . Applets can be initialized by the constructors but init method is preferred for doing all the applet initialization.


2. public void start()

After Java calls init method, this method is executed next. It gets automatically called after init method completes execution. It is special method, when a user goes to any other website and finally returns back to html page of applet, this method is called again. All the tasks that we need to perform after applet gets loaded and after user revisits applet is written here. Generally, animation performing code or new start to a different thread is written here.

3. public void paint( Graphics g )

This method is called usually after init and start method gets executed. This method is called whenever applet needs to be redrawn in the browser window. If user covers applet window by a different window and later comes back to same window by uncovering, the paint method is called. This method has one parameter of type Graphics which performs actions involved with drawing. It is also used for displaying text, graphics etc.

4. public void stop()

This method is called when the user leaves the page on which applet is running. This method performs tasks of stopping execution of animations and threads. This method is called just after the destroy method.

5. public void destroy()

This method is called when the browser completely ends the applet and it is being removed from memory. This method is called when browser shuts down normally. The destroy method has the code that perform tasks that are required to clean up resources such as graphics object, threads etc allocated to the applet.

Applet Advantages and Restrictions

What is an Applet in Java ?
An Applet is a small java program that runs in a Java enabled web browser. Java Applet is a small piece of java code that is embedded into HTML page, which gets executed when the HTML page loads into the browser.Applets provide powerful client-side functionality. As applets are loaded from remote machines and executed on client-side, there are various security restrictions on applets. Let us look into various Applets advantages and Applets Restrictions in brief :

Applet Advantages
Applets have many advantages over stand-alone application. Some of them are :

  1. As applet is a small java program, so it is platform independent code which is capable to run on any browser.
  2.  Applets can perform various small tasks on client-side machines. They can play sounds, show images, get user inputs, get mouse clicks and even get user keystrokes, etc ... 
  3.  Applets creates and edit graphics on client-side which are different and independent of client-side platform.
  4. As compared to stand-alone application applets are small in size, the advantage of transferring it over network makes it more usable.
  5. Applets run on client browser so they provide functionality to import resources such as images, audio clips based on Url's.
  6. Applets are quite secure because of their access to resources.
  7. Applets are secure and safe to use because they cannot perform any modifications over local system.
  8. Various small tasks such as performing login, inventory checking, task scheduling can be done by applets running over Intranets. 


Applets Restrictions
Applets have many restrictions over the areas of security because they are obtained from remote machines and can harm client-side machines. Some of them are as follows :

  1. If we are running an applet from a provider who is not trustworthy than security is important. 
  2. Applet itself cannot run or modify any application on the local system.
  3. Applets has no access to client-side resources such as files , OS etc. 
  4. Applets can have special privileges. They have to be tagged as trusted applets and they must be registered to APS (Applet Security Manager). 
  5. Applet has little restriction when it comes to communication. It can communicate only with the machine from which it was loaded. 
  6. Applet cannot work with native methods. 
  7. Applet can only extract information about client-machine is its name, java version, OS, version etc ... .
  8. Applets tend to be slow on execution because all the classes and resources which it needs have to be transported over the network. 
 
© 2021 Learn Java by Examples Template by Hubberspot