what is faster?

I have a class that I use to do certain calculations.
it has some methods thatare called by other classes to do stuff like pick a random letter.
now my question is how can I make this as fast as posibble.

lets call this object “calc”

should I create a local instance of this “calc” object every time I want to use one of its methods?

or should I create this “calc” once in every object that uses calc and then use that link to call a method?

No, I wouldn’t. Otherwise you’re going to end up with lots of ‘calc’ objects being created, abandoned and GC’d. It’s generally a good idea to avoid creating new objects where possible; particularly if you are doing it in frequently called methods or loops.

Possibly. But assuming ‘calc’ makes calculations, you could just make them static methods. Then you can call Calc.whatever() without instantiating a Calc object.

Well what seems more logical?
The answer is kind of obvious i think.

*For this problem static is very much suited.

at the moment I have every object that uses “calc” create one instance of it and use that object.
is it worth it to change this to the static method?

here is the code of the class btw:


public class Physics extends ROGameObject
{
    public Random r = new Random();  
    
    
    @Override
    public void update()
    {
        
    }

    //check if two objcts collide
    public boolean checkCollisions(ROGameObject go1, ROGameObject go2)
    {
        Rectangle r1 = new Rectangle((int)go1.getx(),(int)go1.gety(),(int)go1.getxsize(),(int)go1.getysize());
        Rectangle r2 = new Rectangle((int)go2.getx(),(int)go2.gety(),(int)go2.getxsize(),(int)go2.getysize());
        
        return r1.intersects(r2);
    }  
    
    public int removenegative(int value)
    {
        if (value < 0)
        {
            return (value*-1);
        }
        return value;
             
    }
    
    
    
    //function checks if a value is between minimum and maximum value
    public float floatbetweenfunction(float minvalue, float midvalue, float maxvalue)
    {       
        if (minvalue < midvalue && midvalue < maxvalue)
        {
            return (midvalue);
        }
        if (midvalue <= minvalue )           
        {    
            return (minvalue);
        }
        if (midvalue >= maxvalue )           
        {    
            return (maxvalue);
        }
        
        return (midvalue);
    }
    
    //function checks if a value is between minimum and maximum value
    public int intbetweenfunction(int minvalue, int midvalue, int maxvalue)
    {       
        if (minvalue < midvalue && midvalue < maxvalue)
        {
            return (midvalue);
        } 
        if (midvalue <= minvalue )           
        {    
            return (minvalue);
        }
        if (midvalue >= maxvalue )           
        {    
            return (maxvalue);
        }
        
        return (midvalue);
    }    
    
    //returns a random number between minvalue and maxvalue
    public int RandomInt(int minvalue, int maxvalue)
    {          
        return (minvalue + (int)(Math.random() * ((maxvalue - minvalue) + 1)));   
    }
    
    public String Randombit()
    {
        float rand = r.nextFloat();
        if (rand < 0.5)
        {
            return("0");
        }
        else
        {
            return("1");
        }
    }
    public String Randomswitch()
    {
        float rand = r.nextFloat();
        if (rand < 0.5)
        {
            return("!");
        }
        else
        {
            return("?");
        }
    }    
    
    
    public char flipbit(char bit)
    {

        if (bit == '1')
        {
            return('0');
        }
        else if (bit == '0')
        {
            return('1');
        }
        return (bit);
    }    
    public String flipbitstring(String newstring)
    {
     
        String string1 = "";
        String string2 = "";
        String string3 = "";
        
        int newrand = RandomInt(0,newstring.length()-1);
        for (int i = 0; i < newstring.length(); i++)
        {
            if (i < newrand)
            {
                string1+= newstring.charAt(i);
            }
            else if (i == newrand)
            {
                string2 += flipbit(newstring.charAt(i));
            }
            else if (i > newrand)
            {
                string3+= newstring.charAt(i);
            }
        }
        return(string1+string2+string3);
    }
    
    //returns the biggest value of two values
    public int MaxInt(int value1, int value2)
    {     
        if (value1 > value2)  
        {
            return value1;
        }
        else
            return value2;
    }    
    
    public static double PytagorasInt(int value1, int value2)
    {     
        double nvalue1 = (double)value1;
        double nvalue2 = (double)value2;        
        double int1 = (nvalue1*nvalue1) + (nvalue2*nvalue2);
        
        return Math.sqrt(int1);
   
    }   
    
    public int returnarray(int currentarray,int newarray)
    {
        return (intbetweenfunction(0,currentarray+newarray,game.amountofgrids-1));           
    }
    
    public int inversedaxis(int position)
    {
        return (game.subgridsize - position - 1);
    }    
    
    
    public int plusormin()
    {
        float newchance = r.nextFloat();
        if (newchance <= 0.5)
        {
            return (-1);
        }
        return(1);     
    }
    

    public boolean Randombool()
    {  
        return Math.random() < 0.5; 
    }     
  
    public char inttoletter(int newint)
    {    
        if (newint == 0)    {   return('A'); }
        else if (newint == 1)    {   return('B'); }
        else if (newint == 2)    {   return('C'); }        
        else if (newint == 3)    {   return('D'); }    
        else if (newint == 4)    {   return('E'); }        
        else if (newint == 5)    {   return('F'); }             
        else if (newint == 6)    {   return('G'); }        
        else if (newint == 7)    {   return('H'); }    
        else if (newint == 8)    {   return('I'); }        
        else if (newint == 9)    {   return('J'); }          
        else if (newint == 10)    {   return('K'); }        
        else if (newint == 11)    {   return('L'); }    
        else if (newint == 12)    {   return('M'); }        
        else if (newint == 13)    {   return('N'); }             
        else if (newint == 14)    {   return('O'); }        
        else if (newint == 15)    {   return('P'); }    
        else if (newint == 16)    {   return('Q'); }        
        else if (newint == 17)    {   return('R'); }           
        else if (newint == 18)    {   return('S'); }        
        else if (newint == 19)    {   return('T'); }    
        else if (newint == 20)    {   return('U'); }        
        else if (newint == 21)    {   return('V'); }             
        else if (newint == 22)    {   return('W'); }        
        else if (newint == 23)    {   return('X'); }    
        else if (newint == 24)    {   return('Y'); }        
        else if (newint == 25)    {   return('Z'); } 
        
        else if (newint == 26)    {   return('a'); }     
        else if (newint == 27)    {   return('b'); }      
        else if (newint == 28)    {   return('c'); }            
        else if (newint == 29)    {   return('d'); }            
        else if (newint == 30)    {   return('e'); }            
        else if (newint == 31)    {   return('f'); }            
        else if (newint == 32)    {   return('g'); }   
        else if (newint == 33)    {   return('h'); }   
        
        return('A');
        
    }
    public float lettertofloat(char letter)
    {
        if (letter=='A')    {   return(0f/33f);  }
        else if (letter=='B')    {   return(1f/33f);  }     
        else if (letter=='C')    {   return(2f/33f);  }  
        else if (letter=='D')    {   return(3f/33f);  } 
        else if (letter=='E')    {   return(4f/33f);  } 
        else if (letter=='F')    {   return(5f/33f);  } 
        else if (letter=='G')    {   return(6f/33f);  }    
        else if (letter=='H')    {   return(7f/33f);  }     
        else if (letter=='I')    {   return(8f/33f);  }  
        else if (letter=='J')    {   return(9f/33f);  } 
        else if (letter=='K')    {   return(10f/33f);  } 
        else if (letter=='L')    {   return(11f/33f);  } 
        else if (letter=='M')    {   return(12f/33f);  }       
        else if (letter=='N')    {   return(13f/33f);  } 
        else if (letter=='O')    {   return(14f/33f);  } 
        else if (letter=='P')    {   return(15f/33f);  }         
        else if (letter=='Q')    {   return(16f/33f);  } 
        else if (letter=='R')    {   return(17f/33f);  } 
        else if (letter=='S')    {   return(18f/33f);  }       
        else if (letter=='T')    {   return(19f/33f);  } 
        else if (letter=='U')    {   return(20f/33f);  } 
        else if (letter=='V')    {   return(21f/33f);  }              
        else if (letter=='W')    {   return(22f/33f);  }         
        else if (letter=='X')    {   return(23f/33f);  } 
        else if (letter=='Y')    {   return(24f/33f);  } 
        else if (letter=='Z')    {   return(25f/33f);  }  
        
        else if (letter=='a')    {   return(26f/33f);  }    
        else if (letter=='b')    {   return(27f/33f);  }            
        else if (letter=='c')    {   return(28f/33f);  }            
        else if (letter=='d')    {   return(29f/33f);  }            
        else if (letter=='e')    {   return(30f/33f);  }   
        else if (letter=='f')    {   return(31f/33f);  }            
        else if (letter=='g')    {   return(32f/33f);  }       
        else if (letter=='h')    {   return(33f/33f);  }    
        
        return(0);                
    }    
    
    //number between -1 and 1
    public float lettertofloat2(char letter,float tresholdfactor)
    {
        if (letter=='A')    {   return(tresholdfactor*(-1f+(0f/33f)));  }
        else if (letter=='B')    {   return(tresholdfactor*(-1f+(2f/33f)));  }     
        else if (letter=='C')    {   return(tresholdfactor*(-1f+(4f/33f)));  }  
        else if (letter=='D')    {   return(tresholdfactor*(-1f+(6f/33f)));  } 
        else if (letter=='E')    {   return(tresholdfactor*(-1f+(8f/33f)));  } 
        else if (letter=='F')    {   return(tresholdfactor*(-1f+(10f/33f)));  } 
        else if (letter=='G')    {   return(tresholdfactor*(-1f+(12f/33f)));  }    
        else if (letter=='H')    {   return(tresholdfactor*(-1f+(14f/33f)));  }     
        else if (letter=='I')    {   return(tresholdfactor*(-1f+(16f/33f)));  }  
        else if (letter=='J')    {   return(tresholdfactor*(-1f+(18f/33f)));  } 
        else if (letter=='K')    {   return(tresholdfactor*(-1f+(20f/33f)));  } 
        else if (letter=='L')    {   return(tresholdfactor*(-1f+(22f/33f)));  } 
        else if (letter=='M')    {   return(tresholdfactor*(-1f+(24f/33f)));  }       
        else if (letter=='N')    {   return(tresholdfactor*(-1f+(26f/33f)));  } 
        else if (letter=='O')    {   return(tresholdfactor*(-1f+(28f/33f)));  } 
        else if (letter=='P')    {   return(tresholdfactor*(-1f+(30f/33f)));  }         
        else if (letter=='Q')    {   return(tresholdfactor*(-1f+(32f/33f)));  } 
        else if (letter=='R')    {   return(tresholdfactor*(-1f+(34f/33f)));  } 
        else if (letter=='S')    {   return(tresholdfactor*(-1f+(36f/33f)));  }       
        else if (letter=='T')    {   return(tresholdfactor*(-1f+(38f/33f)));  } 
        else if (letter=='U')    {   return(tresholdfactor*(-1f+(40f/33f)));  } 
        else if (letter=='V')    {   return(tresholdfactor*(-1f+(42f/33f)));  }              
        else if (letter=='W')    {   return(tresholdfactor*(-1f+(44f/33f)));  }         
        else if (letter=='X')    {   return(tresholdfactor*(-1f+(46f/33f)));  } 
        else if (letter=='Y')    {   return(tresholdfactor*(-1f+(48f/33f)));  } 
        else if (letter=='Z')    {   return(tresholdfactor*(-1f+(50f/33f)));  }    
        
        else if (letter=='a')    {   return(tresholdfactor*(-1f+(52f/33f)));  }            
        else if (letter=='b')    {   return(tresholdfactor*(-1f+(54f/33f)));  }   
        else if (letter=='c')    {   return(tresholdfactor*(-1f+(56f/33f)));  }  
        else if (letter=='d')    {   return(tresholdfactor*(-1f+(58f/33f)));  }  
        else if (letter=='e')    {   return(tresholdfactor*(-1f+(60f/33f)));  } 
        else if (letter=='f')    {   return(tresholdfactor*(-1f+(62f/33f)));  }
        else if (letter=='g')    {   return(tresholdfactor*(-1f+(64f/33f)));  }          
        else if (letter=='h')    {   return(tresholdfactor*(-1f+(66f/33f)));  }     
        
        return(0);                
    }        
    
    public int lettertoint(char letter)
    {
        if (letter=='A')    {   return(0);  }
        else if (letter=='B')    {   return(1);  }     
        else if (letter=='C')    {   return(2);  }  
        else if (letter=='D')    {   return(3);  } 
        else if (letter=='E')    {   return(4);  } 
        else if (letter=='F')    {   return(5);  } 
        else if (letter=='G')    {   return(6);  }    
        else if (letter=='H')    {   return(7);  }     
        else if (letter=='I')    {   return(8);  }  
        else if (letter=='J')    {   return(9);  } 
        else if (letter=='K')    {   return(10);  } 
        else if (letter=='L')    {   return(11);  } 
        else if (letter=='M')    {   return(12);  }       
        else if (letter=='N')    {   return(13);  } 
        else if (letter=='O')    {   return(14);  } 
        else if (letter=='P')    {   return(15);  }         
        else if (letter=='Q')    {   return(16);  } 
        else if (letter=='R')    {   return(17);  } 
        else if (letter=='S')    {   return(18);  }       
        else if (letter=='T')    {   return(19);  } 
        else if (letter=='U')    {   return(20);  } 
        else if (letter=='V')    {   return(21);  }              
        else if (letter=='W')    {   return(22);  }         
        else if (letter=='X')    {   return(23);  } 
        else if (letter=='Y')    {   return(24);  } 
        else if (letter=='Z')    {   return(25);  }  
        
        else if (letter=='a')    {   return(26);  }          
        else if (letter=='b')    {   return(27);  }    
        else if (letter=='c')    {   return(28);  }          
        else if (letter=='d')    {   return(29);  }     
        else if (letter=='e')    {   return(30);  }          
        else if (letter=='f')    {   return(31);  }            
        else if (letter=='g')    {   return(32);  }    
        else if (letter=='h')    {   return(33);  }    
        
        return(0);                
    }    
    
    
    public char letterup(char letter)
    {
        if (letter=='A')    {   return('B');  }
        else if (letter=='B')    {   return('C');  }     
        else if (letter=='C')    {   return('D');  }    
        else if (letter=='D')    {   return('E');  }   
        else if (letter=='E')    {   return('F');  }   
        else if (letter=='F')    {   return('G');  }   
        else if (letter=='G')    {   return('H');  }    
        else if (letter=='H')    {   return('I');  }   
        else if (letter=='I')    {   return('J');  }   
        else if (letter=='J')    {   return('K');  }   
        else if (letter=='K')    {   return('L');  }    
        else if (letter=='L')    {   return('M');  }   
        else if (letter=='M')    {   return('N');  }          
        else if (letter=='N')    {   return('O');  }   
        else if (letter=='O')    {   return('P');  }   
        else if (letter=='P')    {   return('Q');  }       
        else if (letter=='Q')    {   return('R');  }   
        else if (letter=='R')    {   return('S');  }    
        else if (letter=='S')    {   return('T');  }          
        else if (letter=='T')    {   return('U');  }   
        else if (letter=='U')    {   return('V');  }    
        else if (letter=='V')    {   return('W');  }                
        else if (letter=='W')    {   return('X');  }            
        else if (letter=='X')    {   return('Y');  }   
        else if (letter=='Y')    {   return('Z');  }   
        else if (letter=='Z')    {   return('a');  }   
        
        else if (letter=='a')    {   return('b');  }   
        else if (letter=='b')    {   return('c');  }           
        else if (letter=='c')    {   return('d');  }       
        else if (letter=='d')    {   return('e');  }     
        else if (letter=='e')    {   return('f');  }     
        else if (letter=='f')    {   return('g');  }   
        else if (letter=='g')    {   return('h');  }      
        else if (letter=='h')    {   return('h');  }   
        
        else
        {
            return(letter);
        }                   
    }
    
    public char letterdown(char letter)
    {
        if (letter=='A')    {   return('A');  }
        else if (letter=='B')    {   return('A');  }     
        else if (letter=='C')    {   return('B');  }    
        else if (letter=='D')    {   return('C');  }   
        else if (letter=='E')    {   return('D');  }   
        else if (letter=='F')    {   return('E');  }   
        else if (letter=='G')    {   return('F');  }    
        else if (letter=='H')    {   return('G');  }   
        else if (letter=='I')    {   return('H');  }   
        else if (letter=='J')    {   return('I');  }   
        else if (letter=='K')    {   return('J');  }    
        else if (letter=='L')    {   return('K');  }   
        else if (letter=='M')    {   return('L');  }          
        else if (letter=='N')    {   return('M');  }   
        else if (letter=='O')    {   return('N');  }   
        else if (letter=='P')    {   return('O');  }       
        else if (letter=='Q')    {   return('P');  }   
        else if (letter=='R')    {   return('Q');  }    
        else if (letter=='S')    {   return('R');  }          
        else if (letter=='T')    {   return('S');  }   
        else if (letter=='U')    {   return('T');  }    
        else if (letter=='V')    {   return('U');  }                
        else if (letter=='W')    {   return('V');  }            
        else if (letter=='X')    {   return('W');  }   
        else if (letter=='Y')    {   return('X');  }   
        else if (letter=='Z')    {   return('Y');  } 
        
        else if (letter=='a')    {   return('Z');  }   
        else if (letter=='b')    {   return('a');  }           
        else if (letter=='c')    {   return('b');  }       
        else if (letter=='d')    {   return('c');  }     
        else if (letter=='e')    {   return('d');  }     
        else if (letter=='f')    {   return('e');  }   
        else if (letter=='g')    {   return('f');  }      
        else if (letter=='h')    {   return('g');  }           
        else
        {
            return(letter); 
        }    
    } 
    
    public char letterrandom(char letter)
    {
        Boolean letterisletter=false;
        
        int rand = RandomInt(0, 33);
        
             if (letter=='A')    {   letterisletter=true;  }
        else if (letter=='B')    {   letterisletter=true;  }     
        else if (letter=='C')    {   letterisletter=true;  }    
        else if (letter=='D')    {   letterisletter=true;  }   
        else if (letter=='E')    {   letterisletter=true;  }   
        else if (letter=='F')    {   letterisletter=true;  }   
        else if (letter=='G')    {   letterisletter=true;  }    
        else if (letter=='H')    {   letterisletter=true;  }   
        else if (letter=='I')    {   letterisletter=true;  }   
        else if (letter=='J')    {   letterisletter=true;  }   
        else if (letter=='K')    {   letterisletter=true;  }    
        else if (letter=='L')    {   letterisletter=true;  }   
        else if (letter=='M')    {   letterisletter=true;  }          
        else if (letter=='N')    {   letterisletter=true;  }   
        else if (letter=='O')    {   letterisletter=true;  }   
        else if (letter=='P')    {   letterisletter=true;  }       
        else if (letter=='Q')    {   letterisletter=true;  }   
        else if (letter=='R')    {   letterisletter=true;  }    
        else if (letter=='S')    {   letterisletter=true;  }          
        else if (letter=='T')    {   letterisletter=true;  }   
        else if (letter=='U')    {   letterisletter=true;  }    
        else if (letter=='V')    {   letterisletter=true;  }                
        else if (letter=='W')    {   letterisletter=true;  }            
        else if (letter=='X')    {   letterisletter=true;  }   
        else if (letter=='Y')    {   letterisletter=true;  }   
        else if (letter=='Z')    {   letterisletter=true;  } 
        
        else if (letter=='a')    {   letterisletter=true;  }   
        else if (letter=='b')    {   letterisletter=true;  }           
        else if (letter=='c')    {   letterisletter=true;  }       
        else if (letter=='d')    {   letterisletter=true;  }     
        else if (letter=='e')    {   letterisletter=true;  }     
        else if (letter=='f')    {   letterisletter=true;  }  
        else if (letter=='g')    {   letterisletter=true;  }    
        else if (letter=='h')    {   letterisletter=true;  }   
             
        if (letterisletter==true)     
        {
            if (rand==0)    {return('A');   }
            if (rand==1)    {return('B');   }                
            if (rand==2)    {return('C');   }   
            if (rand==3)    {return('D');   }        
            if (rand==4)    {return('E');   }        
            if (rand==5)    {return('F');   }        
            if (rand==6)    {return('G');   }               
            if (rand==7)    {return('H');   }           
            if (rand==8)    {return('I');   }           
            if (rand==9)    {return('J');   }           
            if (rand==10)    {return('K');   }           
            if (rand==11)    {return('L');   }           
            if (rand==12)    {return('M');   }   
            if (rand==13)    {return('N');   }           
            if (rand==14)    {return('O');   }           
            if (rand==15)    {return('P');   }           
            if (rand==16)    {return('Q');   }           
            if (rand==17)    {return('R');   }   
            if (rand==18)    {return('S');   }           
            if (rand==19)    {return('T');   }           
            if (rand==20)    {return('U');   } 
            if (rand==21)    {return('V');   }   
            if (rand==22)    {return('W');   }   
            if (rand==23)    {return('X');   }           
            if (rand==24)    {return('Y');   }   
            if (rand==25)    {return('Z');   }   

            if (rand==26)    {return('a');   }   
            if (rand==27)    {return('b');   }           
            if (rand==28)    {return('c');   }           
            if (rand==29)    {return('d');   }           
            if (rand==30)    {return('e');   }           
            if (rand==31)    {return('f');   }   
            if (rand==32)    {return('g');   }     
            if (rand==33)    {return('h');   }       
        }     
        return(letter);
    }    
    
    public char letterinvert(char letter)
    {
        if (letter=='A')    {   return('h');  }
        else if (letter=='B')    {   return('g');  }     
        else if (letter=='C')    {   return('f');  }    
        else if (letter=='D')    {   return('e');  }   
        else if (letter=='E')    {   return('d');  }   
        else if (letter=='F')    {   return('c');  }   
        else if (letter=='G')    {   return('b');  }    
        else if (letter=='H')    {   return('a');  }   
        else if (letter=='I')    {   return('Z');  }   
        else if (letter=='J')    {   return('Y');  }   
        else if (letter=='K')    {   return('X');  }    
        else if (letter=='L')    {   return('W');  }   
        else if (letter=='M')    {   return('V');  }          
        else if (letter=='N')    {   return('U');  }   
        else if (letter=='O')    {   return('T');  }   
        else if (letter=='P')    {   return('S');  }       
        else if (letter=='Q')    {   return('R');  }   
        else if (letter=='R')    {   return('Q');  }    
        else if (letter=='S')    {   return('P');  }          
        else if (letter=='T')    {   return('O');  }   
        else if (letter=='U')    {   return('N');  }    
        else if (letter=='V')    {   return('M');  }                
        else if (letter=='W')    {   return('L');  }            
        else if (letter=='X')    {   return('K');  }   
        else if (letter=='Y')    {   return('J');  }   
        else if (letter=='Z')    {   return('I');  }   
        
        else if (letter=='a')    {   return('H');  }   
        else if (letter=='b')    {   return('G');  }           
        else if (letter=='c')    {   return('F');  }       
        else if (letter=='d')    {   return('E');  }     
        else if (letter=='e')    {   return('D');  }     
        else if (letter=='f')    {   return('C');  }     
        else if (letter=='g')    {   return('B');  }    
        else if (letter=='h')    {   return('A');  }            
        else
        {
            return(letter);
        }                   
    }    
    
    public String inverseletter(String prestring,int lettertoinverse)
    {
        String newstring = "";
    
        for (int i = 0; i < prestring.length(); i ++)
        {
            if (i == lettertoinverse)
            {
                newstring += letterinvert(prestring.charAt(i));
            }
            else
            {
                newstring += prestring.charAt(i);
            }
        }
        return(newstring);
    }
    
      
    
    public String insertsubstring(String originalstring,String insertstring,int indexstart,int indexend)
    {

        String substring1 ="";
        String substring2 ="";
        String substring3= "";
        
        int originalstringlength = originalstring.length();
        int insertstringlength = insertstring.length();
        
        for(int j = 0; j<(insertstringlength) ;j++)
        {        
            substring2 += insertstring.charAt(j);
        }    
        for(int i = 0; i<(originalstringlength) ;i++)
        {           
            if (i < indexstart)
            {
                substring1 +=originalstring.charAt(i);
            }
            else if ( i >= indexend)
            {
                substring3 +=originalstring.charAt(i);
            } 
        }      
                     
        return(substring1+substring2+substring3);
    }
    
    public String removesubstring(String originalstring,String insertstring,int indexstart,int indexend)
    {

        String substring1 ="";
        String substring3= "";
        
        int originalstringlength = originalstring.length();
          
        for(int i = 0; i<(originalstringlength) ;i++)
        {           
            if (i < indexstart)
            {
                substring1 +=originalstring.charAt(i);
            }
            else if ( i > indexend)
            {
                substring3 +=originalstring.charAt(i);
            } 
        }              
        return(substring1+substring3);
    }    
}



If it’s already heavily used in this way throughout your code, then perhaps you could leave it??? In theory you’re not going to gain blistering increases in performance by changing it. But for your future reference keep in mind that utility classes like this are very suited to being static (take java.lang.Math as an example).

*Just to add: It looks like you’ve made a utility class that will be re-used in other programs? In this case, yes I’d change your methods to static. You should be able to do this without breaking your current code that uses objects.

yes I have thousends of objects that use this object. thanx for the reply. think I will leave it this way

Also…

your methods tell me that you are quite new to programming and Java. You duplicate a lot of code which is either present already in the standard library or doing stuff which is extremely unperformant or code size.

yes I am
this is my first project
can you tell me which part is unperformant?

Hey, you need to clean up your code. (Or at least make the same mistakes consistantly). You sometimes use camel case and then other times you dont. I recomend never starting a method off with a capital letter, that implys that it is a constructor, which it isnt.

Would this work for your intToLetter?

public static String intToLetter(int input) {
String[] alphabet = new String[] {“a”,“b”,“c”,“d”,“e”,“f”,“g”,“h”,“i”,“j”,“k”,“l”,“m”,“n”,“o”,“p”,“q”,“r”,“s”,“t”,“u”,“v”,“w”,“x”,y",“z”};
return alphabet[input];
}

;D

Actually, you could take my previously stated method and adapt it to alot of your needs.
And you could make alphabet a constant, to avoid repetitive code.

Hope I helped. :slight_smile:

here did some fixes/changes:


public class Physics extends ROGameObject
{
    public Random r = new Random();  
    
    
    @Override
    public void update()
    {
        
    }

    //check if two objcts collide
    public boolean checkCollisions(ROGameObject go1, ROGameObject go2)
    {
        Rectangle r1 = new Rectangle((int)go1.getx(),(int)go1.gety(),(int)go1.getxsize(),(int)go1.getysize());
        Rectangle r2 = new Rectangle((int)go2.getx(),(int)go2.gety(),(int)go2.getxsize(),(int)go2.getysize());
        
        return r1.intersects(r2);
    }  
    
+    //use Math.abs(...)	
-    public int removenegative(int value)
-    {
-        if (value < 0)
-        {
-            return (value*-1);
-        }
-        return value;             
-    }
    
    
    
    //function checks if a value is between minimum and maximum value
    public float floatbetweenfunction(float minvalue, float midvalue, float maxvalue)
    {       
-        if (minvalue < midvalue && midvalue < maxvalue)
-        {
-            return (midvalue);
-        }
        if (midvalue <= minvalue )           
        {    
            return (minvalue);
        }
+        else if (midvalue >= maxvalue )           
        {    
            return (maxvalue);
        }
+	else
             return (midvalue);
+	//but why not use Math.max(Math.min(maxvalue, midvalue), minvalue);
    }
    
+    //no need see above
-    public int intbetweenfunction(int minvalue, int midvalue, int maxvalue)
-    {       
-        ...
-    }    
    
    //returns a random number between minvalue and maxvalue
    public int RandomInt(int minvalue, int maxvalue)
    {          
        return (minvalue + (int)(Math.random() * ((maxvalue - minvalue) + 1)));   
    }
    
+   //never use for Strings for math, pls...
    public String Randombit()
    {
        ...
    }
   
+    //same as aboth
    public char flipbit(char bit)
    {
	...
    }  
  
+   //why?
    public String flipbitstring(String newstring)
    {
     ...
    }
    
+    //Math.max(...)
    public int MaxInt(int value1, int value2)
    {     
    }    
    
    public static double PytagorasInt(int value1, int value2)
    {    
+	//no need to convert to double, I would say counter productive. If you should convert to long 
        double nvalue1 = (double)value1;
        double nvalue2 = (double)value2;        
        double int1 = (nvalue1*nvalue1) + (nvalue2*nvalue2);
        
        return Math.sqrt(int1);
   
    }      
  
    public char inttoletter(int newint)
    {    
+	if(newint < 0 && newint > 33)
+		return 'A';
+	else if(newint < 26)
+		return (char)(newint + 'A');
+	else
+		return (char)(newint - 26 + 'a');        
    }

    public float lettertofloat(char letter)
    {
+	return lettertoint(letter) / 33f;               
    }    
    
    //number between -1 and 1
    public float lettertofloat2(char letter,float tresholdfactor)
    {
+	return tresholdfactor * (-1 + lettertofloat(letter));	           
    }        
    
    public int lettertoint(char letter)
    {
+	if(letter >= 'A' && letter <= 'Z')
+		return letter - 'A';
+	else if(letter >= 'a' && letter <= 'h')
+		return letter - 'h';
+	else
+		return 0;              
    }    
    
    
    public char letterup(char letter)
    {
+	if((letter >= 'A' && letter < 'Z') || (letter >= 'a' && letter < 'h'))
+		return letter + 1;
+	else if(letter == 'Z')
+		return 'a';
+	else if(letter == 'h')
+		return 'h';
+	else
+		return letter;                  
    }
    
    public char letterdown(char letter)
    {
+	if((letter > 'A' && letter <= 'Z') || (letter > 'a' && letter <= 'h'))
+		return letter - 1;
+	else if(letter == 'a')
+		return 'Z';
+	else if(letter == 'A')
+		return 'A';
+	else
+		return letter;   
    } 
    
    public char letterrandom(char letter)
    {
+	if((letter >= 'A' && letter <= 'Z') || (letter >= 'a' && letter <= 'h')){
+        	return inttoletter(RandomInt(0, 33));
+	}    
        return(letter);
    }    
    
    public char letterinvert(char letter)
    {
+	if((letter >= 'A' && letter <= 'Z') || (letter >= 'a' && letter <= 'h')){
+        	return inttoletter( 33 - lettertoint(letter) );
+	}    
        return(letter);               
    }    
    
    public String inverseletter(String prestring,int lettertoinverse)
    {
+        String first = prestring.substring(0, lettertoinverse - 1);
+        String last = prestring.substring(lettertoinverse + 1, prestring.length());
+        return first + letterinvert(prestring.charAt(lettertoinverse)) + last;
    }
    
      
    
    public String insertsubstring(String originalstring,String insertstring,int indexstart,int indexend)
    {
        String substring1 = originalstring.subString(0, indexstart);
        String substring3= originalstring.subString(indexenx, originalstring.length());
        
-        for(int j = 0; j<(insertstringlength) ;j++)// wtf
-        {        
-            substring2 += insertstring.charAt(j);
-        }        
                     
+        return substring1 + insertstring2 + substring3;
    }
    
    public String removesubstring(String originalstring,String insertstring,int indexstart,int indexend)
    {

        //also with substring
    }    
}

thank you very much. I used most of your improvements.

that improved for performance a bit. why didnt I think of this?
thanks!