I am playing around with Java and would like to know just how different the following are in terms of performance. I understand that premature optimization is a plight on programming but my curiosity is just for future reference.
public class Type1{
int[] data = new data[4];
public int getData( int index ){
return data[index];
}
}
public class Type2{
int data1;
int data2;
int data3;
int data4;
public int getData1(){
return data1;
}
public int getData2(){
return data2;
}
public int getData3(){
return data3;
}
public int getData4(){
return data4;
}
}
It is understandable that there are many factors that might make a difference but there must be some noticeable performance in some way. Obviously class Type1 is a much more attractive solution in terms of design but it seems as if it holds an extra step when retrieving data, namely going into an array to get an int as oppose to Type2 which just goes straight for the data. Perhaps the differences are more noticeable if they held Class object arrays since it would probably be guaranteed that Java uses references for the array members. Am I completely off the ball?