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
Inner class and Anonymous class
Inner class: classes defined in other classes, including those defined in methods are called inner classes. An inner class can have any accessibility including private.

Anonymous class: Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors.
Famous Quotations
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek

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.