[SWEA][JAVA] 2027. 대각선 출력하기

2021. 6. 14. 15:30· 알고리즘 풀이/SWEA
728x90

StringBuilder 인스턴스를 그대로 넣어준 것과 StringBuilder의 toString()함수를 붙여준 것과의 차이점은 무엇인가요?

 

There's nothing different between line 6 and line 8.

PrintStream.println(Object) calls string.valueOf(Object) which calls the object's toString() method, and that gets printed.

System.out.println(s) and System.out.println(s.toString()) have the same output (unless s is null, in which case the latter throws an exception).

The reason you call s.toString() directly is to get the current "built" value from the StringBuilder as a string so you can pass it on to other code which expects a string. If the code you want to call takes a StringBuilder you could just pass s, but then the called code has the ability to modify your value (they can't with string because it is an immutable data type).

 

출처 : https://stackoverflow.com/questions/46713326/why-do-we-need-to-use-tostring-method-with-stringbuilder

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
 
class Solution {
    
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    
    public static void main(String[] args) throws IOException {
        
        for(int i=0; i<5; i++) {
            StringBuilder str = new StringBuilder("+++++");
            str.setCharAt(i, '#');
            System.out.println(str.toString());
        }
    }
}
Colored by Color Scripter
cs
저작자표시 (새창열림)

'알고리즘 풀이 > SWEA' 카테고리의 다른 글

[SWEA][JAVA] 11592. 크루즈 컨트롤  (0) 2021.06.14
[SWEA][JAVA] 2025. N줄덧셈  (0) 2021.06.14
[SWEA][JAVA] 3143. 가장 빠른 문자열 타이핑  (0) 2021.06.14
[SWEA][JAVA] 2029. 몫과 나머지 출력하기  (0) 2021.06.14
[SWEA][JAVA] 2043. 서랍의 비밀번호  (0) 2021.06.13
'알고리즘 풀이/SWEA' 카테고리의 다른 글
  • [SWEA][JAVA] 11592. 크루즈 컨트롤
  • [SWEA][JAVA] 2025. N줄덧셈
  • [SWEA][JAVA] 3143. 가장 빠른 문자열 타이핑
  • [SWEA][JAVA] 2029. 몫과 나머지 출력하기
배게
배게
배게
백엔드
배게
전체
오늘
어제
  • 분류 전체보기 (430)
    • 알고리즘 풀이 (338)
      • 백준 (167)
      • Codility (22)
      • 프로그래머스 (123)
      • LeetCode (2)
      • CodeForces (9)
      • SWEA (15)
    • 백엔드 (11)
    • Coding existing for (3)
    • 무지성 메모 (40)
    • Debug (30)
    • 자바 (8)

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • hibernate
  • MYSQL
  • 카톡 내보내기한 파일 정렬
  • 카톡
  • 카카오톡
  • 카카오톡 txt파일 정렬

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.1
배게
[SWEA][JAVA] 2027. 대각선 출력하기
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.