JAVA Programming 1
2 3, ->
() 3 Project FileIO WebFile.class FileCopy.class FileRW.class Tools.class Graphic DObject.class Line.class Rect.class Circle.class Project/FileIO/Tools.class Project/UI/Tools.class UI Main.class GUI.class EventHandler.class Tools.class
(package) 4!!! (.) Project.FileIO.Tools.class Project.UI.Tools.class jar! ) JDK rt.jar
JDK 5 () java.awt.color : java.awt java.awt
, import 6! import! Import import java.util.scanner; import java.util.*; *. public class ImportExample { public static void main(string[] args) { java.util.scanner scanner = new java.util.scanner(system.in); System.out.println(scanner.next()); import java.util.scanner; public class ImportExample { public static void main(string[] args) { Scanner scanner = new Scanner(System.in); import java.util.*; public class ImportExample { public static void main(string[] args) { Scanner scanner = new Scanner(System.in);
7 ()! 2 : CLASSPATH java classpath C:> java -classpath "C:\Programs Files\java\jdk1.8.0_131\jre\lib" ImportExample -classpath
8! package ; package UI; // Tools UI public class Tools {... // UI.Tools. Tools UI.Tools Tools import UI.Tools package Graphic; // Line Graphic import UI.Tools; // Tools public class Line { public void draw() { Tools t = new Tools();
9! (5 5-7) abstract class Calculator { public abstract int add(int a, int b); public abstract int subtract(int a, int b); public abstract double average(int[] a); public class GoodCalc extends Calculator { public int add(int a, int b) { return a+b; public int subtract(int a, int b) { return a - b; public double average(int[] a) { double sum = 0; for (int i = 0; i < a.length; i++) sum += a[i]; return sum/a.length; public static void main(string [] args) { Calculator c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] {2,3,4 ));
10 ( : PackageEx)
11 lib, app
app lib. 12
Calculator Calculator public abstract. 13
Calculator, app public. 14
GoodCalc.java import. Calculator. 15
Run Configurations. main(). 16
17 PackageEx
18! package!!!!!
JDK 19 JDK!! C! JDK rt.jar C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar rt.jar java.awt.
java applet awt beans io lang math net nio rmi security sql text util beancontext spi color datatransfer dnd event font geom im image print channels charset spi renderable spi spi annotation instrument management ref reflect activation dgc registry server acl cert spec interfaces concurrent jar logging prefs regex spi zip 20 atomic locks
21 java.lang! language,,! import - import java.util!,,, java.io!,,, java.awt! GUI javax.swing! GUI
API 22 API! Oracle Technology Network(http://docs.oracle.com/javase/8/docs/api/)
Object 23! java.lang!
6-1 : Object 24,, class Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; public class ObjectPropertyEx { public static void print(object obj) { System.out.println(obj.getClass().getName()); // System.out.println(obj.hashCode()); // System.out.println(obj.toString()); // System.out.println(obj); // public static void main(string [] args) { Point p = new Point(2,3); print(p); Point 366712642 Point@15db9742 Point@15db9742
25 String tostring()!! Object tostring() public String tostring() { return getclass().getname() +"@" + Integer.toHexString(hashCode()); + ->.tostring() + Point p = new Point(2,3); System.out.println(p); String s = p + ""; System.out.println(p.toString()); String s = p.tostring()+ ""; Point@15db9742 tostring()! Object tostring()
6-2 : Point tostring() 26 Point Point tostring(). class Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; public String tostring() { return "Point(" + x + "," + y + ")"; Point tostring() public class ToStringEx { public static void main(string [] args) { Point p = new Point(2,3); System.out.println(p.toString()); System.out.println(p); // p p.tostring() System.out.println(p + "."); // p.tostring() + "" Point(2,3) Point(2,3) Point(2,3).
equals() 27 == boolean equals(object obj)!! class Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; Point a = new Point(2,3); Point b = new Point(2,3); Point c = a; if(a == b) // false System.out.println("a==b"); if(a == c) // true System.out.println("a==c"); a==c class Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; public boolean equals(object p) { Point p = (Point)obj; if(x == p.x && y == p.y) return true; else return false; Point a = new Point(2,3); Point b = new Point(2,3); Point c = new Point(3,4); if(a == b) // false System.out.println("a==b"); if(a.equals(b)) // true System.out.println("a is equal to b"); if(a.equals(c)) // false System.out.println("a is equal to c"); a is equal to b
6-3 : Point equals() 28 Point true equals(). class Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; public boolean equals(object obj) { Point p = (Point)obj; if(x == p.x && y == p.y) return true; else return false; public class EqualsEx { public static void main(string[] args) { Point a = new Point(2,3); Point b = new Point(2,3); Point c = new Point(3,4); if(a == b) // false System.out.println("a==b"); if(a.equals(b)) // true System.out.println("a is equal to b"); if(a.equals(c)) // false System.out.println("a is equal to c"); a is equal to b
6-4 : Rect equals() 29 int width() height() Rect, Rect equals(). width, height. class Rect { int width; int height; public Rect(int width, int height) { this.width = width; this.height = height; public boolean equals(object obj) { Rect p = (Rect)obj; if (width*height == p.width*p.height) return true; else return false; public class EqualsEx { public static void main(string[] args) { Rect a = new Rect(2,3); Rect b = new Rect(3,2); Rect c = new Rect(3,4); if(a.equals(b)) System.out.println("a is equal to b"); if(a.equals(c)) System.out.println("a is equal to c"); if(b.equals(c)) System.out.println("b is equal to c"); a is equal to b
Wrapper 30 8! Wrapper!
Wrapper 31 Wrapper Integer i = new Integer(10); Character c = new Character( c ); Double f = new Double(3.14); Boolean b = new Boolean(true); Wrapper Integer I = new Integer( 10 ); Double d = new Double( 3.14 ); Boolean b = new Boolean( false ); Float double Float f = new Float((double) 3.14);
32! Wrapper, static! Integer
Wrapper 33 Wrapper Integer i = new Integer(10); int ii = i.intvalue(); // ii = 10 Character c = new Character('c' ); char cc = c.charvalue(); // cc = c Double f = new Double(3.14); double dd = d.doublevalue(); // dd = 3.14 Boolean b = new Boolean(true); boolean bb = b.booleanvalue(); // bb = true int i = Integer.parseInt("123"); // i = 123 boolean b = Boolean.parseBoolean("true"); // b = true double f = Double.parseDouble("3.14" ); // d = 3.14 String s1 = Integer.toString(123); String s2 = Integer.toHexString(123); String s3 = Double.toString(3.14); String s4 = Charater.toString('a'); String s5 = Boolean.toString(true); // 123 "123" // 123 16 "7b" // 3.14 "3.14" // a "a" // true "true"
6-5 : Wrapper 34 Wrapper.? public class WrapperEx { public static void main(string[] args) { System.out.println(Character.toLowerCase('A')); // 'A' char c1='4', c2='f'; if(character.isdigit(c1)) // c1 true System.out.println(c1 + " "); if(character.isalphabetic(c2)) // c2 true System.out.println(c2 + " "); System.out.println(Integer.parseInt("-123")); // "-123" 10 System.out.println(Integer.toHexString(28)); // 28 2 System.out.println(Integer.toBinaryString(28)); // 28 16 System.out.println(Integer.bitCount(28)); // 28 2 1 Double d = new Double(3.14); System.out.println(d.toString()); // Double "3.14" System.out.println(Double.parseDouble("3.14")); // 3.14 boolean b = (4>3); // b true System.out.println(Boolean.toString(b)); // true "true" System.out.println(Boolean.parseBoolean("false")); // false 4 F -123 1c 11100 3 3.14 3.14 true false
35 (boxing)! Wrapper (unboxing)! Wrapper JDK1.5 Integer ten = 10; int n = ten; //. Integer ten = new Integer(10); //. int n = ten.intvalue();
6-6 : 36? public class AutoBoxingUnBoxingEx { public static void main(string[] args) { int n = 10; Integer intobject = n; // auto boxing System.out.println("intObject = " + intobject); int m = intobject + 10; // auto unboxing System.out.println("m = " + m); intobject = 10 m = 20
String 37 String - java.lang.string! String // String str1 = "abcd"; // String char data[] = {'a', 'b', 'c', 'd'; String str2 = new String(data); String str3 = new String("abcd"); // str2 str3 "abcd"! String
new String() 38!, String s = "Hello"; JVM,! String, String t = new String("Hello"); String
39 equals()! equals()
40
41! int compareto(string anotherstring) 0 anotherstring anotherstring String java= "Java"; String cpp = "C++"; int res = java.compareto(cpp); if(res == 0) System.out.println("the same"); else if(res <0) System.out.println(java + " < " + cpp); else System.out.println(java + " > " + cpp); "Java" "C++" Java > C++! ==
42 +!.tostring() System.out.print("abcd" + 1 + true + 3.13e-2 + 'E'+ "fgh" ); // abcd1true0.0313efgh String concat(string str) "I love ".concat("java.") "I Love Java."! String
concat() 43 String s1 = "abcd"; String s2 = "efgh"; s1 = s1.concat(s2); s1.concat(s2) abcdefgh s1 abcd s1 abcd s2 efgh s2 efgh
, 44! String trim() (tab, enter, space) String a = " abcd def "; String b = " xyz\t"; String c = a.trim(); // c = "abcd def". String d = b.trim(); // d = "xyz". '\t' "! char charat(int index) String a = "class"; char c = a.charat(2); // c = 'a' // "class" s int count = 0; String a = "class"; for(int i=0; i<a.length(); i++) { // a.length() 5 if(a.charat(i) == 's') count++; System.out.println(count); // 2
6-7 : String 45 String. public class StringEx { public static void main(string[] args) { String a = new String(" C#"); String b = new String(",C++ "); System.out.println(a + " " + a.length()); // ( ) System.out.println(a.contains("#")); // a = a.concat(b); // System.out.println(a); a = a.trim(); // System.out.println(a); a = a.replace("c#","java"); // System.out.println(a); String s[] = a.split(","); // for (int i=0; i<s.length; i++) System.out.println(" " + i + ": " + s[i]); a = a.substring(5); // 5 System.out.println(a); char c = a.charat(2); // 2 System.out.println(c); C# 3 true C#,C++ C#,C++ Java,C++ 0: Java 1: C++ C++ +
46
StringBuffer 47! Java.lang.StringBuffer! String! StringBuffer StringBuffer sb = new StringBuffer("java");
48
49 StringBuffer
6-8 : StringBuffer 50 StringBuffer? public class StringBufferEx { public static void main(string[] args) { StringBuffer sb = new StringBuffer("This"); sb.append(" is pencil"); // System.out.println(sb); sb.insert(7, " my"); // "my" System.out.println(sb); sb.replace(8, 10, "your"); // "my" "your" System.out.println(sb); sb.delete(8, 13); // "your " System.out.println(sb); sb.setlength(4); // System.out.println(sb); sb.tostring() This is pencil This is my pencil This is your pencil This is pencil This
StringTokenizer 51 java.util.stringtokenizer! : (delimiter) & String query = "name=kitae&addr=seoul&age=21"; StringTokenizer st = new StringTokenizer(query, "&"); (token)! String split()
52 StringTokenizer
53
6-9 : StringTokenizer 54 //// /. import java.util.stringtokenizer; public class StringTokenizerEx { public static void main(string[] args) { StringTokenizer st = new StringTokenizer("////", "/"); while (st.hasmoretokens()) System.out.println(st.nextToken());
Math 55, java.lang.math! static :
Math 56! static double random() 0.0 1.0 double 0 100 10 for(int x=0; x<10; x++) { int n = (int)(math.random()*100 + 1); // n [1~100] System.out.println(n); Math.random()*100 0.0~99.99.. Math.random()*100+1 1.0~100.99.. (int)(math.random()*100 + 1) 1~100 java.util.random!
6-10 : Math 57 Math. public class MathEx { public static void main(string[] args) { System.out.println(Math.PI); // System.out.println(Math.ceil(a)); // ceil() System.out.println(Math.floor(a)); // floor() System.out.println(Math.sqrt(9)); // System.out.println(Math.exp(2)); // e 2 System.out.println(Math.round(3.14)); // // [1, 45] 5 System.out.print(" "); for(int i=0; i<5; i++) System.out.print((int)(Math.random()*45 + 1) + " "); 3.141592653589793 4.0 3.0 3.0 7.38905609893065 3 15 31 9 7 5
Calendar 58 Calendar! java.util!,,,,,,,, Calendar
Calendar 59 Calendar,! Calendar now = Calendar.getInstance(); now Calendar new Calendar() int year = now.get(calendar.year); // now int month = now.get(calendar.month) + 1; // now! Calendar Calendar // Calendar firstdate = Calendar.getInstance(); firstdate.clear(); //. firstdate.set(2016, 11, 25); // 2016 12 25. 12 11 firstdate.set(calendar.hour_of_day, 20); // 8 firstdate.set(calendar.minute, 30); // 30
6-11 : Calendar / 60 import java.util.calendar; public class CalendarEx { public static void printcalendar(string msg, Calendar cal) { int year = cal.get(calendar.year); // get() 0~30. int month = cal.get(calendar.month) + 1; int day = cal.get(calendar.day_of_month); int dayofweek = cal.get(calendar.day_of_week); int hour = cal.get(calendar.hour); int hourofday = cal.get(calendar.hour_of_day); int ampm = cal.get(calendar.am_pm); int minute = cal.get(calendar.minute); int second = cal.get(calendar.second); int millisecond = cal.get(calendar.millisecond); public static void main(string[] args) { Calendar now = Calendar.getInstance(); printcalendar(" ", now); Calendar firstdate = Calendar.getInstance(); firstdate.clear(); // 2016 12 25. 12 month 11 firstdate.set(2016, 11, 25); firstdate.set(calendar.hour_of_day, 20); // 8 firstdate.set(calendar.minute, 30); // 30 printcalendar(" ", firstdate); System.out.print(msg + year + "/" + month + "/" + day + "/"); switch(dayofweek) { case Calendar.SUNDAY : System.out.print(""); break; case Calendar.MONDAY : System.out.print(""); break; case Calendar.TUESDAY : System.out.print(""); break; case Calendar.WEDNESDAY : System.out.print(""); break; case Calendar.THURSDAY : System.out.print(""); break; case Calendar.FRIDAY: System.out.print(""); break; case Calendar.SATURDAY : System.out.print(""); break; System.out.print("(" + hourofday + ")"); if(ampm == Calendar.AM) System.out.print(""); else System.out.print(""); System.out.println(hour + " " + minute + " " + second + " + millisecond +""); 2017/3/29/(19)7 59 51 892 2016/12/25/(20)8 30 0 0
61