So,
I've got a class called IntRectangle that holds int x1, int x2, int y1, int y2.
This is my assumption:
If I make the attributes above public I will get a performance boost, since I don't have to push and pop things when accessing them. I'll give it a shot.
public class CrapMain {
public void start () {
IntRectangle rec = new IntRectangle (5, 10, 6, 12);
int value = 0;
double time = System.nanoTime();
for (int i = 0; i < 1000000000; i++){
value = rec.X1;
}
time = System.nanoTime() - time;
System.out.println(time);
}
public static void main(String[] args) {
new CrapMain().start();
}
}
It prints 2559391.0
If I make the attributes private, create getters for them and change "value = rec.X1;" to "value = rec.getX1();" it prints 3551075.0
So there's obviously a performance boost given.
However, It makes the code venerable, since it's easy to change the attributes by mistake.
My question is, is it worth it? And is it common practice among programmers writing high-performance code?