equals() 와 hashCode() 는 함께 오버라이드 할 것.

자료형

해시 값 계산 방법

boolean

(value ? 1 : 0)

byte, char short, int

(int) value

float

Float.floatToIneBits(value)

double

Double.doubleToLongBits(value)

 String 및 기타 객체

"test".hashCode() 

package Test;

public class Test {
	int intVal;
	String strVal;

	public Test(int intVal, String strVal) {
		this.intVal = intVal;
		this.strVal = strVal;
	}

	public int getIntVal() {
		return intVal;
	}

	public void setIntVal(int intVal) {
		this.intVal = intVal;
	}

	public String getStrVal() {
		return strVal;
	}

	public void setStrVal(String strVal) {
		this.strVal = strVal;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + intVal;
		result = prime * result + ((strVal == null) ? 0 : strVal.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Test other = (Test) obj;
		if (intVal != other.intVal)
			return false;
		if (strVal == null) {
			if (other.strVal != null)
				return false;
		} else if (!strVal.equals(other.strVal))
			return false;
		return true;
	}

}

이클립스에서는 만드는 방법

단축키 Shift + Alt + S 

Generate hashCode() and equals()... 선택




1. if

구조 

 

if(조건식)

{

//조건식에 만족할 때 수행되는 문장.

}

else if(조건식2)

{

//조건식1이 만족하지 않을 때, 조건식2로 내려와 만족하면 수행되는 문장.

}

else

{

//생략이 가능하며, 위의 어느 조건도 만족하지 않을 때 수행되는 문장.

}

 

 

* equals 메소드

 

String str1 = new String("Hello");

String str2 = new String("Hello");

 

if(str1 == str2)                            // 주소값을 비교. false

if(str1.equals(str2))                    // 변수 안에 내용을 비교하는 메소드. true

if(str1.equalsIgnoreCase(str2))    // 변수안에 대소문자 가리지않고 내용만 확인. true

 

 

2. switch

구조

 

switch(조건식){

case 값1 :

// 조건식의 값이 값1과 같을 경우 실행.

break;    // 모두 실행하고 switch문을 빠져나오기 위함.

case 값2 :

// 조건식의 값이 값2과 같을 경우 실행.

break;

default :

// 위의 조건식에 일치하지 않을 때 실행.

 

※ case문의 값으로 변수를 사용할 수 없다.(리터럴, 상수만 가능)

 

* Math 클래스의 random() : 0.0과 1.0 사이의 범위에 속하는 하나의 double값을 반환하는 메소드.

 

 임의의 문자를 얻을 수 있도록 하는 예제.

0.0 <= Math.random() <1.0

 

0.0 * 26 <= Math.random() * 26 < 1.0 * 26

0.0 <= Math.random() * 26 < 26.0

출력하고자 하는 문자의 갯수 26을 각 변에 곱한다.

 

0.0 * 26 + 65 <= Math.random() * 26 + 65 <1.0 * 26 + 65

65.0 <= Math.random() * 26 + 65 < 91.0

아스키코드 65(A)부터 시작하므로 65를 각 변에 더한다.

 

(char)65.0 <= (char)Math.random() * 26 + 65 < (char)91.0

'A' <= (char)Math.random() * 26 + 65 < '['

문자로 형변환을 한다.

 

 

3. for

구조 

 

for(초기화 ; 조건식 ; 증감식){

// 조건식에 만족할 때 수행되는 문장.

// 순서 : 초기화 → 조건식 → for문 수행 → 증감식 조건식

// for문 안에 초기화에서 변수가 선언되면 for문 안에서만 사용가능하다.

//



4. do-while

구조 

 

do{

// 조건식에 만족할 때 수행되는 문장.

// 최소한 한번은 수행되는 반복문.

}while(조건식)



5. break, continue, Loop

break     : 사용되는 위치에서 가까운 반복문을 빠져나오는데 사용된다.

continue : 반복문의 끝으로 이동하여 다음 반복이 진행된다.

Loop      : 반복문 앞에 이름을 정해주고, break이나 continue에 같이 써줘 반복문을 벗어나거나 반복을 

   건너뛸  수 있게 한다.




+ Recent posts