자바 기초문법 S1

‍정진철·2022년 6월 27일
0

자바 문법

목록 보기
1/2

1. 1bit x 8 = 1 byte


2.정수형


3. 자동형변환

double a = 3.0F;
--> float형 데이터를 double로 지정해주면 자동으로 3.0F를 double로 인식하게 된다. becase double타입은 float타입보다 범위가 크기 때문.

but,

float a = 3.0;
--
> 3.0은 double 타입이므로 float형으로 형 변환될 수 없다. because, float는 double의 범위를 담아낼 수 없다.

point: 그릇이 작은 데이터타입은 보다 큰 그릇의 데이터타입을 담아낼 수 없다.

3-1 명시적 형변환

float a = 100.0;
int b = 100.0F
위 두가지 사례는 작은그릇안에 큰 그릇을 담으려해 오류가 나는 사례다.

따라서 위와 같이 적용시키고 싶은 경우,
float a = (float)100.0;
int b = (int)100.0F;
와 같이 괄호안에 데이터타입을 명시해준다.


4. 형변환

int a = 10;
float b = 3.0F;

System.out.println(a/b); 를 수행하면 float가 int보다 더 조밀하고 범위가 넓은 타입이므로 Java는 int a 를 10.0으로 치환시켜 10.0/3.0 즉, 3.33333... 의 값을 출력하게 한다.


5. 조건문: switch

public class SwitchDemo {
	public static void main(String[] args) {
		switch(1){
		case 1:
			System.out.println("one");
		
		case 2:
			System.out.println("two");
		
		case 3:
			System.out.println("three");
		
		default:
			System.out.println("default");
		}
	}
}

switch( )에서 괄호안은 여러 case 들 중 하나의 조건을 만조시키는 케이스를 의미한다. 즉, 괄호안의 조건이 만족되는 case에 맞게 출력을 하는것이다.
default 는 만약 switch(4) 처럼 case 에 없는 조건이 걸린 경우 기본적으로 세팅된 default값을 출력하라는 의미다.


6. 반복문


while문의 괄호안의 조건은 "언제까지" 수행해라 라는 의미.

7. 반복문 : for

package test;

public class javatest {
	public  static void main(String[] args) {
		for (int i =0; i<=10; i++) {
			System.out.println("Coding Everyday " + i );
		}
	}

}

for (초기화 , 종료조건, 반복실행) {
반복적으로 실행될 구문
}

  • for문 괄호안에 쉼표없이 코드

8. 반복문: continue

public class javatest {
	public  static void main(String[] args) {
		for (int i =0; i<=10; i++) {
			if(i==5)
				continue;
			System.out.println("Coding Everyday " + i );
		}
	}

}

continue 를 만나게되면 우선 작업이 중지됨.
따라서 print 작업을 실행할 수 없음.
하지만 반복문이 끝난건 아니므로 이어서 다음 조건의 작업을 수행함.


9. 배열: 제어

package test;

public class javatest {
	public  static void main(String[] args) {
		
		//classGroup = {"홍길동", "철수", "영희", "영자"} ;
		String[] classGroup = new String[4];
        --> 새로운 문자열(배열)은 4개의 원소로 이루어짐.
		
		classGroup[0] = "홍길동";
		System.out.println(classGroup.length);
		classGroup[1] = "철수";
		System.out.println(classGroup.length);
		classGroup[2] = "영희";
		System.out.println(classGroup.length);
		classGroup[3] = "영자";
		System.out.println(classGroup.length);
		
	}
}

출력값: 4 4 4 4
자바의 length 메소드는 현재 몇개의 원소를 담고 있는지에 대한 정보가 아닌
객체가 "담을 수 있는" 총원소의 양을 출력시킨다.


10. 반복문과 배열의 조화

package test;

public class javatest {
	public  static void main(String[] args) {
		
		
		String[] members = {"영숙","희숙","말숙"};
		
		for(int i=0; i<members.length; i++) {
        ->세미콜론 잊지말기
        
			String member = members[i];
		
		System.out.println(member + " 이(가) 상담받았습니다.");
			
		}
	  }
	}

11. 배열: for-each

package test;

public class javatest {
	public  static void main(String[] args) {
		String[] members = {"철수", "해수", "형수" } ;
		for (String e: members) {
     --> 콜론 뒤에 있는 데이터의 값들을 반복문이 돌 때 마다 
     데이터타입이 Stirng인 변수'e'에 저장시킴.
     
			System.out.println(e +  " 가 상담을 받았습니다");
		}
	  }
	}

12. 메소드(입력값)

package test;

public class javatest {
	public  static void numbering(int init, int limit) {
		int i = init;
		while(i< limit) {
			System.out.println(i);
			i++;
		}
	}

public static void main(String[] args) {
		numbering(2,6);
 	}
 }

13. 메소드(출력값)

public class javatest {
	public  static String numbering(int init, int limit) {
		int i = init;
		String output = "";
		while(i< limit) {
			output += i;
			i++;
		
	}
		return output; 
}

//void => 메소드의 리턴값이 존재하지 않는다.
public static void main(String[] args) {
	//numbering메소드의 타입이 string이므로 리턴값을 받는 'result'의 타입도 string으로 지정해야함.	
	String result = numbering(1,5);
	
	System.out.println(result);
	}
 }
  • 메소드를 굳이 return 하는 이유는 (출력도 비슷하지 않은가?) 일종의 부품으로서의 가치를 높이기 위해서다. 무슨말이냐 메소드를 하나의 부품으로서 가정을 할 때 단순히 출력의 기능만 존재한다면 부품으로서의 기록,저장 등등 다양한 기능은 하지 못하고 오로지 출력만의 기능만 할 수 있기 때문이다.

profile
WILL is ALL

0개의 댓글