예외처리
package ch10;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class ExceptionEx5 {
public static void main(String[] args) {
try {
FileReader fr = myRead("aaa.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static FileReader myRead(String name) throws FileNotFoundException {
FileReader fr = new FileReader(name);
return fr;
}
}
package ch10;
public class ExceptionEx6 {
public static void main(String[] args) {
try {
exce3();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void exce1() throws Exception{}
public static void exce2() throws Exception {
exce1();
}
public static void exce3() throws Exception {
exce2();
}
}
package ch10;
public class ExceptionEx7 {
int a=100;
public void m(int b) throws Exception{
if(b==0)
throw new Exception("a는 0으로 나누면 안돼요.");
else
System.out.println(a+" / "+b+" = "+(a/b));
}
public static void main(String[] args) {
ExceptionEx7 et= new ExceptionEx7();
try {
et.m(10);
et.m(0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Object
package ch11;
import java.lang.*;
class Point{}
public class ObjectEx1 {
public static void main(String[] args) {
Point p = new Point();
System.out.println("클래스 이름 : "+p.getClass());
System.out.println("해쉬코드 : "+p.hashCode());
System.out.println("객체문자열 : "+p.toString());
Point p1 = new Point();
System.out.println("클래스 이름 : "+p1.getClass());
System.out.println("해쉬코드 : "+p1.hashCode());
System.out.println("객체문자열 : "+p1.toString());
String s= new String("금요일");
System.out.println("객체문자열 : "+s.toString());
}
}
package ch11;
class Point2 {
int x, y;
public Point2(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
public class ObjectEx2 {
public static void main(String[] args) {
Point2 p = new Point2(10, 20);
System.out.println(p);
}
}
String
package ch11;
class Point3 {
}
public class StringEx1 {
public static void main(String[] args) {
Point3 p1 = new Point3();
Point3 p2 = new Point3();
System.out.println(p1 == p2);
int a = 10;
int b = 10;
System.out.println(a == b);
System.out.println("------------");
String s1 = new String("Java");
String s2 = new String("Java");
String s3 = "Java";
String s4 = "Java";
System.out.println(s1==s2);
System.out.println(s3==s4);
System.out.println(s1==s3);
System.out.println("------------");
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s3.equals(s4));
System.out.println(p1.equals(p2));
System.out.println("------------");
String s5 = "APPLE";
String s6 = "apple";
System.out.println(s5.equals(s6));
System.out.println(s5.equalsIgnoreCase(s6));
}
}
package ch11;
public class StringEx2 {
public static void main(String[] args) {
String str = "Java Programming";
int len = str.length();
System.out.println("len : " + len);
System.out.println(str.toLowerCase());
System.out.println(str.toUpperCase());
String str2 = str.substring(5);
String str3 = str.substring(5, 10);
System.out.println("str2 : " + str2);
System.out.println("str3 : " + str3);
char c1 = str.charAt(8);
System.out.println("8번째 문자 : " + c1);
for (int i = 0; i < str.length(); i++) {
if (i % 2 == 0)
System.out.println(str.charAt(i));
}
int idx1 = str.indexOf('a');
System.out.println("\n" + "idx1 : " + idx1);
int idx2 = str.lastIndexOf('a');
System.out.println("idx2 : " + idx2);
String str4 = str.replace('a', 'q');
System.out.println("str4");
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'a') {
System.out.print('q');
} else
System.out.print(str.charAt(i));
}
for (int i = str.length() - 1; i >= 0; i--) {
System.out.print(str.charAt(i));
}
StringBuffer sb = new StringBuffer(str);
String reverse = sb.reverse().toString();
System.out.println(reverse);
String str5 = "Java&JSP&Android&Iot&Spring";
String str6[] = str5.split("&");
for (int i = 0; i < str6.length; i++) {
System.out.println(str6[i]);
}
String str7 = " JSPStudy ";
System.out.println("***" + str7.trim() + "***");
int idx3 = 22;
String str8 = String.valueOf(idx3);
String str9 = idx3+"";
System.out.println(str + " : " + str9);
}
}
package ch11;
public class StringEx3 {
public static void main(String[] args) {
String str = "전지현이가 백화점에서 팬사인회를 연다." + "전지현은 5일 오후 3시 서울 소공동 롯데 백화점" + " 8층 이벤트홀에서... 구두 브랜드 조이제화의"
+ " 홍보를 위한 팬사인회에 참석한다.";
int idx = -1;
int len=str.length();
do {
idx = str.indexOf(' ', idx + 1);
if (idx >= 0)
System.out.print(idx + ", ");
} while (idx >= 0);
System.out.println();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ')
System.out.print(i + ", ");
}
System.out.println();
idx = len;
do {
idx = str.lastIndexOf(' ',idx-1);
if(idx>=0) System.out.print(idx+", ");
}while(idx>=0);
System.out.println();
for (int i = len-1; i >=0; i--) {
if (str.charAt(i) == ' ')
System.out.print(i + ", ");
}
System.out.println();
for (int i = 0; i < len ; i++) {
char c = str.charAt(i);
if(c!= ' ') System.out.print(c);
else System.out.print('_');
}
System.out.println();
System.out.print(str.replace(' ', '_'));
System.out.println();
int a= str.indexOf(' ');
String str2 = str.substring(0,a);
System.out.println("str2 : "+str2);
int b= str.lastIndexOf(' ')+1;
String str3= str.substring(b,len);
System.out.println("str3 : "+str3);
}
}
Wrapper
package ch11;
import java.util.Vector;
public class WrapperEx1 {
public static void main(String[] args) {
int a = 10;
Integer it1 = a;
int b = it1;
Vector v = new Vector();
v.add(a);
v.add(it1);
int c = (Integer) v.get(0);
Integer it2 = new Integer(10);
Integer it3 = new Integer("20");
Integer it4 = Integer.valueOf(10);
Integer it5 = Integer.valueOf("20");
int a1 = it4.intValue();
a1 = it4;
int b1 = it5.intValue();
int c1 = a1 + b1;
System.out.println(c1);
int a2 = Integer.parseInt("30");
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.toBinaryString(a2));
}
}
package ch11;
public class WrapperEx2 {
public static void main(String[] args) {
char c1 = 'J';
System.out.println(c1);
if(Character.isLetter(c1))
System.out.println(" : 문자입니다.");
char c2 = 'a';
if(Character.isUpperCase(c2) )
System.out.println(" : 대문자.");
char c3 = '2';
if(Character.isDigit(c3))
System.out.println(" : 숫자입니다.");
char c4 = ' ';
if(Character.isWhitespace(c4))
System.out.println(" : 공백입니다.");
Long l;
Boolean b;
Short s;
Double d;
Float f;
Byte bt;
Integer i;
Character c;
}
}