✨
Java
  • 자바의 역사
  • Ch1
    • Before Start Java
      • History of Java
      • Feature of Java
      • API Documentation
      • Hello World!
      • eclipse shortcut
      • Eclipse IDE
      • GitHub to eclipse
  • Ch2
    • Variable
      • Variable
    • Variable Type
    • Character and String
    • Primitive type and range
    • printf and formatter
    • Scanner
    • Overflow of Int
    • Transition between different variable types
    • Object Array
  • CH3
    • Operator
  • CH4
    • 조건문과 반복문
    • if statement
    • switch
    • Math.random()
    • for statement
    • while statement
    • break, continue, named iterated statement
  • CH5
    • Array
    • 배열 활용
    • String Array
  • OOP
    • Intro
  • Class and Object
  • Make multiple Classes in one file
  • How to use Object
  • Object Array
  • Variable type according to declared location
  • Class Variable and Instance Variable
  • Method
  • Call Stack
  • Parameter
  • Static method(Class method) and Instance method
  • (Method)Overloading
  • Constructor
  • Constructor this()
  • Reference type variable "this"
  • Initialize variable
  • Inheritance
  • Composite
  • Single Inheritance, Object Class
  • (Method)Overriding
  • super, super()
  • package, class path
  • import, static import
  • modifier
  • access modifier
  • Encapsulation
  • Polymorphism
  • reference type transition
  • instanceof operator
  • Parameter polymorphism
  • Multiple object using one array
  • Abstract Class, Abstract Method
  • Creating Abstract Class
  • Interface
  • Interface Polymorphism
  • Interface Pros
  • Default method and static method
  • Inner Class
  • Anonymous Class
  • java.lang package and useful class
    • Object class
    • hashCode(), toString()
    • String class
    • StringBuffer class
    • StringBuilder class
    • Math class
    • Wrapper class
    • Number class
    • String to Number, String to Wrapper class
    • Auto-boxing and (auto)Unboxing
  • Collection Framework
    • Collections framework
    • List, Set, Map Interface
    • List의 removeAll() 과 clear() 비교
    • List Interface - ArrayList
    • How to view Java API source
    • List Interface - LinkedList
    • Stack and Queue
    • Iterator, ListIterator, Enumeration
    • Array
    • Comparator와 Comparable
    • Stack
    • String
    • String + char = String
    • String.toCharArray()
    • BufferedReader / BufferWriter
    • Scanner로 String 입력 - next( )와 nextLine( )
    • 공백없이 2차원배열 데이터 String으로 한번에 한줄씩 입력받기(문자/숫자)
    • 공백을 사이에 두고 빠른 2차원 배열 입출력
    • arr[index++]과 arr[index] index++의 차이
    • int와 long의 차이
    • Untitled
    • 타입 간의 변환
    • Array 와 ArrayList
    • valueOf()
    • Char
    • 변수, 객체 초기화(초기값 설정)
  • error troubleshooting
    • No enclosing instance of type
    • ASCII Code Table
    • java.lang.NumberFormatException
    • No enclosing instance..
  • reference
    • String을 생성하는 2가지 방법과 차이점
    • StackOverflowError(스택, 힙 메모리)
    • swtich-case 반복문
Powered by GitBook
On this page

Was this helpful?

  1. reference

String을 생성하는 2가지 방법과 차이점

PreviousNo enclosing instance..NextStackOverflowError(스택, 힙 메모리)

Last updated 3 years ago

Was this helpful?

자바에서 String 객체 생성하는 방법 2가지

  1. String literal : " " 사용 => 내용이 같다면 같은 객체! 동일한 메모리 주소를 갖는다!

  2. new 연산자 사용 => 내용이 같더라도 개별적인 객체

흔히 new 연산자로 String 객체를 생성지 않는 것이 좋다는 말이 있다. 이 말을 이해해보자. String literal로 생성하면 해당 String 값은 Heap 영역 내 "Strinng Constant Pool"에 저장되어 재사용되지만, new 연산자로 생성하면 같은 내용이라도 여러 개의 객체가 Heap영역을 차지한다.

  • String literal 로 생성한 객체는 String Pool에 들어간다.

  • String literal 로 생성한 객체값(ex."Cat")이 이미 String Pool에 존재한다면 해당 객체는 String Pool의 reference를 참조한다!

  • new 연산자로 생성 String 객체는 같은 값이 String Pool에 이미 존재하라도, Heap 영역 내에 별도 객체를 가리키게 된다.

  • (참고) String 클래스의 intern( ) 메서드 해당 String과 동등한 String 객체가 이미 String Pool에 존재한다면 그 객체 그대로 리턴하고, 그렇지 않다면 호출된 String 객체를 StringPool에 추가하고 객체의 reference를 리턴한다.

void internedString(){
    String constantString = "interned String";
    String newString = new String("interned String");
    String internedString = newString.intern();
}
String literal과 new를 사용한 String 차이점