1. 내부클래스
- 클래스속에 있는 클래스
- 컴파일하면 (외부클래스명$내부클래스명.class)
--------------------------------------------------------
class Outer
{
private int num;
public Outer(int a)
{
num = a;
}
class Inner // 내부클래스
{
private int s;
public int sum(int a)
{
s = num+a;
return s;
}
}
}
--------------------------------------------------------
class Testout
{
public static void main(String[] args)
{
Outer out = new Outer(100); // 외부클래스 인스턴스 생성
Outer.Inner inner1 = out.new Inner(); // 내부클래스 인트턴스 생성
Outer.Inner inner2 = out.new Inner();
System.out.println(inner1.sum(10));
System.out.println(inner2.sum(20));
}
}
----------------------------------------------------------------------------------------
2. String클래스
class Strclass
{
public static void main(String[] args)
{
int strlen;
String s1 = "Java ";
String s2 = "Programming !!";
String s3 = s1.concat(s2); //s1+s2
System.out.println(s3);
String s4 = s3.substring(5); // s3 의 5번째글자부터 끝까지
System.out.println(s4);
String s5 = s3.substring(2,8); // s3 의 2번째부터 8번째까지
System.out.println(s5);
String s6 = s3.toUpperCase(); // 대문자로 변환
System.out.println(s6);
String s7 = s3.toLowerCase(); // 소문자로 변환
System.out.println(s7);
strlen = s3.length();
System.out.println("s3문자열의 길이는 "+strlen+"입니다.");
String s8 = s3.replace('r','o'); // 문자열에서 r을 o로 치환
System.out.println(s8);
char s9 = s3.charAt(5); //return값이 char이므로 char변수로 받아야 함.
// 문자열중에서 5번째 글자를 반환
System.out.println(s9);
}
}
---------------------------------------------------------------------------
6. StringBuffer 클래스
class Strbuff
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer(); // 버퍼 인스턴스변수 생성
sb.append("java");
sb.append("Programming !!"); // 버퍼의 뒤에 문자열 덧붙임.
String s1 = sb.toString(); // 버퍼를 문자로 변환 후 문자열변수에 지정
System.out.println(s1);
sb.reverse(); // 버퍼안의 문자열의 인덱스를 뒤집음. 뒤의 값이 앞으로...
System.out.println(sb.toString());
sb.reverse();
sb.delete(2,5); // 버퍼안의 문자열에서 2번지~5번지까지 지움
System.out.println(sb.toString());
sb.insert(3,'a'); // 버퍼안의 문자열의 4번째 번지에 'a'추가
System.out.println(sb.toString());
}
}
----------------------------------------------------------------------------------------
7. StringTokenizer / Calendar / Random
import java.util.*;
class Test
{
public static void main(String[] args)
{
/////////// 토큰 /////////////////////////////
StringTokenizer st = new StringTokenizer("aaa-bbb-ccc-ddd","-");
System.out.println("토큰수"+st.countTokens());
for(int i=0; i<=st.countTokens()+1; i++)
{
System.out.println(st.nextToken());
}
//////////// Calendar ///////////////////////
Calendar now = Calendar.getInstance();
int h = now.get(Calendar.HOUR); // 시간
int m = now.get(Calendar.MINUTE); // 분
int s = now.get(Calendar.SECOND); // 초
int ampm = now.get(Calendar.AM_PM); // AM_PM
if(ampm==Calendar.AM)
System.out.print("am ");
System.out.print("pm ");
if(h==0)
h=12;
System.out.println(h+":"+m+":"+s);
/////////// Random /////////////////////////
Random r= new Random();
int i = r.nextInt(50);
System.out.println(i);
float f = r.nextFloat();
System.out.println(f);
double d = r.nextDouble();
System.out.println(d);
}
'JAVA/JSP > Java' 카테고리의 다른 글
Choice/Checkbox/List/TextArea //Event (0) | 2008.07.14 |
---|---|
ActionListener/ WindowListener/ Dialog (1) | 2008.07.14 |
Choice/ List / TextField / TextArea ,Layout, Menu (0) | 2008.07.14 |
Vector,Hashtable,AWT(Label,Checkbox,Button)| (0) | 2008.07.14 |
Abstract와 Interface의 비교 (0) | 2008.07.14 |