일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- 사이드 프로젝트
- CodeDeploy
- 토비의 스프링
- springboot
- java
- mutable
- Github
- git
- compiler
- build_test
- template
- Spring
- QueryDSL
- aws
- EC2
- immutable
- Airflow
- AOP
- string
- workflow
- JUnit
- 알고리즘
- redis
- rds
- kotlin
- JPA
- Action
- db
Archives
- Today
- Total
개발 일기
mutable 과 immutable 본문
오늘 String과 StringBuilder의 속도 차이에 대해 검색을 하다
mutable , immutable의 대해 알게 되었다.
아래 링크의 정보를 참고하면 좋다.
https://zetawiki.com/wiki/%EC%9D%B4%EB%AE%A4%ED%84%B0%EB%B8%94,_%EB%AE%A4%ED%84%B0%EB%B8%94
이뮤터블, 뮤터블 - 제타위키
다음 문자열 포함...
zetawiki.com
MutableClass
생성된 후 값의 내용을 변경할 수 있는 클래스다.
ex ) String을 제외한 참조 타입 변수들
ImmutableClass
인스턴스가 생성된 후에는 값을 변경할 수 없는 클래스
ex) String 등등
Example
Mutable 클래스의 예시 코드다
public class MutableClassExample {
private String name;
public MutableClassExample(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Setter로 값을 변경할 수 있다.
Immutable 클래스의 예시 코드다
(VO 도 Immutable 가 될 수 있다 Setter 가 없기 때문 )
public class ImmutableClassExample {
private String name;
public ImmutableClassExample(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
한번 생성자로 인스턴스를 생성한 후 값을 변경할 수단이 존재하지 않는다.
마지막으로 두 클래스의 값을 변경하거나 변경하지 못하는 예시이다
public class ExampleTest {
public static void main(String[] args) {
// Mutable
MutableClassExample mutableClassExample = new MutableClassExample("홍길동"); //인스턴스 생성
mutableClassExample.setName("김철수"); // 변경이 가능하다.
ImmutableClassExample immutableClassExample = new ImmutableClassExample("김영희"); //인스턴스 생성
//변경이 불가능하다.
}
}
결론
mutable는 변경 가능 클래스
immutable는 변경이 불가능한 클래스이다.
'Java' 카테고리의 다른 글
String 과 StringBuilder 의 속도차이 (0) | 2021.07.09 |
---|