Java

Java

배열과 컬렉션즈 프레임워크 (ArrayList)

배열과 컬렉션즈 프레임워크 배열에는 몇가지 불편한 점이 있다. 한번 정해진 배열의 크기를 변경할 수 없다는 점 이러한 불편함을 컬렉션즈 프래임워크를 사용하면 줄어든다. import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { String[] arrayObj = new String[2]; arrayObj[0] = "one"; arrayObj[1] = "two"; // arrayObj[2] = "three"; 오류가 발생한다. for(int i=0; i

Java

제네릭

클래스 내부에서 사용할 데이터 타입을 외부에서 지정하는 기법 (매개 변수처럼 동작) ex 1) class Person{ public T info; } public class GenericDemo { public static void main(String[] args) { Person p1 = new Person(); Person p2 = new Person(); } } 데이터 타입 p1.info : String p2.info : StringBuilder T는 아래 코드의 안에 지정된 데이터 타입에 의해서 결정 클래스를 정의 할 때는 info의 데이터 타입을 확정하지 않고 인스턴스를 생성할 때 데이터 타입을 지정하는 기능이 제네릭이다. 제네릭을 사용하는 이유 class StudentInfo{ public ..

Java

참조

복제 ex 1) public class ReferenceDemo1 { public static void runValue(){ int a = 1; int b = a; b = 2; System.out.println("runValue, "+a); // 실행 결과 : runValue, 1 } public static void main(String[] args) { runValue(); } } 복제 : a의값을 b라는 박스에 넣는다 참조 (reference) ex 1) class A{ public int id; A(int id){ this.id = id; } } public class ReferenceDemo1 { public static void runValue(){ // 복제 int a = 1; int b =..

Java

상수와 enum

상수 변하지 않는 값 ex 1) public class ConstantDemo { public static void main(String[] args) { /* * 1. 사과 * 2. 복숭아 * 3. 바나나 */ int type = 1; switch(type){ case 1: System.out.println(57); break; case 2: System.out.println(34); break; case 3: System.out.println(93); break; } } } 변수도 상수가 될 수 있다. 변수를 지정하고 그 변수를 final로 처리하면 한번 설정된 변수의 값은 더 이상 바뀌지 않는다 바뀌지 않는 값이라면 인스턴스 변수가 아니라 클래스 변수(static)로 지정하는 것이 더 좋을 것이다 ..

Java

Object

자바에서 모든 클래스는 사실 Object를 암시적으로 상속받고 있는 것이다 Object는 모든 클래스의 조상이라고 할 수 있다 이유 : 모든 클래스가 공통으로 포함하고 있어야 하는 기능을 제공하기 위해서다. object 메소드 toString 객체를 문자로 표현하는 메소드 ex 1 ) class Calculator{ int left, right; public void setOprands(int left, int right){ this.left = left; this.right = right; } public void sum(){ System.out.println(this.left+this.right); } public void avg(){ System.out.println((this.left+this.r..

Sun-Koo
'Java' 카테고리의 글 목록 (2 Page)