본문 바로가기

자바

Scanner 클래스 예제1) import java.util.Scanner; /* * Scanner * 단락문자의 패턴을 사용하여 입력을 토근에 따라 * 분할하며 기본 토근은 공백이다. * 예외처리를 명시하지 않아도 된다. */public class Test5 {public static void main(String[] args) {String name;int k, e;Scanner sc=new Scanner(System.in); /*System.out.print("이름?");name=sc.next();System.out.print("국어?");k=sc.nextInt();System.out.print("영어?");e=sc.nextInt();*//*// 홍길동 80 60System.out.print("이름 국어 영어(공백으로 .. 더보기
SimpleDateFormat 예제1) import java.text.SimpleDateFormat;import java.util.Date; public class Test3 {public static void main(String[] args) throws Exception {Date dd=new Date();System.out.println(dd);/*// 년, 월, 일(추천되지 않음)int y=dd.getYear()+1900;int m=dd.getMonth()+1;int d=dd.getDate();System.out.println(y+"-"+m+"-"+d);*//* * SimpleDateFormat : Date 객체를 텍스트로 변환하거나 * 텍스트를 Date 객체로 변환 * yyyy-MM-dd HH:mm:ss * h:mm a -.. 더보기
Wrapper 클래스 예제) public class Test1 {public static void main(String[] args) {int a=10;Integer ii;// 기본자료형 -> Wrapper 클래스로 변환ii=new Integer(a);// JDK 5.0부터는 다음과 같이 가능ii=a; // autoboxing(컴파일시 ii=new Integer(a); 로 변환됨)// Wrapper -> 기본자료형으로 변환int b=ii.intValue();// JDK 5.0 부터는 다음과 같이 사용 가능int c=ii; // auto-unboxing(컴파일시 c=ii.intValue(); 로변환)System.out.println(a);System.out.println(b);System.out.println(c);System.. 더보기
BigDecimal와 BigInteger 예제) import java.math.BigDecimal;import java.math.BigInteger; public class Test2 {public static void main(String[] args) {// 아주 큰 정수BigInteger a=new BigInteger("123456789123456789");BigInteger b=new BigInteger("123456789123456789");// 더하기BigInteger c=a.add(b);System.out.println(c);// 빼기c=a.subtract(b);System.out.println(c);// 곱하기c=a.multiply(b);System.out.println(c);// 나누기c=a.divide(b);System.out.. 더보기
StringBuffer 클래스 예제1) public class Test7 {public static void main(String[] args){StringBuffer sb1=new StringBuffer();StringBuffer sb2=new StringBuffer();System.out.println(sb1.capacity()); //초기버퍼크기 : 16sb1.append("seoul");sb2.append("seoul");//주소비교System.out.println(sb1==sb2); //false//StringBuffer에는 equals()메소드가 없으며 최상위 클래스인 Object의 equals()메소이드임//==> 내용을 비교하는 것이 아니라 주소비교System.out.println(sb1.equals(sb2)); //f.. 더보기
string 클래스 예제1) /* * String : 문자열을 처리하는 클래스 * -final 클래스로 하위 클래스를 가질 수 없다. * -불변의 원칙 */ public class Test1 {public static void main(String[] args){// int a=10; //스텍메모리영역에 저장String s1="seoul"; // 문자열은 힙메모리영역에 저장 주소를 갖지 값을 갖지 않는다String s2="seoul"; // 기존(11번줄) soeul의 주소를 가져온다. 결과적으로 seoul 주소 위치는 똑같다.String s3=new String("seoul");// new로 인해 새로운 주소를 할당 받는다11번과 12번의 seoul의 주소와는 다르다System.out.println(s1==s2); // .. 더보기
Method 메서드 예제1) public class Test1 {public static void main(String[] args){ Ex1 oo=new Ex1(); // Ex1 oo; oo=new Ex1; 과 같다 new 다음 생성자.double a=oo.pow(2, 10);// 2,10:실인수pow의 함수 값은 double 이므로 double로 설정System.out.println("2의 10승 : " +a); char aa=oo.upper('b');System.out.println("b:"+aa); System.out.println("b:"+oo.isUpper('b'));System.out.println("A:"+oo.isUpper('A')); int aaa=oo.sum(3,1);System.out.println(".. 더보기
객체지향을 위한 첫걸음 class 클래스 예제1) public class Test1 {public static void main(String[] args) {// 객체생성(인스턴스 생성)Rect ob1=new Rect();Rect ob2=new Rect(); int a,b; ob1.width=10; // 직접 인스턴스변수 적용(속성 변화)ob1.height=5; a=ob1.area(); // 인스턴스를 이용한 변수 적용(속성 변화)b=ob1.len(); ob1.print(a,b); // 메서드 호출(기능 실행) a=ob2.area();b=ob2.len();ob2.print(a,b);}} /*사각형의 넓이와 둘레 계산하는 클래스 작성1. 추상화상태(데이터) : 가로,세로행동(메소드) : 넓이계산,둘레계산,출력2. 클래스작성3. 객체생성*/ cla.. 더보기