내가 좋아하는, 루프 내부의 객체를 선언에 대한 몇 가지 다른 질문을 읽었습니다 :자바 루프 객체 선언은 가장 좋은 방법
Is it Better practice to Declare individual objects or loop Anonymous objects into ArrayList?
Java : declaring objects in a loop
하지만 둘 다 정말 내 문제를 해결합니다.
나는 반복적으로 사용자 입력에 대한 검사 및 해당 문자열을 구문 분석하는 클래스를 만드는거야마다 반복 :
public static void main(String[] args) {
while (true) {
System.out.print("Enter a string of brackets to test: ");
String exp = new Scanner(System.in).nextLine().trim();
if (exp.equals("q")) break; // q is the 'quit' flag
System.out.println(new BracketMatcher(exp.length()).parse(exp));
}
}
어떤 차이가 있습니까 -
성능이 아닌 범위에서 -이 블록은?
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
BracketMatcher matcher;
while (true) {
System.out.print("Enter a string of brackets to test: ");
String exp = input.nextLine().trim();
if (exp.equals("q")) break; // q is the 'quit' flag
matcher = new BracketMatcher(exp.length());
System.out.println(matcher.parse(exp));
}
나는 그 방법을 사용하기 때문에 BracketMatcher에서 정적 메서드 인 parse()를 만드는 것이 더 좋을까요?
감사합니다.
그 차이가 있다면, 그것을 JIT 컴파일러가 일단 사라지면 빨리 사라집니다. 나는 확실히 알지 못한다 ... – awksp
오, 그래, 거기에 차이가있을 것이다 - '스캐너'에 대한, 'BracketMatcher'. – awksp
예 성능에는 차이가 있지만 중요하지 않습니다. 사용자 입력을 기다리고 있기 때문에 성능상의 이득을 완전히 극복 할 수 있습니다. 비교할 때 성능 차이는 중요하지 않습니다. –