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
What is fail-fast in java?
A fail-fast system is nothing but immediately report any failure that is likely to lead to failure. When a problem occurs, a fail-fast system fails immediately. In Java, we can find this behavior with iterators. Incase, you have called iterator on a collection object, and another thread tries to modify the collection object, then concurrent modification exception will be thrown. This is called fail-fast.
Famous Quotations
Before you go and criticize the younger generation, just remember who raised them.
-- Unknown Author

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

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