A rey! :P

01) class Test {
   public static void main(String args[]) {
     int arr[2]; //Line1
     System.out.println(arr[0]);
     System.out.println(arr[1]);
   }
}   
Error at line 1
Explanation:
In Java, it is not allowed to put the size of the array in the declaration because an array declaration specifies only the element type and the variable name. The size is specified when you allocate space for the array. Even the following simple program won't compile.
class Test {
   public static void main(String args[]) {
     int arr[5];   //Error
   }
}


02) class Test {
   public static void main(String args[]) {
     int arr[] = new int[2];
     System.out.println(arr[0]);
     System.out.println(arr[1]);
   }
}
    Output: 0 0
Explanation:
Java arrays are first class objects and all members of objects are initialized with default values like o, null.



03)class Test
{
    public static void main (String[] args)
    {
        int arr1[] = {1, 2, 3};
        int arr2[] = {1, 2, 3};
        if (arr1 == arr2)
            System.out.println("Same");
        else
            System.out.println("Not same");
    }
}
    Output:NotSame


04)import java.util.Arrays;
class Test
{
    public static void main (String[] args)
    {
        int arr1[] = {1, 2, 3};
        int arr2[] = {1, 2, 3};
        if (Arrays.equals(arr1, arr2))
            System.out.println("Same");
        else
            System.out.println("Not same");
    }
}
    Output: Same

05)class Test
{
    public static void main (String[] args)
    {
        int arr1[] = {1, 2, 3};
        int arr2[] = {1, 2, 3};
        if (arr1.equals(arr2))
            System.out.println("Same");
        else
            System.out.println("Not same");
    }
}
    Output: Not same
arr1.equals(arr2) is same as (arr1 == arr2) 





SHARE

About df

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment