Variables in java Programs

Read tutorials from Variables in java


Program List:


Long Program Share

Program:

public class CopyArray {    
    public static void main(String[] args) {        
             //Initialize array     
        int [] arr1 = new int [] {1, 2, 3, 4, 5};     
         //Create another array arr2 with size of arr1    
        int arr2[] = new int[arr1.length];    
        //Copying all elements of one array into another    
        for (int i = 0; i < arr1.length; i++) {     
            arr2[i] = arr1[i];     
        }      
         //Displaying elements of array arr1     
        System.out.println("Elements of original array: ");    
        for (int i = 0; i < arr1.length; i++) {     
           System.out.print(arr1[i] + " ");    
        }     
            
        System.out.println();    
            
        //Displaying elements of array arr2     
        System.out.println("Elements of new array: ");    
        for (int i = 0; i < arr2.length; i++) {     
           System.out.print(arr2[i] + " ");    
        }     
    }    
}    

Output:


 Elements of original array
1 2 3 4 5
Elements of new array:
1 2 3 4 5


Long Program Share

Program:

public class ArrayExample {

   public static void main(String[] args) {
      double[] myList = {3.9, 5.9, 22.4, 31.5};

      // Print all the array elements
      for (int i = 0; i < myList.length; i++) {
         System.out.println(myList[i] + " ");
      }

   }
}

Output:


3.9
5.9
22.4
31.5
Press any key to continue . . .


Long Program Share

Program:

public class ArrayExample {

   public static void main(String args[]){

   int a[]=new int[5];//declaration and instantiation
   a[0]=10;//initialization index 0
   a[1]=20;//initialization index 1
   a[2]=30;//initialization index 2
   a[3]=40;//initialization index 3
   a[4]=50;//initialization index 4

   //printing array
   for(int i = 0; i < a.length; i++)//length is the property of array
   System.out.println(a[i]);

   }
}

Output:


10
20
30
40
50
Press any key to continue . . .