본문 바로가기

Study/Java28

[Java] 문자열 클래스(String, StringBuilder, StringBuffer) String 객체와 메모리 문자열을 다루는 String 클래스(객체)는 데이터가 변하면 메모리 상의 변화가 많아 속도가 느리다 String str = "JAVA"; str = str + "_8"; 문자열이 변경되면 기존의 객체를 버리고, 새로운 객체를 메모리에 생성된다 이때, 기존 객체는 GC에 의해서 메모리 회수가 이루어진다. 결과적으로 메모리의 효율성면에서 떨어진다고 볼 수 있다 클래스에서 객체를 생성할 때 new를 사용하지만, String은 기본 자료형처럼 간단하게 쓸 수 있다는 장점이 있다 StringBuffer, StringBuilder String 클래스의 단점을 보완한 클래스로 데이터가 변경되면 메모리에서 기존 객체를 재활용한다 StringBuffer sf = new StringBuffer(.. 2020. 7. 3.
[Java] Lambda 람다식 기존의 객체 지향이 아닌 함수 지향 프로그래밍 방법 람다식이란? 익명 함수(Anonymous Function)를 이용해서 익명 객체를 생성하기 위한 식 [ 기존 방법 ] InterfaceType 변수 --- 할당(대입) ---> Interface 구현 [ 람다식 방법 ] InterfaceType 변수 --- 할당(대입) ---> Lambda Expressions 람다식 구현 1. 매개변수와 실행문만으로 작성한다(접근자, 반환형, return 키워드 생략) public interface LambdaInterface { public void method(String s1, String s2, String s3); } public static void main(String[] args) { LambdaInte.. 2020. 7. 3.
[Java] 인터페이스와 추상클래스 인터페이스란? - 추상화의 꽃, 추상 method들의 집합 클래스와 달리 객체를 생성할 수 없으며, 클래스에서 구현해야 하는 작업 명세서 반드시 interface에 있는 abstract method를 상속받은 클래스에서 구현해야 한다 인터페이스를 사용하는 이유 인터페이스를 사용하는 이유는 많지만, 가장 큰 이유는 객체가 다양한 자료형(타입)을 가질 수 있기 때문이다 인터페이스 구현 public interface InterfaceA { public void funA(); } public interface InterfaceB { public void funB(); } public class MainClass { public static void main(String[] args) { InterfaceA ia.. 2020. 7. 3.
[Java] 내부(inner) 클래스 & 익명(Anonymous) 클래스 내부(inner) 클래스와 익명(anonymous) 클래스 내부(innter) 클래스 클래스 안에 또 다른 클래스를 선언하는 것으로 이렇게 하면 두 클래스의 멤버에 쉽게 접근할 수 있다 public class OuterClass{ int num = 10; String str1 = "java"; static String str11 = "world"; public OuterClass(){ System.out.println("OuterClass Constructor"); } class InnerClass{ int num = 100; String str2 = str1; public InnerClass() { System.out.println("InnerClass Constructor"); } } class SI.. 2020. 7. 3.