identity function은 같은 타입의 값 그대로를 리턴하는 메소드이다.


public String f (String value) {

return "value is" + value;

}


의 방식이 아닌 말 그대로 받은 값 그대로 리턴을 해야한다.


public String f (String value) {

return value;

}


그럼 이 방식을 왜 사용하는가?


identity 방식을 사용하면 (return t->t;)

널 포인터에 관한 처리를 해줄 수 있기 때문


ps

Function 인터페이스의 identity()를 사용해도 된다.(자동으로 타입을 추론한다)


ex) 리스트의 숫자에 2를 곱한 값과 그렇지 않은 값을 출력하기


public class Ex1 {

public static void main(String[] args) {

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);


System.out.println("map (number, null)" + map(numbers, i->i*2));

System.out.println("map (number, null)" + map(numbers, null));

}


private static <T, R> List<R> map(List<T> list, Function<T, R> mapper) {

final Function<T, R> function;

if (mapper != null) {

function = mapper;

} else {

function = t -> (R) t;

}

final List<R> result = new ArrayList();

for(final T t: list){

result.add(function.apply(t));

}


return result;


}

}

방식에서 보듯이 null에 관한 처리를 해주고 있지만 identity function을 이용해 처리한다면


public class Ex2 {

public static void main(String[] args) {

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);


System.out.println("map (number, null)" + map(numbers, i->i*2));

System.out.println("map (number, null)" + map(numbers, i->i));

}


private static <T, R> List<R> map(List<T> list, Function<T, R> mapper) {

final List<R> result = new ArrayList();

for(final T t: list){

result.add(mapper.apply(t));

}


return result;


}

}


로 처리가 가능하다.


'Programing > JAVA' 카테고리의 다른 글

JAVA 1.8 정리 (1)  (0) 2016.02.12
JAVA static 선언 try catch  (0) 2016.01.12
Netty 인코더 디코더의 아주 간단한 개념  (0) 2016.01.11
Netty  (0) 2016.01.11
This Keyword  (0) 2015.02.10
Posted by kimmayer

java 8의 funtional programming을 통한 기술,


final List<Integer> number = Array.asList(1, 2, 3);

final String result = number.stream().map(String::valueOf).collect(joining(" , ");


의 경우

integer 였던 number의 리스트가 stream()의 map을 이용해 String value로 바뀌어 저장되고 collect joining 을 통해 ,가 붙여 지는 결과가 만들어진다.


즉 System.out.println(result)의 결과는

1, 2, 3이 된다.


public classExample {

public static void main (String[] args)

{

List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9);

String result = numbers.stream().map(String::valueOf).collect(Collectors.joining(" , "));

//Collectors를 추가하니 에러 없이 모두 정상 처리 되었다.

System.out.println(result);

}

}



'Programing > JAVA' 카테고리의 다른 글

JAVA 1.8 정리 (2)  (0) 2016.02.13
JAVA static 선언 try catch  (0) 2016.01.12
Netty 인코더 디코더의 아주 간단한 개념  (0) 2016.01.11
Netty  (0) 2016.01.11
This Keyword  (0) 2015.02.10
Posted by kimmayer

JAVA 개발 시 static으로 선언하고 싶은 메소드에 try catch 구문으로 감싸야 되는 경우 

static {

method();

}


방식으로 작성하면 static 안에서 try catch 구문을 작성 할 수 있다.

'Programing > JAVA' 카테고리의 다른 글

JAVA 1.8 정리 (2)  (0) 2016.02.13
JAVA 1.8 정리 (1)  (0) 2016.02.12
Netty 인코더 디코더의 아주 간단한 개념  (0) 2016.01.11
Netty  (0) 2016.01.11
This Keyword  (0) 2015.02.10
Posted by kimmayer
네티에서 정의된 여러 클래스들은 갖다 붙이기만 해도 상황에 맞게 알아서 메소드를 호출한다.
(Event Driven:용자의 명령·마우스 클릭·다른 프로그램의 메시지·키보드 글쇠 입력 등의 ‘사건’에 따라, 제어 흐름이 결정되어 일을 하도록 하게끔 만들어진 프로그래밍 언어 방식을 뜻한다.)

때문에 별다른 정의 없이 클래스 객체만 파라미터로 선언해준다면 알아서 처리가 가능하다. 

Netty에서 디코더와 인코더는 받으면 디코더로 풀고 보낼 때 인코딩을 해서 보내는 개념
따라서 받은 문자열을 적당히 디코더로 처리해서 실제 값들을 확인하고 다시 보낼 때 인코딩을 통해 프로토콜 규약에 따라 문자열을 붙이거나 해서 통신을 한다.

인코더와 디코더는 클라이언트와 약속을 해서 어떻게 처리할지 미리 약속을 해야한다.


'Programing > JAVA' 카테고리의 다른 글

JAVA 1.8 정리 (1)  (0) 2016.02.12
JAVA static 선언 try catch  (0) 2016.01.12
Netty  (0) 2016.01.11
This Keyword  (0) 2015.02.10
접근 한정자 (member access)  (0) 2015.02.10
Posted by kimmayer

2016. 1. 11. 22:51 Programing/JAVA

Netty

Netty 기본 설정으로는 Netty.io에 접속하여 Netty JAR파일을 다운로드 받는다.

이클립스에서 외부 JAR(다른 방법도 가능하지만) 추가로 JAR을 추가하면 되는데,


압축을 풀면 생성된 All-inone 을 통하여 Netty 프레임워크를 빌드 패스에 추가하도록 하자.


Netty-Source JAR파일을 통해서 네티 프레임워크의 모든 클래스와 메소드를 확인 할 수 있다.


PS. 상세한 설정 방법은 조만간 업데이트 하도록 하겠습니다...

'Programing > JAVA' 카테고리의 다른 글

JAVA static 선언 try catch  (0) 2016.01.12
Netty 인코더 디코더의 아주 간단한 개념  (0) 2016.01.11
This Keyword  (0) 2015.02.10
접근 한정자 (member access)  (0) 2015.02.10
접근 한정자 예제(객체지향 예제)  (0) 2015.02.10
Posted by kimmayer

2015. 2. 10. 14:17 Programing/JAVA

This Keyword

c++ 에서도 한번 스쳐 지나간 적이 있는 this 키워드,

이번에 자세히 개념이해를 하려고 공부를 해봤는데

생각보다 간단하면서도 이해가 되지 않는다.


책에서 보면 this의 사용은 객체 변수나 생성자, 메소드의 매개 변수의 이름을 의미적으로

정확하게 하기 위해서 사용한다고 쓰여 있다(being java 154p).


이것만으로는 무언가 부족하다!

그런데 예제 하나를 보고 이해했다.


public class Box {

    int length;

    int width;

    int height;

    public Box(int length, int width, int height) {

        this.length = length;

        this.width = width;

        this.height = height;

    }

}


Box 생성자에서 length = length 라고 썼으면 위에서 선언한 length 변수 인지

아니면 생성자에서 사용되는 length 인지 이해하기 힘들 것인데

this를 사용 함으로써 생성자에서 사용되는 length 라고 알려주고 있는것!


단순히 이거 뿐일까?

더 알아보고 추가되는 내용이 있으면 추가 해야겠다.



'Programing > JAVA' 카테고리의 다른 글

Netty 인코더 디코더의 아주 간단한 개념  (0) 2016.01.11
Netty  (0) 2016.01.11
접근 한정자 (member access)  (0) 2015.02.10
접근 한정자 예제(객체지향 예제)  (0) 2015.02.10
인스턴스 복사  (0) 2015.02.10
Posted by kimmayer

public class test1{

  public int a; //public 멤버변수 선언

  int b;  //접근한정자를 지정하지 않고 선언

  private int c; //private 멤버변수 선언



public void method1() {} //public 메소드 선언

void method2() {}  // 접근한정자 지정하지 않음

private void method3 () {} private 메소드 선언

}


public class SamePackage{

Test t1 = new Test1();

t1.a = 3; //접근 가능

t1.b = 5; //접근 가능

t1.c = 7; //접근 불가능

t1.method (); //접근 가능

t2.method (); //접근 가능

t3.method (); //접근 불가능

}



public class OtherPackage{

Test1 t2  = new Test1();

t1.a = 3; //접근 가능

t1.b = 5; //접근 불가능

t1.c = 7; //접근 불가능

t1.method (); //접근 가능

t2.method (); //접근 불가능

t3.method (); //접근 불가능

}



같은 패키지 내에서는 지정되지 않은 지역에서는 접근 가능하나

다른 패키지에서는 지정되지 않은 한정자 들은 접근 불가능으로 나온다.

'Programing > JAVA' 카테고리의 다른 글

Netty  (0) 2016.01.11
This Keyword  (0) 2015.02.10
접근 한정자 예제(객체지향 예제)  (0) 2015.02.10
인스턴스 복사  (0) 2015.02.10
배열과 객체생성 잊지 말아야 할 점!  (0) 2015.02.10
Posted by kimmayer

class Fruit {

 int apple;

 int straw;

 int grapes;

 int sum;

 

 Fruit(int apple, int straw, int grapes){

  this.apple = apple;

  this.straw = straw;

  this.grapes = grapes;

 }

 

 public int Count(){

  sum = apple + straw + grapes;

  return sum;

 }

}



public class MethodDemo1 {

 public static void main(String args[]){

  int total;

  Fruit f1 = new Fruit(30, 30, 40);

  total = f1.Count();

  System.out.println("객체 f1의 총 갯수 = " + total);

  System.out.println("객체 f1의 apple 개수 = "+ f1.apple);

  System.out.println("객체 f1의 straw 개수 = "+ f1.straw);

  System.out.println("객체 f1의 graphs 개수 = "+ f1.grapes);

 }


}


여기까지 예제는 따로 클래스화 시키지 않은 내용들인데

사실 객체지향을 위해서라면 아래 코딩된 예시가 더욱 올바른 것이라고 한다.



class Fruit{

 private int a;

 private int b;

 private int c;

 private int sum;

 Fruit (int apple, int straw, int grapes){

  a = apple;

  b = straw;

  c = grapes;

  this.count();

 }

 

 private void count(){

  sum = a + b + c;

 }

 public int gettotal(){

  return sum;

 }

 public int getapple(){

  return a;

 }

 public int getstraw(){

  return b;

 }

 public int getgrapes(){

  return c;

 }

}


public class MethodDemo2 {

 public static void main(String args[]){

  int total;

  Fruit f1 = new Fruit(30,30,40);

  total = f1.gettotal();

  System.out.println("객체 f1의 총 개수 =" + total);

  System.out.println("객체 f1의 apple 개수 = "+ f1.getapple());

  System.out.println("객체 f1의 straw 개수 = "+ f1.getstraw());

  System.out.println("객체 f1의 graphs 개수 = "+ f1.getgrapes());

 }


}


각각의 값을 return 해 주는 클래스 메소드가 따로 있다.


'Programing > JAVA' 카테고리의 다른 글

This Keyword  (0) 2015.02.10
접근 한정자 (member access)  (0) 2015.02.10
인스턴스 복사  (0) 2015.02.10
배열과 객체생성 잊지 말아야 할 점!  (0) 2015.02.10
Setter와 Getter  (0) 2015.02.10
Posted by kimmayer

Book b = new Book();

Book c = new Book();


Book 레퍼런스 두 개를 성성하고 새로운 객체 Book 두 개를 생성한다.

그리고 생성한 Book 객체를 레퍼런스 변수에 대입한다.


결국 Book b와 c가 Book 객체 1, 2를 참조하고 있다.



Book d = c;


새로운 Book 레퍼런스 d가 생성되고

c와 똑같은 객체를 참조 한다.



c = b;


b와 c는 똑같은 객체를 참조한다.

Posted by kimmayer

class Books{

 String title;

 String author;

}

public class BooksTestDrive {

 public static void main(String args[]){

  Books [] myBooks = new Books[3];

  //myBooks[0]  = new Books();

  //myBooks[1]  = new Books();

  //myBooks[2]  = new Books();

  int x = 0;

  myBooks[0].title = "The Grapes of JAVA";

  myBooks[1].title = "The JAVA of Gatsby";

  myBooks[2].title = "The JAVA Cook Books";

  myBooks[0].author = "bob";

  myBooks[1].author = "sue";

  myBooks[2].author = "ian";

  

  while (x < 3){

   System.out.println(myBooks[x].title);

   System.out.println(" by ");

   System.out.println(myBooks[x].author);

   x = x + 1;

  }

  

 }


}


굵게 글씨쓴 부분이 존재하지 않으면(그니까 주석을 풀지 않는다면) 프로그램 오류가 뜬다.


이유인 즉슨,

 Books [] myBooks = new Books[3]; 이 구문은 단순히 배열만 선언 했다는것!

지금은 Books 배열에 들어있는 원소는 Books 레퍼런스 변수에 불과하다는 점이다!


Books 객체는 따로 만들어 주어야 한다.

'Programing > JAVA' 카테고리의 다른 글

접근 한정자 예제(객체지향 예제)  (0) 2015.02.10
인스턴스 복사  (0) 2015.02.10
Setter와 Getter  (0) 2015.02.10
다형성 배열을 이용하여 객체를 생성!  (0) 2015.02.10
추상 클래스, 추상 메소드의 개념  (0) 2015.02.10
Posted by kimmayer
이전버튼 1 2 이전버튼

블로그 이미지
IT 기술들 정리, 독후감을 주로 남깁니다!
kimmayer

공지사항

Yesterday
Today
Total

달력

 « |  » 2024.4
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함