일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 개발자도서#개발자책#도메인#DDD#도메인주도개발시작하기#개발스터디#
- 국비지원JAVA
- 항해플러스#항해#항해플러스3기#회고
- Java#정처기#비트연산자#정보처리기사
- 항해99 #항해플러스 #주니어개발자 #주니어개발자역량강화 #주니어개발자멘토링 #개발자사이드프로젝트 #코딩부트캠프 #코딩부트캠프후기
- 국비지원#국비교육#국비지원자바#국비교육자바#css#HTML#JAVA
- IntelliJ#인텔리제이#인텔리#단축키
- 자바#Java#배열예시#연습#기초다지기
- Java#java#메모리영역#클래스로더#가비지컬렉터
- java
- Java#JAVA#매개변수
- Java#java#자바#다오#디티오#브이오#dao#dto#vo
- 쿼리스트링#쿼리문자열#바인딩
- 개발자#it도서#도메인#DDD#ddd
- 국비지원#국비교육
- #
- 프로그래밍
- Spring#spring#스프링#스프링프레임워크#스프링의존성주입#스프링생성자#스프링기본#국비지원#국비교육#국비지원스프링
- tibero#티베로#이중화#failvover
- Java#컴파일러#자바컴파일러#
- 레스트컨트롤러
- 국비지원자바#국비교육자바#국비지원java#국비교육java#자바스크립트#프론트엔드
- spring#Spring#RequestBody#ResponseBody
- 국비지원JAVA#국비지원자바#프로그랭#JSP#국비지원JSP#국비교육JSP#웹개발자#코딩
- html#HTML#프론트엔드#개발자#코딩#국비지원#국비교육#국비지원프론트엔드#국비지원HTML#국비지원html#국비프론트엔드
- #java#JAVA#프로그래밍#웹개발자
- 자바
- 국비지원JAVA#국비교육JAVA#국비지원자바#국비교육자바#JSP#SERVLET#서블릿#
- Spring#spring#스프링#스프링프레임워크#스프링자동주입#스프링생성자#스프링기본#국비지원#국비교육#국비지원스프링
- Resource #
- Today
- Total
개린이 개발노트
국비지원 JAVA(자바) 프로그래밍(인터페이스, 다형성, 내부클래스, 여러가지 예외처리방법, 정규식, 정규표현식, 자바API) 본문
국비지원 JAVA(자바) 프로그래밍(인터페이스, 다형성, 내부클래스, 여러가지 예외처리방법, 정규식, 정규표현식, 자바API)
개린이9999 2022. 11. 30. 09:13클래스 -> ADD -> 인터페이스추가
추상 클래스 인터페이스(정형화를 위해서 사용)는
extents로 받고 implements를 받고 a,b,c(인터페이스 이름)
다중 상속x 다중 상속o->클래스+인터페이스
업캐스팅 자식에서 부모로 형변환 자동 형변환
인터페이스 다형성
package 연습;
public interface Mineral {
int getValue(); // 추상메서드
}
package 연습;
public class Gold implements Mineral {
@Override
public int getValue() {
return 100;
}
}
package 연습;
public class Silver implements Mineral {
@Override
public int getValue() {
return 90;
}
}
package 연습;
public class Bronze implements Mineral {
@Override
public int getValue() {
return 80;
}
}
package 연습;
public class MineralCalculator {
int value=0;
void add(Mineral mineral) {
value=value+mineral.getValue();
}
int getValue() {
return value;
}
}
package 연습;
public class Main {
public static void main(String[] args) {
MineralCalculator mcl= new MineralCalculator();
mcl.add(new Gold());
mcl.add(new Silver());
mcl.add(new Bronze());
System.out.println(mcl.getValue());
}
}
내부클래스
내부클래스 형식
public class A{
public class B{
}
}
https://thisisprogrammingworld.tistory.com/29
기초 #7. 중첩 클래스(인스턴스 멤버 클래스, 정적 멤버 클래스, 로컬 클래스)
본격적으로 들어가기전에! 우선, 인터페이스는 하나의 규격(틀)으로, 생성자가 없다. 즉, 인스턴스화할 수도, 실행시킬 수도 없다. 익명 구현 객체란 익명 클래스를 통해 만들어진 객체로 일회성
thisisprogrammingworld.tistory.com
내부클래스 참조
package 내부클래스;
public class A {
A() {
System.out.println("A객체 생성");
}
static class B {
B() {
System.out.println("B객체 생성");
}
int var1;
static int var2;
void method1() {
System.out.println("static 내부 클래스의 메서드1");
}
static void method2() {
System.out.println("static 내부클래스의 static 메서드2");
}
}
public class C{
C() {
System.out.println("객체 생성");
}
int var1;
void method() {
System.out.println("C클래스의 메서드");
}
}
void method() {
class D {
D() {
System.out.println("D 객체생성");
}
int var1;
void method1() {
System.out.println("D클래스의 메서드");
}
}
D d = new D();
d.var1 = 3;
d.method1();
}
}
그냥 중첩클래스 참조만
package 내부클래스;
import 내부클래스.A.B;
public class Main {
public static void main(String[] args) {
A a = new A(); //A클래스에 대한 객체생성
A.B b= new A.B(); //A안에 있는 B생성자 실행
b.var1=3; //b누르고 .
b.method1();
b.var2=3;
A.B.var2=3;
System.out.println(b.var2);
}
}
내부 인터페이스
예외처리
예외처리 경우
특수한 경우에는 처리 되지않음
예외처리를 이용하기 참조
https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=heartflow89&logNo=220981003640
http://close852.tistory.com/47
예외처리 (Throwable) : Unchecked Exception과 Checked Exception
1. 예외란? (Error vs Exception)먼저 오류(Error)와 예외(Exception)의 개념을 정리하고 넘어가자.오류(Error)는 시스템에 비정상적인 상황이 생겼을 때 발생한다. 이는 시스템 레벨에서 발생하기 때문에 심
close852.tistory.com
Dimiden – 뼈발자의 블로그
뼈발자의 블로그
blog.b4you.net
예외
try {
}catch {
}catch {
}catch {
}finally{
}
package 예외처리;
public class ExceptionEx {
public static void main(String[] args) {
System.out.println(1);
try {
System.out.println(2);
System.out.println(3/0);
System.out.println(4);
} catch (ArithmeticException e) {
System.out.println(e); // 예외처리 관련 코드 할 때 보통 e로 처리
}
System.out.println(6);
}
}
에러가 없는 경우 예외처리
package 예외처리;
public class ExceptionEx {
public static void main(String[] args) {
System.out.println(1);
try {
System.out.println(2);
System.out.println(3);
System.out.println(4);
} catch (ArithmeticException e) {
System.out.println(5); // 에러가 없으므로 캐치가 발생이 안되고 12346 이 실행됨
}
System.out.println(6);
}
}
다양한 예외처리
일반 사람들은 예외코드를 알 수 없으니 예외상황이 발생했을 때 프린트문으로 친절히 알려줌.
package 예외처리;
//다양한 예외처리
public class ExceptionEx2 {
public static void main(String[] args) {
try {
int[] arr= { 1, 2, 3};
System.out.println(arr[3]);
System.out.println(3/0);
Integer.parseInt("a"); //정수로 바꿔주는 코드
} catch (ArithmeticException e) {
System.out.println("0으로 나눌 수 없다.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("인덱스 범위초과.");
} catch (Exception e) {
System.out.println("예외 발생.");
}
}
}
package 예외처리;
public class Exception2 {
public static void main(String[] args) {
System.out.println("DB 연결시작");
try {
System.out.println("DB작업");
System.out.println(3/0);
} catch (Exception e) {
System.out.println(e);
System.out.println(e.getMessage()); //e.getMessage() 오류에 관련된 기본적인 부분만 사용
System.out.println(e.toString());
e.printStackTrace(); // 오류에 관련돼서 가장 세세하게 기술.
} finally {
System.out.println("DB 연결종료");
}
}
}
예외 강제 발생 // 테스트 할 때 사용
예외 떠넘기기 thorws
기본 자바 API
package 자바API;
public class Sample01 {
public static void main(String[] args) {
String str1 = new String("abc");
String str2 = new String("abc");
if (str1==str2) { // String 자체를 비교
System.out.println("같다");
}else {
System.out.println("다르다");
}
if(str1.equals(str2)) { // 안에 있는 값을 비교
System.out.println("같다");
}else {
System.out.println("다르다");
}
}
}
// String 자체를 비교 (번지수, 주소가 같냐), // 안에 있는 값(내용물)을 비교
자바 equals
https://kmj1107.tistory.com/207
[JAVA] 문자열(string) 비교 equals와 == 의 차이점 ( + equals의 반대)
equals와 == 은 어떤 차이점이 있을까요. 기본적으로 이 둘은 모두 양 쪽에 있는 내용을 비교한 값을 boolean type으로 반환한다는 공통점을 가집니다.하지만 차이점이 분명 존재합니당. 1) 형태의 차
kmj1107.tistory.com
//Clone 클론, 객체복사
자바API
package 자바API;
public class Sample03 {
public static void main(String[] args) {
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.home"));
System.out.println(System.getProperty("os.name"));
System.out.println(System.getProperty("file.separatior"));
System.out.println(System.getProperty("user.name"));
System.out.println(System.getProperty("user.home"));
System.out.println(System.getProperty("user.dir"));
}
}
자바 API 이어서 계속
자바 정규식
[Java] 정규표현식 사용법 및 예제 - Pattern, Matcher
자바에서 정규표현식(Regular Expression)'을 사용해보자. 1. 정규표현식(Regular Expression) 정규표현식 혹은 정규식은 특정한 규칙을 가진 문자열의 집합을 표현하는데 사용되는 언어다. 정규 표현식은
hbase.tistory.com
[JAVA] 정규표현식
📋 정규 표현식 문법 정규 표현식 역할 ^ 정규식의 시작 $ 정규식의 끝 . 임의의 한 문자 ? 앞의 문자가 하나 있거나 없을 수 있다 * 앞의 문자가 하나도 없거나 무한히 많을 수 있다 + 앞의 문자가
gh402.tistory.com
package 자바API;
public class Sample04 {
public static void main(String[] args) {
// 123456789.....쭉~
String str = "Hello My Name is Hong Gil Dong";
String str2 = " Hello My Name is Hong Gil Dong ";
System.out.println("===기본 입력 된 값===");
System.out.println("Hello My Name is Hong Gil Dong");
System.out.println("===기본 입력 된 값===");
System.out.println("\n==6번째 문자만 출력==");
System.out.println(str.charAt(6)); //6번째 문자만 출력
System.out.println("\n==\"Hong\"가 몇번째 있는지 찾아라==");
System.out.println(str.indexOf("Hong")); //"Hong"가 몇번째 있는지 찾아라
//(indexOf 문자 시작 위치), (중복시 맨 앞쪽에 있는 것만 인식)
System.out.println("\n==17번째 부터 문자열 출력==");
System.out.println(str.substring(17)); //17번째 부터 문자열 출력
System.out.println("\n==6번째 부터 13번째 전! 까지 문자열 출력 ==");
System.out.println(str.substring(6, 13)); //6번째 부터 13번째 전! 까지 문자열 출력
System.out.println("\n==입력된 알파벳을 모두 소문자로 변경 ==");
System.out.println(str.toLowerCase()); //입력된 알파벳을 모두 소문자로 변경
System.out.println("\n==입력된 알파벳을 모두 대문자로 변경 ==");
System.out.println(str.toUpperCase()); //입력된 알파벳을 모두 대문자로 변경
System.out.println("\n==입력된 문자열 길이 알려줌==");
System.out.println(str.length()); //입력된 문자열 길이 알려줌
System.out.println("\n==괄호안에 입력된 값으로 시작하는지==");
System.out.println(str.startsWith("Hello")); //괄호안에 입력된 값으로 시작하는지
System.out.println("\n==괄호안에 입력된 값으로 끝나는지==");
System.out.println(str.endsWith("Dong")); //괄호안에 입력된 값으로 끝나는지
System.out.println("\n==\"Hong\"을 \"Kim\"으로 변경하여 입력된 값 출력 ==");
System.out.println(str.replace("Hong", "Kim")); //"Hong"을 "Kim"으로 변경하여 입력된 값 출력
str = str.replace("Hong", "Kim");
System.out.println("\n==\"Hong\"을 \"Kim\"으로 변경하여 입력된 값 출력==");
System.out.println(str);
System.out.println("\n==i를 a로 바꾸고 입력된 값 출력 ==");
System.out.println(str.replace("i", "a")); //i를 a로 바꾸고 입력된 값 출력
System.out.println("\n==i라는 단어를 모두 제거 하고 입력된 값 출력 ==");
System.out.println(str.replaceAll("i", "")); //i라는 단어를 모두 제거 하고 입력된 값 출력 (정규식)
System.out.println("\n==toString ==");
System.out.println(str.toString());
System.out.println("\n==불필요한 띄어쓰기는 줄여준다==");
str2 = str2.trim(); //불필요한 띄어쓰기는 줄여준다.
str2 = str2.replace(" ", "");
System.out.println(str2);
System.out.println("\n==해당 식을 String(문자열)로 바꿔라==");
str = String.valueOf(10); //해당 식을 String로 바꿔라
System.out.println(10 + 5);
System.out.println(str + 5);
System.out.println("\n==해당 입력된 값으 배열로 넣어주고자 할 때==");
str = "홍,이,유,안"; //배열로 넣어주고자 할 때
String[] arr = str.split(","); //, <-- 이걸 기준으로 잘라서 배열방에 값을 순서대로 넣어라
for(String a : arr) { //향상된 for문 이용
System.out.println(a);
}
}
===기본 입력 된 값===
Hello My Name is Hong Gil Dong
===기본 입력 된 값===
==6번째 문자만 출력==
M
=="Hong"가 몇번째 있는지 찾아라==
17
==17번째 부터 문자열 출력==
Hong Gil Dong
==6번째 부터 13번째 전! 까지 문자열 출력 ==
My Name
==입력된 알파벳을 모두 소문자로 변경 ==
hello my name is hong gil dong
==입력된 알파벳을 모두 대문자로 변경 ==
HELLO MY NAME IS HONG GIL DONG
==입력된 문자열 길이 알려줌==
30
==괄호안에 입력된 값으로 시작하는지==
true
==괄호안에 입력된 값으로 끝나는지==
true
=="Hong"을 "Kim"으로 변경하여 입력된 값 출력 ==
Hello My Name is Kim Gil Dong
=="Hong"을 "Kim"으로 변경하여 입력된 값 출력==
Hello My Name is Kim Gil Dong
==i를 a로 바꾸고 입력된 값 출력 ==
Hello My Name as Kam Gal Dong
==i라는 단어를 모두 제거 하고 입력된 값 출력 ==
Hello My Name s Km Gl Dong
==toString ==
Hello My Name is Kim Gil Dong
==불필요한 띄어쓰기는 줄여준다==
HelloMyNameisHongGilDong
==해당 식을 String(문자열)로 바꿔라==
15
105
==해당 입력된 값으 배열로 넣어주고자 할 때==
홍
이
유
안
체이닝
sb.append("abc").append(123).append('A').append(false);
StringBuffer <--일반 문자열
한번 정한 값은 바뀌지 않는다.
package 자바API;
public class Main {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println(sb); //객체 생성 후 초기값이 비워져 있어 비워져 있는 값이 출력
sb.append("abc").append(123).append('A').append(false); //체이닝
//체이닝은 작성된 순서대로 실행되서 순서대로 출력됨
System.out.println(sb); //추가된 값들이 출력된다
sb.delete(2, 4); //2부터 4 전! 까지 지워라 (어디서 시작해서 어디 까지 지울건지)
//위치는 0버 부터 시작한다.
System.out.println(sb);
sb.deleteCharAt(4); //특정 위치에 있는거 지울때
System.out.println(sb);
sb.insert(5, "=="); //5번 전에 ==이 추가되게
System.out.println(sb);
}
}
package 자바API;
public class Sample07 {
public static void main(String[] args) {
System.out.println(Math.PI); // 메서드가 아니라 상수임
System.out.println(Math.rint(5.22222)); // 뒤에 소수점 자릿수 버리고 가까운 정수 출력
System.out.println(Math.rint(-5.9999));
int a = (int)(Math.random()*45)+1;
System.out.println(a);
// System.out.println(Math.random());
//
// System.out.println(Math.max(5,3));
// System.out.println(Math.min(5,3));
//
// double a = 5.45;
//
//
// System.out.println(Math.round(a*10)/10.0);
//
// System.out.println(Math.floor(5.8)); // 올림
// System.out.println(Math.ceil(5.4)); // 버림
//
// System.out.println(Math.abs(-10)); // .abs 대문자
//
//
//
}
}
Wrapper class
char-Chracter
int-Integer
int, double,char,long-> 객체
package 자바API;
public class Sample08 {
public static void main(String[] args) {
Integer i1 = new Integer(10); // 박싱-> 객체화시키는것
Integer i2 = new Integer(10);
System.out.println(i1==i2); // 프린트문 안에 조건식 쓸 수 있고, 다만 그게 true or false로 밖에 안나옴
System.out.println(i1.equals(i2));
System.out.println(i1==10); // 언박싱->객체화 됐던 것을 다시 기본타입으로 바꾸는것. 자동으로 int로 형변환 돼서 나옴
Integer num = new Integer(10); // 박싱
int n = num.intValue(); // 언박싱
Character c = 'x'; // 오토 박싱 (알아서 형변환)
char ch = c; // 오토 박싱
}
}
박싱, 언박싱, 오토박싱, 오토 언박싱의 연산
package 자바API;
public class Sample09 {
public static void main(String[] args) {
Integer num1 = 7; // 오토박싱
Integer num2 = new Integer(3); // 박싱
int int1 = num1; // 오토 언박싱
int int2 = num2.intValue(); // 언박싱
Integer result1 = num1+num2;
Integer result2 = int1+int2;
int result3= num1+int1; // num1=7, int1=num1, num1=7 이니까 7+7
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
}
}
'국비지원(국비교육) 웹 개발자 과정' 카테고리의 다른 글
국비지원 JAVA(자바) 프로그래밍 (제네릭,제네릭스,람다,람다식) (0) | 2022.12.02 |
---|---|
국비지원 JAVA(자바) 프로그래밍(전화번호,이메일 정규식,정규표현식,ArrayList,LinkedList) (1) | 2022.12.01 |
국비지원 JAVA(자바) 프로그래밍 웹개발자 ( 상속,다형성,추상화) (0) | 2022.11.29 |
국비지원 JAVA(자바) 프로그래밍 웹개발자 (싱글톤, 캡슐화, 상속화, 오버라이딩, 다형성 까지) (0) | 2022.11.28 |
국비지원 JAVA(자바) 프로그래밍 웹개발자 (계산기 만들기, 오버로딩 ,접근제한자,캡슐화,에어컨 만들기) (0) | 2022.11.25 |