목차
break, return과 continue
- break, return과 continue는 기본적으로 (표면상)같은 동작을 한다.
public static void func01() {
for(int i = 0; i < 10; i++) {
if(i > 5) break;
System.out.println("func01-" + i);
}
}
public static void func02() {
for(int i = 0; i < 10; i++) {
if(i > 5) return;
System.out.println("func02-" + i);
}
}
public static void func03() {
for(int i = 0; i < 10; i++) {
if(i > 5) continue;
System.out.println("func03-" + i);
}
}
break
반복, switch문의 탈출의 의미한다. (최근접 반복문)
return
해당 메서드의 종료를 의미한다.
continue
반복문의 상단으로 돌아감.
배열과 String
배열의 선언과 초기화
int[] arr1;
: 배열변수 선언
int arr3[] = new int[5]
로도 선언될 수 있다. (하지만 사용하지 않는걸 추천)arr1 = new int[3];
: 초기화
int[] arr1;
arr1 = new int[3];
System.out.println(arr1);
Ex05 me = new Ex05();
System.out.println(me); // [I@2a139a55
System.out.println(me.toString()); // com.Day06.Ex05@15db9742
System.out.println(me.getClass()); // [I@2a139a55
System.out.println("@");
System.out.println(Integer.toHexString(me.hashCode())); // 15db9742
}
java는 포인터가 없기 때문에 참조변수를 사용한다.
배열은 참조변수이다.
참조변수의 default값은 null이다.
따라서 배열을 생성과 선언을 같이 한다면 다음과 같이 할 수 있다.
int[] arr1 = new int[] {1, 3, 5, 7};
하지만 다음과 같은 초기화는 안됨:
int[] arr1;
arr1 = {1, 3, 5, 7};
=> 배열의 자료형을 추론할 수 없기 때문이다.
배열 복사
얕은복사
int[] arr1 = new int[] {1, 3, 5, 7};
int[] arr2 = arr1; // 배열의 얕은복사
arr1[2] = 6;
for(int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}
깊은복사
1. 직접 입력하여 복사
int[] arr2 = new int[arr1.length];
for(int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
arr1[2] = 6;
for(int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}
2. 시스템이 제공하는 배열복사를 이용 System.arraycopy()
int[] arr3 = new int[arr1.length];
System.arraycopy(arr1, 0, arr3, 0, 4);
for(int i = 0; i < arr3.length; i++) {
System.out.println(arr3[i]);
}
3. util 패키지의 Arrays를 이용 java.util.Arrays.copyOfRange()
int[] arr4 = java.util.Arrays.copyOfRange(arr1, 0, arr1.length);
// for(int i = 0; i < arr4.length; i++) {
// System.out.println(arr4[i]);
// }
System.out.println(java.util.Arrays.toString(arr4)); // 간편히 배열을 프린트할때 사용
String의 method
- String은 문자열을 메모리에 미리 저장해 그 주소를부여한다. 만약 값이 변하게 되면 메모리 안의 값을 바꾸지 않고 새로운 String을 만든다.
- Scanner는 입력 받은 순간 새로운 객체를 생성한다.
// 문자열
String msg1 = "abcd";
String msg2 = new String("abcd");
String msg3 = new String(new char[] {'a', 'b', 'c', 'd'});
String msg4 = new String(new byte[] {97, 98, 99, 100});
System.out.println(msg1);
System.out.println(msg2);
System.out.println(msg3);
System.out.println(msg4);
String msg5 = "abcd";
// 문자열 = 참조변수
System.out.println(msg1 == msg2); // false
System.out.println(msg1 == msg3); // false
System.out.println(msg1 == msg4); // false
System.out.println(msg1 == msg5); // true
msg5 = "dsaf";
System.out.println(msg1 == msg5); // false
java.util.Scanner sc = new java.util.Scanner(System.in);
String msg6 = sc.nextLine(); // false
- String을 합치면 새로운 문자열을 만든다.
String msg1 = "ja";
String msg2 = "va";
System.out.println(msg1 + msg2);
System.out.println(msg1.hashCode());
System.out.println(msg2.hashCode());
System.out.println((msg1+msg2).hashCode());
=> 해당 사안은 모든 String 클래스에 해당된다.
String msg3 = "java";
String msg4 = new String("java");
System.out.println(msg3==msg4); // 레퍼런스 비교 (false)
System.out.println(msg3.equals(msg4)); // valuse 값 비교 (true)
=> switch는 자동으로 value값 비교를 한다. (if는 아님)
String의 return에 따른 길이 비교
char[] arr1 = msg3.toCharArray();
byte[] arr2 = msg3.getBytes();
System.out.println(arr1.length==arr2.length); // true
String msg5 = "자바";
char[] arr3 = msg5.toCharArray();
byte[] arr4 = msg5.getBytes();
System.out.println(arr3.length==arr4.length); // false
String에서 특정 char 뽑기
String msg = "a ab abc abcd";
System.out.println(msg.charAt(3));
System.out.println(msg.indexOf('b')); // 제일 처음 나오는 'b' 찾기
System.out.println(msg.indexOf('b', 4)); // 4번부터 시작해서 'b' 찾기
System.out.println(msg.lastIndexOf('b')); // 마지막부터 시작해서 'b' 찾기
System.out.println(msg.indexOf("ab"));
System.out.println(msg.indexOf("ab", 3));
System.out.println(msg.contains("abcd"));
String의 특정 문자 바꾸기/지우기
String msg2 = msg.replace('a', 'A');
String msg3 = msg.replace("abc ", "Abc ");
String msg4 = msg.replace("abc ", "");
String의 기타 method들
String msg = "a ab abc abcd";
System.out.println(msg.codePointAt(0)); // 97
String msg2 = "https://naver.com";
System.out.println(msg2.endsWith(".com"));
System.out.println(msg2.startsWith("http"));
String msg3 = "";
System.out.println(msg3.length() == 0);
System.out.println(msg3.isEmpty()); //null일 경우 error
System.out.println(msg2.substring(8)); // naver.com
System.out.println(msg2.substring(8, 13)); // naver
System.out.println(msg2.substring(8, 13)); // naver
String msg4 = "Hello Java";
System.out.println(msg4.toUpperCase());
System.out.println(msg4.toLowerCase());
System.out.println(msg.toString()); // 다른 참조변수와는 다르게 코드가 아닌 string으로 호출가능
System.out.println(msg4.trim()); // 앞뒤의 공백만 없애준다.
static String mothod들
int su = 25;
char ch = 'A';
System.out.println("20" + su);
System.out.println("20" + ch);
System.out.println(20 + String.valueOf(su));
System.out.println(20 + String.valueOf(ch));
System.out.println(String.join(", ", "java ", "web ", "framework"));
System.out.println(String.join(" + ", "5 ", "2 "));
비교하기
String msg1 = "abcd";
String msg2 = "abcd";
String msg3 = "abcde";
System.out.println(msg1.compareTo(msg2)); // 0 (같다)
System.out.println(msg1.compareTo(msg3)); // -1 (1만큼 다름)
System.out.println(msg3.compareTo(msg1)); // 1
- 다름에서 얼마나 다른지를 나타내준다.
- 모두가 같을 경우 길이를 비교한다.
문자열을 10진수 정수로 바꾸기
String msg = "12";
int su = Integer.parseInt(msg);
System.out.println(su);
'100일 챌린지 > 빅데이터기반 인공지능 융합 서비스 개발자' 카테고리의 다른 글
Day 08 - 인터페이스와 java.lang 클래스 (0) | 2024.07.31 |
---|---|
Day 07 - 상속 (0) | 2024.07.30 |
Day05 - 배열과 문자열 (0) | 2024.07.26 |
Day04 - Package와 생성자 사용하기 (0) | 2024.07.25 |
Day 03 - java의 문법 (0) | 2024.07.24 |