Skip to main content

Array

Introduction:
                        An array is an index collection of a fixed number of homogeneous data elements.

        The main advantage of the array is, we can represent a huge number of values using a single variable. So, the readability of the code will be improved.
        But the main disadvantage of an array is, fixed in size. i.e. Once we create an array there is no chance of increasing or decreasing the size based on our requirement. Hence, to use the array concept compulsory we should know the size in advance, which may not possible always.

Array Declaration:
                            There are three ways to declare the one-dimensional array. They are as follows

                            int[] x;
                            int []x;
                            int x[];

All above are valid, but int[] x; is recommended, because variable name clearly separated from the type of array.
        
        At the time of declaration, we can't specify the size, otherwise, we will get the compile-time error.
            int[6] x;      Invalid
            int[] x;        Valid

We can declare a two-dimensional array in the following ways.

            int[][] x;
            int [][]x;
            int x[][];
            int[] []x;

We can declare more than one array of the same type in the same line.
        e.g.    int[] a, b;


Que: Which of the following are valid if valid then write the dimension of the arrays.


                        If we want to specify dimension before variable that facility is applicable only for 1st variable in a declaration.
                        If we are trying to apply for the next variable, we will get a compile-time error.
            e.g.
                    int[] []a, []b, []c;    


Comments

Popular posts from this blog

Identifiers

Identifiers:                                         A name in java program is called identifier, which can be used for identification purposes. It can be a method name, variable name, class name, or label name . Rules for defining java identifiers: The only allowed characters in java identifiers are a - z, A - Z, 0 - 9, $, and _. If we are using other characters, we will get a compile-time error. Identifiers can't start with digits. Java identifiers are case sensitive. Of course, java is treated as case sensitive programming language. There is no limit for java identifiers but it is not recommended to take too lengthy identifiers. We can't use reserved words as identifiers. All predefined java class name and interface name we can use as identifiers.                     e.g.                         int String = 888 ;                         int Runnable = 999 ;                         Even this is valid, but it is not good programming practice. Examples Que 1) How many identi

Literals

Literals:                 Any constant value which can be assigned to the variable is called literal.     1 . Integer Literals:                                        For integral data types ( byte, short, int, long ), we can specify a literal value in the following base...                         a) Decimal literal (base 10):                                                     Allowed digits are 0 to 9.                                                                e.g.  int x = 10;                         b) Octal literal (base 8):                                                 Allowed digits are 0 to 7.                                             Literal should be prefixed with '0' (Zero).                                                       e.g.   int x = 010;                         c) Hexa decimal literal (base 16):                                                   Allowed digits are 0 to 9, a to f. For extra digits (a to f), we can use both lowercase and uppercase c

Reserved words

Reserved Words:                                    In java, some words are reserved to represent some meaning or functionality such types of words are called reserved words. Data type related keywords:                     1. byte                         2. short                         3. int                         5. long                         6. float                         7. double                         8. char Flow control-related keywords:                     1. if                         2. else                         3. switch                         4. case                         5. default                         6. do                         7. while                         8. for                         9. continue                         10. return                         11. break Modifiers related keywords:                     1. public                         2. private                         3. protected                         4. static