문준영
새벽 코딩
문준영
전체 방문자
오늘
어제
  • 분류 전체보기
    • 웹 개발
    • JAVA
      • 기본 문법 내용 정리
      • 함수 내용 정리
      • 쉽게 배우는 자바 프로그래밍 문제 풀이
    • HTML
      • HTML
      • CSS
      • 문제풀이
    • JavaScript
    • MYSQL
    • C
      • 기본 문법 내용 정리
      • 백준 알고리즘 (c언어)
      • 자료구조
    • Python
      • 참고 알고리즘
      • 기본 문법 내용 정리
      • 자료구조 내용 정리
      • 백준 알고리즘 (파이썬)
    • 깃허브
    • 멀티잇 풀스택

티스토리

hELLO · Designed By 정상우.
문준영

새벽 코딩

멀티잇 풀스택

1,2 week JAVA

2022. 12. 19. 17:17
헷갈리는 함수 모음

- indexOf

특정 문자의 인덱스를 찾음

자바 배열에서는 indexOf를 지원하지 않고 ArrayList 에서만 지원하므로 asList()를 통해 인덱스를 찾음  

1
2
3
String arr[]= {"abc","abcd","bbc"};
int index=Arrays.asList(arr).indexOf("bbc");
System.out.println(index); // 2 return
cs

 

- toString

배열을 모두 출력

1
2
String arr[]= {"abc","abcd","bbc"};
System.out.println(Arrays.toString(arr));
cs

 

- 가변 인수

인자와 매개변수를 배열같이 사용 

1
2
3
4
5
6
7
8
9
static int Sum(int ...num) {
    int total=0;
    for(int i:num) {
        total+=i;
    }
    return total;
}
 
System.out.println(a(1,2,3)); // return 6
cs
 

- System

System.exit(0) : JVM 강제 종료

 
헷갈리는 약어

OOP - Object Oriented Programming

API - Application Programming Interface

JDK - Java Development tooKit

 

BufferReader 과 Scanner의 차이점
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        
Scanner sc=new Scanner(System.in);
cs

1. 속도 차이 : 버퍼를 사용하여 입력을 받으면 속도가 상당히 효율적

2. 데이터 처리 : 버퍼를 사용하면 많은 양의 데이터를 처리 할 때 효율적

 

 자동 형변환과 강제 형변환
int i=5;
short s;
double d;
// 자동 형변환
// 작은 범위 -> 큰 범위 ex) int -> double 
d=i;
 
// 강제 형변환
// 큰 범위 -> 작은 범위 ex) int -> short
 
//ex1)
//s=i; 에러 발생!
s=(short)i;
 
//ex2)
// num1/num2을 실수형 f에 정수형이 저장됨
int num1=10;
int num2=8;
 
float f;
f=num1/num2; // 1
f=(float)num1/num2; // 1.25 
 
int n1,n2;
n1=7;
n2=2;
 
int result;
 
result=n1+n2;
System.out.printf("%d+%d=%d",n1,n2,result);
cs

자동 형변환 : 작은 범위에서 큰 범위로 자료형이 바뀔 때 자동으로 자료형을 변환

강제 형변환 : 큰 범위에서 작은 범위로 자료형이 바뀔 때 변환할 자료형을 지정하여 변환

 

 

Wrapper class

- 대표적인 Wrapper Class

일반 자료형 Wrapper Class
int Integer
char Charater
char [] String

나머지 대부분은 일반 자료형 앞에 대문자

클래스명 변수 = new 클래스명() = 클래스명 변수 = 값
cs

 

        int i=123; 
        Integer iobj=123; //wrapper class
        Integer iobj2=new Integer(123);
        System.out.println(i);
        System.out.println(iobj2);
        
        // 문자열 -> 숫자
        String str1="123";
        int num1=Integer.parseInt(str1);
        
        // 숫자 -> 문자열
        Integer num2=12345;
        String str2=num2.toString();
        str2=num2+""; //더 간편한 방법 
        
        
        int num3=67;
        // 10진수 -> 2진수(문자열)
        String str3=Integer.toBinaryString(num3);
        System.out.println(str3); //0100 0011
        
        // 10진수 -> 16진수(문자열)
        String str4=Integer.toHexString(num3);
        System.out.println(str4); // 43
        
        
        // 2진수(문자열) -> 10진수
        int str5=Integer.parseInt(str3,2); //str3=0100 0011
        System.out.println(str5); // 67
cs
 
변수의 스코프

1. 클래스 영역에서 선언한 변수는 static 타입이 아니면 객체화를 하여야 불러올 수 있다.

2. 지역 변수는 해당 메서드 안에서만 사용할 수 있다.

3. static 으로 선언한 변수는  클래스 내 어디에서든 사용할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class test {
    int a;
    static int c;
    public void b() {
        int b;
    }
    public static void main(String[] args) {
        System.out.println(b); // x
        System.out.println(a); // x
        test s= new test(); // 객체화
        System.out.println(s.a); // o
        System.out.println(c); // o
    }
 
}
Colored by Color Scripter
cs

 

this 와 super

this : 클래스 영역에서 정의한 변수와 동일한 변수가 사용될 때 클래스 영역 내에 정의한 변수를 사용할 수 있게 함

1
2
3
4
5
6
7
8
9
10
11
12
public class test {
    int val=10;
    public test(int val) {
        System.out.println(this.val);
    }
    public static void main(String[] args) {
    test t =new test(100); // 10 return
    
    }
 
}
 
Colored by Color Scripter
cs

 

super : 이미 클래스 내에 동일한 이름의 메서드,변수가 있을 때 상속받은 클래스의 메서드나 변수를 사용할 수 있게 

1
2
3
4
5
6
7
8
9
10
11
12
public class Child  extends Parent{
    String name="Child";
    public void call() {
        System.out.println(super.name);
    }
    public static void main(String[] args) {
        Child c =new Child();
        c.call(); // Parent return
    }
 
}
 
Colored by Color Scripter
cs

 

 

간단한 Math 함수
(Math.random()*end-start+1) + start 랜덤 함수
Math.power(value,지수) 거듭제곱 함수
Math.sqrt(value) 루트 함수

 

변수의 종류 

 

지역 변수 :  클래스 이외의 영역에 선언한 변수, 선언문이 실행 될 때 생성

클래스 변수 : 클래스 영역에서 선언한 변수, 메모리에 올라갈 때 생성, 모든 클래스  전역 변수

인스턴스 변수 :  클래스 영역에서 선언한 변수, 객체를 선언할 때 생성, 같은 클래스 내 전역 변수

public class test {
 
int instance_value; // 인스턴스 변수 = 객체 변수
static int static_value; // 클래스 변수 = 스태틱 변수
 
void method() {
int local_value; // 지역 변수
}
 
public static void main(String[] args) {
 
}
 
}
Colored by Color Scripter
cs

 

- 변수 주의사항
  1. 클래스 설계 시 , 모든 객체가 공통으로 사용하는 것은 static으로 선언한다.
  2. static 메서드는 인스턴스 변수를 사용할 수 없다. (static은 객체 선언없이 호출 가능하므로 사용을 금지함)

 

 

'멀티잇 풀스택' 카테고리의 다른 글

7 week_2 XML, JSON  (0) 2023.02.01
7 week_1 JavaScript  (0) 2023.01.30
6 week html & css  (0) 2023.01.30
4,5 week 알고리즘  (0) 2023.01.09
3 week MYSQL  (0) 2023.01.03
    '멀티잇 풀스택' 카테고리의 다른 글
    • 7 week_1 JavaScript
    • 6 week html & css
    • 4,5 week 알고리즘
    • 3 week MYSQL
    문준영
    문준영
    공부한 내용 정리!

    티스토리툴바