DEV Community

Composite
Composite

Posted on

1

자바와 닷넷의 문자열 연산자 차이

2015-04-06 10:30:11 from blog.hazard.kr

1. ==!= 연산자

닷넷

닷넷은 == 연산자 오버로딩을 통하여 String.Equals 사용하여 값의 동일성을 비교.

자바

자바는 String이 닷넷과 같이 클래스이며 연산자 지원 안하는 특성상 레퍼런스 비교밖에 못하므로 동일한 값 비교 불가.
따라서 개발자가 직접 String.equals를 사용.

2. + 연산자

닷넷 :

string s = "asd" + b + "qwe";
//>> string s = string.Concat("asd", b, "qwe");
Enter fullscreen mode Exit fullscreen mode

String.cs
.NET concat 원리

        [System.Security.SecuritySafeCritical]  // auto-generated
        public static String Concat(String str0, String str1) {
            //Contract 는 Test 및 유효성 검사를 위한 내부 클래스임.
            Contract.Ensures(Contract.Result<string>() != null);
            Contract.Ensures(Contract.Result</string><string>().Length ==
                (str0 == null ? 0 : str0.Length) +
                (str1 == null ? 0 : str1.Length));
            Contract.EndContractBlock();

            if (IsNullOrEmpty(str0)) {
                if (IsNullOrEmpty(str1)) {
                    return String.Empty;
                }
                return str1;
            }

            if (IsNullOrEmpty(str1)) {
                return str0;
            }

            int str0Length = str0.Length;
            //.NET 은 네이티브를 통해 포인터에다가 합칠 문자열 길이를 모두 합산하여 배열에 자리 부여
            String result = FastAllocateString(str0Length + str1.Length);
            //그리고 포인터에다가 순서대로 삽입
            FillStringChecked(result, 0,        str0);
            FillStringChecked(result, str0Length, str1);
            //그리하여 포인터 문자열 출력.
            return result;
        }
Enter fullscreen mode Exit fullscreen mode

자바 :

String s = "asd" + b + "qwe";
//>> String s = new StringBuffer().append("asd").append(b).append("qwe").toString();
Enter fullscreen mode Exit fullscreen mode

StringBuffer.java
자바는 문자열 증가 연산자 약속을 StringBuffer 클래스를 통해 합치며 원리는 닷넷과 차이가 있음.

닷넷과 자바의 문자열 합치기 차이점

닷넷 : 처음부터 합칠 모든 문자열의 길이만큼 자리를 포인터에 할당 후 삽입한 다음 포인터 결과값 출력.
자바 : StringBuffer 특성상 일정 자리를 부여 후 문자열 넣을 때마다 필요 시 일정량 증가 후 삽입한 다음 문자열 출력. (기본값은 +16)

닷넷과 자바의 문자열 합치기 공통점

반복문 등에서 문자열 추가시 닷넷은 StringBuilder, 자바는 StringBuffer를 쓰는 것이 성능상 이득.

여기까지.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (1)

Collapse
 
composite profile image
Composite

참고: 자바의 경우 원래 닷넷과 똑같은 이름인 StringBuilder 인데 잘못 기재했으나, 당시 작성한 원본 형상을 남기기 위해 일부러 수정하지 않음.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay