728x90

equalsIgnoreCase()

  • equalsIgnoreCase()는 대소문자를 고려하지 않고 문자열을 비교.
  • 두 문자열의 대소문자를 무시하고 동일한 문자를 가지고 있으면 true 반환.

 

equals() vs equalsIgnoreCase() 차이점

  • equals()는 대소문자를 비교한다.
  • equalsIgnoreCase()는 대소문자를 비교하지 않는다.
public class Main {

    public static void main(String args[]) {
    	
    	String str1 = "hello";
    	String str2 = "HELLO";
    	
    	System.out.println(str1.equalsIgnoreCase(str2)); // true
    	System.out.println(str1.equals(str2));		 // false
    	
    }
}

 

 

728x90

'Java & Spring' 카테고리의 다른 글

[JAVA] substring() 사용법  (0) 2022.03.11
[JAVA] Integer.parseInt() 사용법  (0) 2022.03.10
[JAVA] Character.isAlphabetic() 사용법  (0) 2022.03.08
[JAVA] Array, ArrayList 차이점  (0) 2022.03.07
[JAVA] charAt() 사용법  (0) 2022.03.06
728x90

Character.isAlphabetic()

  • isAlphabetic()은 명시된 char 값이 문자 인지 여부를 판단하여 true/false를 리턴
  • 문자(char)의 경우 Character.isAlphabetic()을 활용

 

public class Main {
	
    public static void main(String args[]) {
   	
    	System.out.println(Character.isAlphabetic('a'));	// true
    	System.out.println(Character.isAlphabetic('!'));	// flase
    	System.out.println(Character.isAlphabetic('ㄱ'));	// true
    	System.out.println(Character.isAlphabetic('가'));	// true
    	System.out.println(Character.isAlphabetic('7'));	// false
    	
    }
}

 

 

 

 

728x90
728x90

Array

  • 배열의 길이는 고정되어 변경 불가
  • 선언할 때 별도의 초기화가 없다면 배열의 크기만큼 기본값이 채워진다
  • 물리 주소와 논리 주소가 동일 (index로 해당 원소 접근 가능)
  • 연속된 메모리의 공간으로 이루어져 있다

 

public class Main {
    public static void main(String []args) {
        int [] arr  = new int[5];
        arr[0] = 1;
        arr[1] = 2;
        arr[2] = 3;
        arr[3] = 4;
        arr[4] = 5;

        for(int i=0; i<arr.length; i++) {
            System.out.print(arr[i] + " "); // 1 2 3 4 5
        }
    }        
}

 

ArrayList

  • ArrayList는 순서가 있는 엘리먼트의 모임으로 배열과는 다르게 빈 엘리먼트는 절대 허용하지 않는다.
  • ArrayList는 배열이 가지고 있는 index라는 장점을 버리고 대신 빈틈없는 데이터의 적재 라는 장점을 취함
  • ArrayList에서 index는 몇 번째 순서의 의미를 가진다. (배열-Array에서의 인덱스는 값에 대한 유일무이한 식별자)
  • ArrayList는 크기가 가변적이다.
  • ArrayList는 빈 공간을 허용하지 않기 때문에 메모리의 낭비가 없다.
  • 포인터를 통하여 다음 데이터의 위치를 가르켜고 있어 삽입 삭제의 용이.

 

public class Main {
    public static void main(String []args) {
        List <Integer> arrList = new ArrayList<>();
        
        arrList.add(1);  //add 데이터 입력
        arrList.add(3);
        arrList.add(5);
        arrList.add(7);

        arrList.set(0, 100); //set 데이터 수정
        arrList.set(1, 200);
        arrList.remove(3); //remove 데이터 삭제

        for(int i=0; i<arrList.size(); i++) {
            System.out.print(arrList.get(i) + " "); // 100 200 5
     	}
    }
}

 

 

 

728x90
728x90

charAt()

String 타입의 데이터(문자열)에서 특정 문자를 char 타입으로 변환할 때 사용하는 함수.

숫자로 구성된 String 변수에서 특정 숫자를 int 변수로 가져올 수 있다.

 

charAt() 예제

String str = "Hello World"; 

System.out.println(str.charAt(0)); // H
System.out.println(str.charAt(1)); // e
System.out.println(str.charAt(2)); // l
System.out.println(str.charAt(3)); // l
System.out.println(str.charAt(4)); // o

String num = "12345";

int num1 = num.charAt(0); // 49 -- 문자 '1'의 아스키코드값인 49가 나온다.
// 그러므로 '0' 또는 '0'의 아스키코드값 48을 빼준다.
int num2 = num.charAt(0) - '0'; // 1
int num3 = num.charAt(0) - 48; // 1
728x90
728x90

정수의 최대값, 최소값

 

  • static int Integer.MAX_VALUE
  • static int Integer.MIN_VALUE

32비트에서 Int 정수 범위는 -2,147,483,648 ~ 2,147,483,647 이다.

정수의 최대값은 231-1(2,147,483,647)

정수의 최소값은 -231(-2,147,483,648)

64비트에서도 32비트와 마찬가지로 4byte라서 동일

 

public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE); // 2147483647
        System.out.println(Integer.MIN_VALUE); // -2147483648
    }
}

 

Integer 클래스 외 Long, Short 등의 클래스도 같은 메소드를 제공

728x90
728x90

.toString()

  • null 값을 형 변환 시 NullPointerException(NPE)이 발생
  • Object의 값이 String이 아니여도 출력

 

String.valueOf()

  • 어떠한 값이라도 String 문자열로 변환
  • 파라미터로 null이 오면 "null"이라는 문자열을 출력

 

.toString() / String.valueOf() 차이점 비교

public class Main {

    public static void main(String args[]) {

        Object obj = null; 
        
        System.out.println(obj.toString()); // NullPointerException 
        System.out.println(String.valueOf(obj)); // ""null"

    }
    
}
728x90
728x90

forEach

배열과 컬렉션에 저장된 요소에 접근하기 할 때 편리한 방법으로 처리할 수 있도록 for문의 새로운 문법 추가

for (type var : iterate) {
    body-of-loop
}

 

forEach 예제

String[] str = {"aa", "bb", "cc"};
for (String el : str) {
    System.out.print(el + " ");
}
// aa bb cc

int[] arr = {1, 2, 3, 4, 5};
for(int x : arr){	
	System.out.print(x + " ");
}
// 1 2 3 4 5
728x90
728x90

replace()

  • String replace(CharSequence target, CharSequence replacement)
  • replace() 함수는 대상 문자열을 원하는 문자값으로 변환하는 함수
  • 첫번째 매개변수는 변환하고자 하는 대상이 될 문자열
  • 두번째 매개변수는 변환할 문자 값

 

replace() 예제

public class Main {

    public static void main(String args[]) {
    
        String st = "12345";
        System.out.println(st.replace("12", "A")); // A345
        
    }
}

 

replaceAll()

  • String replaceAll(String regex, String replacement)
  • replaceAll() 함수는 대상 문자열을 원하는 문자값으로 변환하는 함수
  • 첫번째 매개변수는 변환하고자 하는 대상이 될 문자열
  • 두번째 매개변수는 변환할 문자 값

 

replaceAll() 예제

public class Main {

    public static void main(String args[]) {
        
        String st = "12345";
        System.out.println(st.replaceAll("12", "A")); // A345

    }
}

 

replace() 와 replaceAll() 차이점

위 예제들로 보면 replace()와 replaceAll()의 결과는 같다.

차이점은 인자 값의 형태를 보면 CharSequence와 String 이라는 차이점을 볼 수 있다.

그리고 replaceAll() 의 설명을 보면 regex 이것은 "정규 표현식" 을 의미

 

replaceAll()은 정규표현식 사용이 가능.


replace() 와 replaceAll() 차이점 예제

public class Main {

    public static void main(String args[]) {
        
        String st = "12345";
        System.out.println(st.replace("12", "A")); // A345
        System.out.println(st.replaceAll("[0-9]", "A")); // AAAAA
     
    }
}

 

 

728x90

+ Recent posts