Java Arrays & Enum

An array is used in java to hold multiple items of a particular datatype. Each item in it is a variable of same datatype which holds value. There will be a place where we might be needing many variables to hold different values and it will become a tedious process to use everything in the program. For this, we go for array where we will be having a single variable to hold the array object with values in it.


How to declare and initialize an array:

Array is an object whether they are declared with primitives or reference objects. Multidimensional arrays are defined as array of arrays. Once the array is declared with a datatype, we can’t initialize it with some other datatype.

Sample Program:


public class ArraysChap {
	public static void main(String[] args) {
		
		// Declaration of an array
		int marks[];
		
		int []scores;
		
		// Initialization of an array
		marks = new int[3];
		marks[0] = 42;
		marks[1] = 43;
		marks[2] = 44;
		
		String names[] = {"John", "Mike"};	
		
		// Multidimensional arrays
		int xy[][] = new int[2][2];
		xy[0][0] = 1;
		xy[0][1] = 2;
		xy[1][0] = 3;
		xy[1][1] = 4;
	}		
}

Enum:

Enum is a custom type defined with a set of constants. Enum keyword is used in order to define an enum type. The constants given to the enum should be in upper case. After defining this type, it can be used similar to other datatypes in the program.


public enum Color {
	BLUE, ORANGE, YELLOW, GREEN		
}

 

Posted on July 1, 2014 in Java for Beginners

Share the Story

Response (1)

  1. tes .com bro
    October 23, 2016 at 5:09 pm · Reply

    I got this web site from my buddy who told me on the topic
    of this web page and at the moment this time
    I am browsing this website and reading very informative content at this time.

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top