import java.awt.Color;import java.awt.Frame;import java.awt.Label;import java.awt.Panel; class LabelTest extends Frame{Panel p;Label label1, label2, label3;public LabelTest(){setTitle("Label Test");p = new Panel();label1 = new Label("개신동");label2 = new Label("모충동");label3 = new Label("수곡동");label1.setBackground(Color.yellow);label2.setBackground(Color.BLUE);label3.setBackground(Color.blue);//Blu..
컴파일할 때 데이터 타입을 확정짓지 않고 실행 시 데이터 타입을 확정짓는 것을 의미다른 언어의 템플릿 기능과 유사 , 즉 미지정 자료형을 사용 컴파일 시에 타입이 결정되어 보다 안전한 프로그래밍 가능개발 시 다운캐스팅(타입 캐스팅) 절차 불필요런타임 타입 충돌 문제 방지 ClassCastException 방지 class Generic {private T data[]; public Generic(T... n) {data = n; // 미확정자료형인 T타입의 n을 인자로 받아 data에 대입} public void disp() {for (T i : data) {System.out.print(i+"\t");}}} public class GenericMain { public static void main(Str..
public class StringFormatTest { public static void main(String[] args) {// TODO Auto-generated method stub String str;str = String.format("%d", 20); System.out.println("str = "+str);//str의 값 세팅, 20의 값을 가진 정수로 출력str = String.format("%10d", 40);System.out.println("str = "+str);//다시 정수형으로 세팅, 10칸의 공간에 오른쪽 정렬//비슷한 기능으로 System.printf() 메소드가 있다. //-> C언어의 printf()와 동일}} str = 20str = 40
public class StringTest { public static void main(String[] args) {// TODO Auto-generated method stub String str1 = "Hello Java";String str2 = "Hello Java";String str3 = new String("Hello Java");String str4 = new String("Hello Java");//같은 리터럴을 사용한 경우에는 문자열이 같다고 나오고//각각 메모리를 할당 받아서 대입하면 서로 다르다고 나온다. (서로 다른 객체)if(str1==str2)System.out.println("두 개의 문자열은 동일하다");elseSystem.out.println("두 개의 문자열은 서로 다..
객체나 인터페이스의 실행 상태를 저장할 수 있는 클래스로 생성자는 숨겨져 있으며 직접 객체를 생성하지 않고 객체의 getClass() 메서드를 이용해서 대입받아서 사용 class First {}class Second extends First {}public class ClassTest { public static void main(String[] args) {// TODO Auto-generated method stub First x = new First();Second y = new Second();Class cyberclass; cyberclass = x.getClass();System.out.println("x의 클래스명 : " + cyberclass.getName());cyberclass = cy..
System 클래스는 실행시간 환경과 관련된 속성과 메소드 제공하는 클래스 모든 멤버가 static이므로 객체 생성없이 사용 생성자는 private으로 숨겨져 있음 System 클래스의 클래스 변수 in과 out은 입출력 패키지의 InputStream 클래스와 PrintStream 클래스의 객체를 지정 public class SystemTest { public static void main(String[] args) {// TODO Auto-generated method stub long start, end;start = System.nanoTime();// 1970년 1월 1일부터 현재까지의 시간을 나노초로 반환int ar[] = new int[10000]; System.out.println("현재 시..
[예제7-15] CastingTest2.java class CastingTest2 { public static void main(String args[]) { Car car = new Car(); Car car2 = null; FireEngine fe = null; car.drive(); fe = (FireEngine)car; // 실행 시 에러가 발생한다. fe.drive(); car2 = fe; car2.drive(); } } [실행결과]drive, Brrrr~ java.lang.ClassCastException: Car at CastingTest2.main(CastingTest2.java:8) 이 예제는 컴파일은 성공하지만, 실행시 에러(ClassCastException)가 발생한다. 에러가 발생한..