Search Results for '분류 전체보기'

231 POSTS

  1. 2015.04.10 Equals_Ex2
  2. 2015.04.10 Equals
  3. 2015.04.10 StringBuffer_Ex2
  4. 2015.04.10 StringBuffer
  5. 2015.04.10 innerClass_Ex4
  6. 2015.04.10 innerClass_Ex3
  7. 2015.04.10 innerClass_Ex2
  8. 2015.04.10 innerClass
  9. 2015.04.10 enum
  10. 2015.04.10 Collection_EX

Equals_Ex2

Posted 2015. 4. 10. 15:54

package chap9;

class Value2{
 int value;
 Value2(int value){
  this.value = value;
 }
 
 // 객체의 내용을 비교해서 같으면 true
 // 다르면 false 리턴하는 메서드로 오버라이딩
 @Override
 public boolean equals( Object obj ){
  if( obj instanceof Value2 ){
   Value2 v = (Value2)obj;
   if( value == v.value ) return true;
   else return false;
  }
  else return false;
 }
}

public class Equalsx2 {
 public static void main(String[] args) {
  Value2 v1 = new Value2(10);
  Value2 v2 = new Value2(10);
  
  if( v1 == v2 ) System.out.println("v1객체와 v2객체는 같습니다.");
  else System.out.println("v1객체와 v2객체는 다릅니다.");

--> 객체를 비교하면 다르다가 출력
  
  if( v1.equals(v2) ) System.out.println("v1.equals(v2) : true");
  else System.out.println("v1.equals(v2) : false");

--> 객체들의 Obj를 비교하면 같다가 출력됨. 둘다 Value2를 사용했기때문
  
  System.out.println(v1.hashCode());
  System.out.println(System.identityHashCode(v1));
 }
}

'Java !!!' 카테고리의 다른 글

String  (0) 2015.04.10
HashCode  (0) 2015.04.10
Equals  (0) 2015.04.10
StringBuffer_Ex2  (0) 2015.04.10
StringBuffer  (0) 2015.04.10

Equals

Posted 2015. 4. 10. 15:54

package chap9;

// Equels 메서드 연습

class Value{
 int value;
 Value(int value){
  this.value = value;
 }
}

// Object 클래스의 equals 메서드는 객체비교방식 구현
// 객체비교는 == 연산자로도 가능
// equals 메서드는 내용비교하는 방식으로 오버라이딩 필요
// 대부분의 클래스는 equals 메서드를 내용비교 방식으로
// 오버라이딩해서 사용하고 있다. ( string )

public class Equalsx1 {
  
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Value v1 = new Value(10);
  Value v2 = new Value(10);
  
  if( v1 == v2 ) System.out.println("v1객체와 v2객체는 같습니다.");
  else System.out.println("v1객체와 v2객체는 다릅니다.");

--> 객체는 v1, v2를 말함. 다르다가 출력됨.
  
  if( v1.equals(v2) ) System.out.println("v1.equals(v2) : true");
  else System.out.println("v1.equals(v2) : false");

--> equals도 마찬가지로 객체를 비교함. 다르다가 출력됨
  
  
  v2 = v1;
  if( v1 == v2 ) System.out.println("v1객체와 v2객체는 같습니다.");
  else System.out.println("v1객체와 v2객체는 다릅니다.");

--> 참조자가 같아 지면서 같다가 출력됨  


  if( v1.equals(v2) ) System.out.println("v1.equals(v2) : true");
  else System.out.println("v1.equals(v2) : false");
 }
}

--> 마찬가지로 같다가 출력됨.

'Java !!!' 카테고리의 다른 글

HashCode  (0) 2015.04.10
Equals_Ex2  (0) 2015.04.10
StringBuffer_Ex2  (0) 2015.04.10
StringBuffer  (0) 2015.04.10
innerClass_Ex4  (0) 2015.04.10

StringBuffer_Ex2

Posted 2015. 4. 10. 15:53

package chap9;

// StringBuffer 예제
// jdk 5.0 이후에 StringBuilder 클래스를 새로 만듬
// StringBuffer와 StringBuilder는 메서드가 같다.

// StringBuffer : 모든 메서드가 스레드에 동기화 되어 있다.
// StringBuilder : 모든 메서드가 스레드에 동기화 되어 있지 않아.

public class StringBufferEx2 {
 public static void main(String[] args) {
  
  long startTime = 0;
  long endTime = 0;
  double dTime = 0d;
  
  long startTime2 = 0;
  long endTime2 = 0;
  double dTime2 = 0d;
  
  startTime = System.nanoTime();
   
  
  StringBuffer sb = new StringBuffer();
  sb.append("abc").append(123).append(true);
  System.out.println(sb); // abc123true
  sb.delete(1, 3);
  System.out.println(sb); // a123true
  sb.deleteCharAt(4);
  System.out.println(sb); // a123rue
  sb.insert(5, "@@");
  System.out.println(sb); // a123r@@ue
  sb.insert(6, 7.89);
  System.out.println(sb); // a123r@7.89@ue
  sb = new StringBuffer("ABCDEFG");
  sb.replace(0, 3, "abc");
  System.out.println(sb);
  sb.reverse();
  System.out.println(sb);

  endTime = System.nanoTime();
  dTime = (double)(endTime - startTime)/(double)1000000;
  
  System.out.println(dTime);
  
  startTime2 = System.nanoTime();
  String str = "abcdefghijklmn";
  String str2 = "1234567890";
  String str3 = str + str2;
  endTime2 = System.nanoTime();
  dTime2 = (double)endTime2 - startTime2 / 1000000;
  System.out.println(str3);
  System.out.println(dTime2);
 }
}

'Java !!!' 카테고리의 다른 글

Equals_Ex2  (0) 2015.04.10
Equals  (0) 2015.04.10
StringBuffer  (0) 2015.04.10
innerClass_Ex4  (0) 2015.04.10
innerClass_Ex3  (0) 2015.04.10

StringBuffer

Posted 2015. 4. 10. 15:53

package chap9;

// StringBuffer 클래스 예제
// 가변적인 문자열 객체임
// 기본적으로 16개의 버퍼를 내부에 가지고 있다.
// new StringBuffer(100) => 116개의 버퍼를 가진다.
// equals 메서드가 오버라이딩 되지 않았다.
// 내용비교는 toString() 메서드로 String 객체로 변경 후 equals 메서드를 사용 해야 한다.

public class StringBufferEx1 {
 public static void main(String[] args) {

  long startTime = 0;
  long endTime = 0;
  double dTime = 0;
  
  
  StringBuffer sb = new StringBuffer("abc");
  StringBuffer sb2 = new StringBuffer("abc");
  
  
  // 시작 시간
  startTime = System.nanoTime();
  
  
  if( sb == sb2 )
   System.out.println("sb == sb2");
  else
   System.out.println("sb != sb2");
  
  if( sb.equals(sb2) )
   System.out.println("sb.equals(sb2) : true");
  else
   System.out.println("sb.equals(sb2) : false");
  
  if( sb.toString().equals(sb2.toString()) )
   System.out.println("sb.toString().equals(sb2.toString()) : true");
  else
   System.out.println("sb.toString().equals(sb2.toString()) : false");
  
  System.out.println();
  
  //끝난 시간
  endTime = System.nanoTime();
  dTime = (double)(endTime - startTime) / 10000000;
  System.out.println(dTime);
 }
}

'Java !!!' 카테고리의 다른 글

Equals  (0) 2015.04.10
StringBuffer_Ex2  (0) 2015.04.10
innerClass_Ex4  (0) 2015.04.10
innerClass_Ex3  (0) 2015.04.10
innerClass_Ex2  (0) 2015.04.10

innerClass_Ex4

Posted 2015. 4. 10. 15:52

package chap10;

class Outer{
 int iv = 10;
 int iv2 = 30;
 class InstanceInner{ // 인스턴스 내부클래스
  int iv = 100;
  void method1(){ // 인스턴스 내부클래스의 멤버 메서드
   System.out.println("iv =" + iv);
   System.out.println("this.iv=" + this.iv); // 100
   // 외부클래스 멤버 접근 방법
   System.out.println("Outer.iv=" + Outer.this.iv); // 10
   System.out.println("iv2=" + iv2); // 30
  }
 }
 
 static class StaticInner{
  int iv = 200;
  static int cv = 300;
 }
 
 void myMethod(){
  class LocalInner{
   int iv = 400;
  }
  LocalInner linner = new LocalInner(); // 반드시 객체 생성을 해야 사용 할 수 있다.
  System.out.println(linner.iv);
 }
}

public class InnerEx4 {
 public static void main(String[] args) {
  Outer oc = new Outer();
  
  // 내부클래스의 타입 : 외부클래스. 내부클래스
  Outer.InstanceInner ii = oc.new InstanceInner();
  System.out.println("ii.iv = " + ii.iv);  // 100
  
  // Static 내부클래스 클래스멤버 접근방법
  System.out.println("Outer.StaticInner.cv :" + Outer.StaticInner.cv); // 300
  
  // Static 내부클래스 인스턴스 멤버 => 객체화 필요
  Outer.StaticInner os = new Outer.StaticInner();
  System.out.println("os.iv = " + os.iv);
  oc.myMethod();
  
  // 인스턴스내부클래스의 메서드 호출
  Outer.InstanceInner ot = oc.new InstanceInner();
  ot.method1(); // 100
  
 }
}

'Java !!!' 카테고리의 다른 글

StringBuffer_Ex2  (0) 2015.04.10
StringBuffer  (0) 2015.04.10
innerClass_Ex3  (0) 2015.04.10
innerClass_Ex2  (0) 2015.04.10
enum  (0) 2015.04.10

innerClass_Ex3

Posted 2015. 4. 10. 15:52

package chap10;

public class InnerEx3 {
 private int outeriv = 0;
 static int outercv = 0;
 class InstanceInner{
  int iiv = outeriv;
  int iiv2 = outercv;
 }
 
 static class StaticInner{
//  int siv = outeriv; // 에러!!
  // outeriv의 객체를 만들어서 사용 해야 한다.
  int siv = new InnerEx3().outeriv;
  static int scv = outercv;
 }
 
 void myMethod(){
  // 내부클래스에서 사용시에는 상수가 되어야 한다.
  int lv = 0;
  final int LV = 0;
  class LocalInner{
   int liv = outeriv;
   int liv2 = outercv;
   // 지역 내부 클래스에서 메서드의 지역변수를 호출하지 못했다.
   int liv3 = lv; // jdk 8.0 이후 가능.
       // lv 변경없으므로 내부적으로 상수로 처리됨.
   int liv4 = LV; // 상수는 접근 가능
  }
 }
 
 public static void main(String[] args) {

 }
}

'Java !!!' 카테고리의 다른 글

StringBuffer  (0) 2015.04.10
innerClass_Ex4  (0) 2015.04.10
innerClass_Ex2  (0) 2015.04.10
enum  (0) 2015.04.10
Collection_EX  (0) 2015.04.10

innerClass_Ex2

Posted 2015. 4. 10. 15:52

package chap10;

public class InnerEx2 {
 class InstanceInner{}
 static class StaticInner{}
 InstanceInner iv = new InstanceInner();
 static StaticInner cv = new StaticInner();
 static void staticMethod(){
  StaticInner obj = new StaticInner();
  // 클래스 멤버에서 인스턴스 내부 클래스의 객체화는 반드시 외부클래스의 객체화 후 가능 하다.
//  InstanceInner obj2 = new InstanceInner(); // 에러!!
  InnerEx2 outer = new InnerEx2();
  InstanceInner obj2 = outer.new InstanceInner();
 }
 
 void instanceMethod(){
  StaticInner obj = new StaticInner();
  InstanceInner obj2 = new InstanceInner();
  // myMethod()의 지역내부클래스는 다른메서드에서 사용 불가
  // LocalInner lv = new LocalInner();
 }
 
 void myMethod(){
  class LocalInner{} // 메서드 내부 클래스다. 밖에서 사용 불가.
  LocalInner iv = new LocalInner();
 }
 
 public static void main(String[] args) {
  
 }
}

'Java !!!' 카테고리의 다른 글

innerClass_Ex4  (0) 2015.04.10
innerClass_Ex3  (0) 2015.04.10
enum  (0) 2015.04.10
Collection_EX  (0) 2015.04.10
Collection _ArrayList  (0) 2015.04.10

innerClass

Posted 2015. 4. 10. 15:51

package chap10;

// 내부클래스 예제
// 1. 클래스 내부에 존재하는 클래스( 클래스 안에 클래스 )
// 2. 참조자료형, 객체화 가능
// 3.멤버( 변수, 메서드, 생성자, Object를 상속 )도 소유가능
// 4. 상속도 가능
// 5. 외부클래스의 멤버임. 외부클래스의 private 멤버에 접근이 가능

public class innerEx1 {
 
 // 인스턴스 내부 클래스 : static 멤버를 가질 수 없다.
 //     단, 상수는 허용
 private int aaaaa = 10;
 public class InstanceInner{
  int iv = 100;
//  static int cv = 10;
  static private final int MAX = 100;
 }
 
 // static 내부 클래스
 static class StaticInner{
  int iv = 200;
  static int cv = 1;
 }
 
 // local inner 지역 내부 클래스 : 메서드 내에서 선언된 클래스
 void myMethod(){
  class LocalInner{
   int iv = 300;
//   static int c = 10; // static 멤버를 가질 수 없다.
   static final int MAX = 100;
  }
  LocalInner l1 = new LocalInner();
  System.out.println(l1.iv);
  System.out.println(LocalInner.MAX);
 }
 
 public static void main(String[] args) {
  innerEx1 in = new innerEx1();
  in.myMethod();
  System.out.println( InstanceInner.MAX );
  System.out.println( StaticInner.cv );
 }
}

 

enum

Posted 2015. 4. 10. 15:51

package chap10;

// 열거형 예제

public class EnumEx1 {
 
 public enum Lesson{
  JAVA, XML, EJB, JSP, SPRING
 }
 
 public static void main(String[] args) {
  Lesson le = Lesson.JAVA;
  System.out.println("Lesson :" + le);
  System.out.println("XML :" + Lesson.XML);
  System.out.println("JSP :" + Lesson.JSP);
  System.out.println("SPRING :" + Lesson.SPRING);
  //le 객체다
  if( le instanceof Object ){
   System.out.println(le.toString());
   System.out.println(le.getClass().getName());
   System.out.println("저장된 정수값:" + le.ordinal());
  }
  Lesson[] lessons = Lesson.values();
  System.out.println("lesson.length =" + lessons.length);
  for( Lesson n : lessons ){
   System.out.println(n + ":" + n.ordinal() );
  }
 }
}

'Java !!!' 카테고리의 다른 글

innerClass_Ex3  (0) 2015.04.10
innerClass_Ex2  (0) 2015.04.10
Collection_EX  (0) 2015.04.10
Collection _ArrayList  (0) 2015.04.10
Collection _Iterator  (0) 2015.04.10

Collection_EX

Posted 2015. 4. 10. 15:51

package chap11;
import java.util.*;
// 제품( Product ) 클래스 만들기
// 멤버 변수 : 제품명( name ), 제품가격( price ), 모델명( modelName )
// 멤버메서드 : toString 메서드만 오버라이딩
// 생성자 : 모든 멤버변수를 입력받도록 구현

// 제품클래스를 set에 저장할 떄 동일한 정보의 객체는 저장되지 않도록 Product 클래스 수정

class Product implements Comparable<Object>{
 String name;
 int price;
 String modelName;
 
 Product( String name, int price, String modelName ){
  this.name = name;
  this.price = price;
  this.modelName = modelName;
 }

 @Override
 public String toString() {
  return "Product [name=" + name + ", price=" + price + ", modelName="
    + modelName + "]";
 }

 @Override
 public int hashCode() {
  return name.hashCode() + price + modelName.hashCode();
 }

 @Override
 public boolean equals(Object obj) {
  if( obj instanceof Product ){
   Product p = (Product)obj;
   if( name.equals(p.name) && ( price == p.price ) && modelName.equals(p.modelName) )
    return true;
   else
    return false;
  }
  
  return false;
 }
 
 // 기본 compareTo 오름차순임. 내림차순은 스왚해야함.
 @Override
 public int compareTo(Object o){
  Product p = (Product)o;
  return name.compareTo(p.name);
 }
}

public class ProductEx {
 public static void main(String[] args) {
  MyTime t = new MyTime();
  
  t.start();
  Product[] items = {
    new Product("TV", 100, "S1000"),
    new Product("TV1", 100, "S2000"),
    new Product("TV2", 100, "S3000"),
    new Product("TV3", 100, "S4000"),
    new Product("컴퓨터", 150, "CS500"),
    new Product("컴퓨터", 150, "CS500"),
    new Product("컴퓨터1", 165, "CS550"),
    new Product("TV", 100, "S4000")
  };
  
  Set<Product> set = new HashSet<Product>();
  for( Product p : items )
   set.add(p);
  for( Product p : set )
   System.out.println(p);
  
  System.out.println();
  System.out.println();
  // set2 조회시 제품명으로 오름차순 정렬되도록
  // product 클래스 프로그램 수정
  TreeSet<Product> set2 = new TreeSet<Product>();
  for( Product p : items )
   set2.add(p);
  
  for( Product p : set2 )
   System.out.println(p);
  t.end();
 }
}

 

'Java !!!' 카테고리의 다른 글

innerClass_Ex2  (0) 2015.04.10
enum  (0) 2015.04.10
Collection _ArrayList  (0) 2015.04.10
Collection _Iterator  (0) 2015.04.10
Collection_ HastSet  (0) 2015.04.10
« PREV : 1 : 2 : 3 : 4 : 5 : 6 : 7 : ··· : 24 : NEXT »