FIF's 코딩팩토리

자바 기초 NCS교육과정(31)-Wrapper 본문

Back-End/국비 NCS교과과정

자바 기초 NCS교육과정(31)-Wrapper

FIF 2019. 7. 31. 17:21
반응형
Wrapper

1 기본자료형을 클래스로 정의해 놓은 것을  Wrapper라 한다
    char -> Character
    byte -> Byte
    short -> Short
    int -> Integer
    long  -> Long
    float  -> Float
    double -> Double
    boolean -> Boolean
  
  2 메모리에 저장된 값을 다른 자료형으로 변환해주는 메소드를 제공하기 위해서
    Wrapper클래스를 제공한다.
   
  3 auto boxing과 auto unboxing은 jdk5.0에서 추가된 기능이다.
    auto boxing => 스택 -> 힙영역에 복사
    auto unboxing => 힙 -> 스택영역에 복사   

 

 

Java142_Wrapper.java

public class Java142_Wrapper {

	public static void main(String[] args) {
		String data="1234";
		
		//String -> Integer
		Integer it = new Integer(data);
		
		//Integer -> int
		int num=it.intValue();
		System.out.println("num="+num);
		
		//Integer -> double
		double pro = it.doubleValue();
		System.out.println("pro="+pro);
		
		//Integer -> long
		long nn=it.longValue();
		System.out.println("nn="+nn);
		
		//String -> Float
		Float ft= new Float(data);
		System.out.println("ft="+ft);
		
		//Float -> int
		int num1=ft.intValue();
		System.out.println("num1="+num1);
		
		//Float -> double
		double da = ft.doubleValue();
		System.out.println("da="+da);
		
		int x=10; //stack
		
		//Integer ig=new Integer(x);//heap
		Integer ie=x;//auto boxing(stack->heap)
		System.out.println("ie="+ie);
		
		//int y=ie.intValue();
		int y=ie;//auto unboxing(heap->stack)
		System.out.println("y="+y);
		
		double z=ie;//auto unboxing(heap->stack)
		System.out.println("z="+z);
		
		

	}

}

출력결과

 

Java143_Wrapper.java

public class Java143_Wrapper {

	public static void main(String[] args) {
		int x = 10;
		double y=5.8;
		char[] data= {'a','b','c'};
		
		//String <- 기본데이터
		String a=String.valueOf(x);
		String b=String.valueOf(y);
		String c=String.valueOf(data);
		
		System.out.printf("%s %s %s\n",a,b,c);
		
		//기본데이터<-String
		/*String input="1234";
		Integer it=new Integer(input);
		int num=it.intValue();*/
		
		//int <- String
		int k=Integer.parseInt("1234");
		
		//double <- String
		double m = Double.parseDouble("1234");
		
		//float <-String
		float n=Float.parseFloat("1234");
		System.out.printf("%d %.1f %.1f\n",k,m,n);

	}

}

출력결과

 

Java144_Wrapper.java

public class Java144_Wrapper {

	public static void main(String[] args) {
		char data='a';
		if (data>='A'&&data<='Z')
			System.out.println("대문자");
		else
			System.out.println("소문자");
		
		//대문자 검색
		System.out.println(Character.isUpperCase(data));
		//소문자 검색
		System.out.println(Character.isUpperCase(data));
		
		//숫자검색
		System.out.println(Character.isDigit(data));
		//알파벳 검색
		System.out.println(Character.isAlphabetic(data));
		//대문자
		System.out.println(Character.toUpperCase(data));
		//소문자
		System.out.println(Character.toLowerCase(data));
		
		

	}

}

출력결과

 

Java145_Wrapper.java

public class Java145_Wrapper {

	public static void main(String[] args) {
		String sn = "korea12 paran3";
		int cnt = 0;
		for (int i = 0; i < sn.length(); i++) {
			/*char data = sn.charAt(i);
			if (data >= '0' && data <= '9')
				cnt++;*/
			if(Character.isDigit(sn.charAt(i)))
				cnt++;
		}
		System.out.println("숫자갯수:" + cnt);
	}

}

출력결과

 

Java146_Wrapper.java 

 

이름, 학점을 입력하는 프로그램을 구현하시오
  입력: 홍길동,80,93


  [출력결과]
  이름: 홍길동
  국어: 80
  영어: 93
  평균: 86.5(double로 계산)

public class Java146_Wrapper {

	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		System.out.print("입력:");
		String name=sc.next();
		int korean = sc.nextInt();
		int english = sc.nextInt();
		double avg = (korean+english)/2.0;
		System.out.printf("이름: %s\n국어: %d\n영어: %d\n평균: %.1f",name,korean,english,avg);
	}
}

출력결과

 

반응형
Comments