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 OOPs?
Object oriented programming organizes a program around its data, i.e. , objects and a set of well defined interfaces to that data. An object-oriented program can be characterized as data controlling access to code.
Famous Quotations
I don’t know the key to success, but the key to failure is trying to please everybody.
-- Bill Cosby

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.