Last active
December 20, 2016 06:52
-
-
Save dnahid/6cbc9c89a0cd8415d43d3077a2821872 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package co.miaki.nahid; | |
import java.util.ArrayList; | |
public class Test { | |
public static void main(String[] args){ | |
float[] studentCGPAFromArray = new float[1000]; | |
float[] studentCGPAFromArrayList = new float[1000]; | |
int i = 0; | |
for(float studentCGPA : getStudentCGPAAsArray()){ | |
// Type Casting is not required since Array is type safe. | |
studentCGPAFromArray[i++] = studentCGPA; | |
} | |
i = 0; | |
for(Object studentCGPAObject : getStudentCGPAAsArrayList()){ | |
// Type Casting is required since ArrayList will return Object type. | |
// No error at compile time. | |
// Error at runtime since it will get objects other than Float | |
studentCGPAFromArrayList[i++] = (float)studentCGPAObject; | |
} | |
} | |
// Return student CGPA as ArrayList | |
public static ArrayList getStudentCGPAAsArrayList(){ | |
ArrayList studentCGPA = new ArrayList(); | |
studentCGPA.add(2.4f); | |
// No Compile time error since, we can add any type of variable rin ArrayList | |
studentCGPA.add("2.3"); | |
studentCGPA.add("anything"); | |
return studentCGPA; | |
} | |
// Return student CGPA as Array | |
public static float[] getStudentCGPAAsArray(){ | |
float[] studentCGPA = new float[1000]; | |
studentCGPA[0] = 2.3f; | |
// Compile time error, since we are trying to add string in float type | |
studentCGPA[1] = "2.3"; | |
return studentCGPA; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment