JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Java One Dimensional Arrays

One dimensional array is a list of same typed variables. To create an array, first you must declare an array variable of required type, the syntax is given below:

type variable-name[];

Example:

int account_numbers[];

To allocate memory or create array object the syntax is given below:

variable-name = new type[size of the array];

Example:

account_numbers = new int[10];

You must specify the size of the array during creating object.

After this statement, account_numbers will refer to 10 integers, and assigned to 0. You can access any element by index, array index starts with 0 and ends with (array length-1). We can assign array values as given below:

account_numbers[0] = 1016;
account_numbers[1] = 449;
account_numbers[2] = 1015;

The below line displays the value stored at index 1.

System.out.println(account_numbers[1]);

We can combine declaration of an array with allocation of array as given below:

int account_numbers[] = new int[10];

Arrays can be initialized when they are declaring as given below:

int account_numbers[] = {12,34,56,12,78,98,34};

Other Java Array Examples

Knowledge Centre
Class, Constructor and Primitive data types
Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object according to the data the object can hold and the operations the object can perform. Constructor is a special kind of method that determines how an object is initialized when created. Primitive data types are 8 types and they are: byte, short, int, long, float, double, boolean, char.

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 6 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2014 by Nataraja Gootooru. All Rights Reserved.