JAV) API 클래스

조한미르·2024년 4월 23일
0
  • API
    - ApplicationProgramningInterface

    • 기능, 클래스, 인터페이스를 구현하는 문서.
  • 자바 Document

      /** document주석!!
      *
      * 학생 정보를 담는 클래스이다.
      *
      */
      public class Student { 
          /**
          * 학생 이름
          */
          private String myName; 
          /**
          * 학생 나이
          */
          private int age;

          /**
          *학생 이름을 변환하는 메서드
          *
          */
          public String getMyName() {
              return myName;
          }

          /**
          *
          *학생이름을 등록하는 메서드
          */
          public void setMyName(String myName) {
              this.myName = myName;
          }

          public int getAge() {
              return age;
          }
          public void setAge(int age) {
              this.age = age;
          }

      }

  • 객체 비교
      public class StringCompareExam {
          public static void main(String[] args) {


              String apple1 = "사과";
              String apple2 = "사과";
              String apple3 = new String("사과");

              //비교
              System.out.println("apple1 == apple2 : " +(apple1 == apple2));
              System.out.println("apple3 == apple2 : " +(apple3 == apple2));


              //객체의 위치값 출력
              System.out.println(" apple1 : " + System.identityHasyCode(apple1));
              System.out.println(" apple2 : " + System.identityHasyCode(apple2));
              System.out.println(" apple3 : " + System.identityHasyCode(apple3));

          }

       }

      -> apple1 == apple2 : true
         apple3 == apple2 : false
         apple1 : 1365202186
         apple2 : 1365202186
         apple3 : 1651191114
  • String class
    • int length()
      • 문장열의 길이 반환
    • char charAt(int idex)
      • 문자열을 하나의 단어 단위로 출력
        • 파라미터로는 추출할 문자열의 위치를 받음
    • int indexof(String ch) / int indexOf(intch)
      • 문자열에 포함된 단어 또는 뮨자열의 위치를 앞에서부터 검색했을 때 일치하는 위치의 인덱스 값을 반환(없을 경우에는 -1을 반환)
    • String replace(변경 할 문자, 변경 문자)
      • 단어 또는 문장에 있는 특정 단어를 변경
    • String substring(int beginIndex)
      • 문자열을 원하는 위치에서 자를 때 사용
        • 입력된 시작 위치부터 문자열의 마지막까지 변환
    • String substring(int beginIndex, int endIndex)
      • 문자열을 입력된 시작 위치부터 마지막 위치 전까지의 값을 리턴
  • StringTest
      public class StringTest {

          public static void main(String[] args) {

              String str = "오늘은 비가 너무 많이 옵니다. 다들 비를 조심하세요.";

              System.out.println("문장의 길이 : "+ str.length());

              System.out.println("문장 추출 : "+ str.charAt(11));

              System.out.println("해당 문자 위치 : "+ str.indexOf("이");

              System.out.println("해당 문자 마지막 위치 : "+ str.lastIndexOf("비"));

              String str2 = "11오늘은 비22가 너무 많이 옵니다. 33다들 비를 조심하세요.";

              System.out.println("str2.replaceAll("[0-9]", ""));

              String str3 = "오늘은 날씨가 험하니 집에 일찍 귀가하세요.";
              System.out.println(str3.substring(11));
              System.out.println(str3.substring(0, 10));

          }
       }


       -> 문장의 길이 :30
          문장 추출 :이
          해당 문자 위치 :4
          해당 문자 마지막 위치 :21
          오늘은 비가 너무 많이 옵니다. 다들 비를 조심하세요.
           집에 일찍 귀가하세요.
          오늘은 날씨가 험하
  • StringBuffer 와 StringBuilder
    • 자바에서 문자열을 처리하는 변수는 String 객체입니다. String 클래스는 최초 지정된 문자열 이후에 값이 추가되면 내부적으로 새로운 메모리룰 할당해 새롭게 문자열을 등록합니다.

실습)

	public class StringBuilderTest {
    	public static void main(String[] args) {
        
        	StringBuilder sb = new StringBuilder();
            
            sb.append("안녕하세요\n"); // 이전 문장 뒤에 붙는다.
            sb. append("만나서 반갑습니다");
            
            System.out.println(sb);
            
            //삭제
            //- delete(int start, int end); start <= x < end
            sb.delete(0, 5);
            System.out.println(sb);
            
            //삽입
            sb.insert(5, "안녕히세요");
            System.out.println("=====================");
            System.out.println(sb);
            System.out.println("=====================");
            System.out.println(sb.reverse());
        }
    }
    
    
    
    
    
    -> 안녕히세요
       만나서 반갑습니다
       
       만나서 반갑습니다
       =====================
       안녕히세요
       만나서 반갑습니다
       =====================
       다니습갑반 서나만
       요세하녕안
  • Math 클래스
    • Math 클래스는 수학에서 자주 사용하는 상수들 과 함수들을 미리 구현해 놓은 클래스로 자바에서 수학 계산이 필요 할 때 주로 사용 된다.
  • Wrapper 클래스
  • 프로그램에 따라 기본 타입의 데아터를 객체형으로 표현해야하는 경우가 있다. 이를 위해 자바에서는 기본 자료형을 객체로 다루기 위한 클래스를 제공하는데 이를 Wrapper 클래스라고 함.
  • 오토박싱과 오토언박싱
    - 기본 타입의 데이터를 Wraper클래스의 인스턴스로 변환하는 과정을 박싱(Boxing)이라고 하고, 반대로 Wraper클래스의 인스턴스에 저장된 값을 기본 타입의 데이터로 꺼내는 과정을 언박싱(UnBoxing)이라고 합니다.
              
  • WrapperTest01

public class WrapperTest01 {

public class WrapperTest01 {
	public static void main(String[] args) {
    
    	Integer num01 = 10; // autoBoxing 가능
        //Integer num02 = new Integer(10); // 이제 사용 불가.
        Integer num03 = Integer.valueof(10);
        Integer num04 = Integer.valueof("10"); // 문자타입 숫자(정수) --> 정수
        
        Double dnum 01 = 10.11;
        Double dnum02 = Double.valueOf("10.11");
        Double dnum03 = Double.valueOf("10");
        
        System.out.println("정수 : " + num01);
        System.out.println("실수 : " + num02);
        
	}
}
-> 정수 : 10
   실수 : 10.11 
  • WrapperTest02

public class WrapperTest02 { 
	public static void main(String[] args) {
    
    
    	String inStr = "70";
        String doubleStr = "60.77";
        
        
        /*
        * 정수 경우
        * valueof() -->  정수 또는 문자형 타입 정수, 리턴타입 : Integer
        * ParseInt() --> 문자형 타입 정수, 리턴타입 : int 
        */ 
        int myScore = Integer.parseInt(intStr);
        double cutLine = Double.parseDouble(doubleStr);
        
        if(myScore >= cutLine) {
        	System.out.println("합격!");
        }else {
        	System.out.println("불합격!");
        }
        
 	}
}



-> 합격!
profile
꼭 해내는 사람

0개의 댓글