HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🤩
개발
/
Java
Java
/
💯
Java 기본
/기본개념/
🥅
For loop.(brek specific loop), Deep&Shallow Copy
🥅

For loop.(brek specific loop), Deep&Shallow Copy

For문 break for specific loop

public class Test { public static void main(String[] args) { outerloop: for (int i=0; i < 5; i++) { for (int j=0; j < 5; j++) { if (i * j > 6) { System.out.println("Breaking"); break outerloop; } System.out.println(i + " " + j); } } System.out.println("Done"); } }
  • loop에 이름을 붙임으로써 nested loop에서 제일 바깥의 loop를 break할 수 있음

Deep copy & Shallow Copy

int[] num = {1, 2, 3, 4}; int[] deepCopy = Arrays.copyOf(num, num.length); // deep copy int[] shallowCopy = num; // shallow copy System.out.println(System.identityHashCode(num)); // 1159190947 System.out.println(System.identityHashCode(deepCopy)); // 925858445 System.out.println(System.identityHashCode(shallowCopy)); // 1159190947
  • Shallow Copy는 객체를 생성하고 그 객체가 참조하는 곳을 원본 객체의 주소값을 가리키도록 함 ⇒ 원본 객체의 값이 바뀌면 Copy의 값도 바뀜
    • In shallow copy, only fields of primitive data type are copied while the objects references are not copied.
  • DeepCopy는 새로운 객체를 생성하고 원본 객체의 값을 그대로 복사함 ⇒ 원본 객체의 값이 바뀌더라도 아무 상관 없음
    • Deep copy involves the copy of primitive data type as well as object references