Upload
muhammad-akbar-sihotang
View
49
Download
0
Embed Size (px)
Citation preview
Java 2013/1/30(C) 2013 Jun Mitani Java
Java 1 : 280: (2010/1/29) ISBN-10: 4798120987ISBN-13: 978-4798120980 2010/1/29 Java1
1 Java
1Java
JavaJavaclass FirstExample { public static void main(String[] args) { System.out.println(""); }}
Java
JavaJavaWindowsMac OSLinuxOS
C/C++ 1/2B(#include #define #if #ifdef ) C/C++
C/C++ 2/2Bbooleantrue/falsenew int[] a = new int[5];delete String String str = "Hello"; C/C++
Java Examplepublic static void main(String[] args) { } {}class { public static void main(String[] args) { }}
Java(;)class FirstExample { public static void main(String[] args) { System.out.println(""); }}
{ } 11{ } class FirstExample {public static void main(String[] args) {System.out.println("");}}
1 /* */ 2 // 1/* 2010121*/class FirstExample { public static void main(String[] args) { // System.out.println(""); }}
1
2 Eclipse> javac FirstExample.java > java FirstExample
Eclipse [][][Java] [][][] [][][Java]
Compile Error (0)(o)(O) (1)(I)(l) (;)(:) (.)(,)
.java.class.java FirstExample.java.class FirstExample.classEclipseworkspace
JavaJavaFirstExample.java .java.class
2 Java
EclipseSystem.out.println();class FirstExample { public static void main(String[] args) { System.out.println(""); }}
\System.out.println( "\"Java\"");
System.out.println"Java"
int i; // i = 5; // System.out.println(i); //
; 1 int i; 2 double d; 3 boolean boo = false; 4 char c = '';
Java
doublebooleancharclass Example { public static void main(String args[]) { int i; i = 5; System.out.println(i); } }
System.out.println(2 + 3);
+,-*,/System.out.println(3 + 6 / 3); // 5System.out.println((3 + 6) /3); // 3
class Example { public static void main(String args[]) { System.out.println(2 + 3); }}
int i = 10;int j = i * 2;System.out.println(j); // 20int i = 10;i = i + 3;System.out.println(i); // 13int i = 10;i += 3; // System.out.println(i); // 13
a = a + 5;b = b - 6;c = c * a;d = d / 3;e = e % 2;f = f + 1;g = g - 1;
class CalcExample3 { public static void main(String[] args) { int i; i = 11; i++; i /= 2; System.out.println("i" + i);
int j; j = i * i; System.out.println("j" + j); }}
i = 2 + 3;i = 2 + 3int i;int j = (i = 2 + 3) * 2;System.out.println(i); System.out.println(j); // 5// 10
i++;++i;i1j=i++;j=++i;jj = i; i = i + 1;i = i + 1; j = i;
int i = 10;int j = i++;int k = ++i;System.out.println(i);System.out.println(j);System.out.println(k);
double > int
(double)(int)
double d = 9.8;int i = d; double d = 9.8;int i = (int)d;
doubleint i = 5;double d = 0.5;System.out.println(i + d); // 5.5
5/2 2 double double c = (double)a/(double)b;int a = 5;int b = 2;double c = a / b;System.out.println(c); // 2.0
723.5class Example { public static void main(String[] args) { int a = 7; int b = 2; double d = a / b; System.out.println(d); }}
String StringString s;s = "";System.out.println(s); +String s1 = "";String s2 = "";String s3 = s1 + s2;System.out.println(s3);
3
if() { ;}if() { ; //true}
if(age < 20) { System.out.println("");}
2 true false
a 20 a 20 a a a 3 a a 5 2
if else if() { ;} else { ;}if() { //true 1; } else { //false 2; }
ifelseint age;age = 20;if(age < 20) { System.out.println("");} else { System.out.println("");}
if else int age;age = 20;if(age < 4) { System.out.println("");} else if(age < 13) { System.out.println("");} else { System.out.println("");}
ifelse
ifelseif( == ) { if( == true) { } else { }} else { }
a 3, 5, 8, 9, 10, 15, 20 if(a < 5) { System.out.println("A");} else if(a < 9) { System.out.println("B");} else if(a < 15) { System.out.println("C");} else { System.out.println("D");}
if{}if(age >= 20) System.out.println("");if(age >= 20) {System.out.println("");}if1{}2{}if(age >= 20) System.out.println("");System.out.println("");
switchbreak;
switchswitch (score) {case 1:System.out.println("");break;case 2:System.out.println("");break;case 3:System.out.println("");break;case 4:System.out.println("");break;case 5:System.out.println("");break;default:System.out.println("");}System.out.println("switch");
switch(2)switch (score) {case 1:case 2:System.out.println("");break;case 3:case 4:case 5:System.out.println("");break;default:System.out.println("");}
switchi1,2,3,4,5switch(i) {case 1: System.out.println("A");case 2: break;case 3: System.out.println("B");case 4:default: System.out.println("C");}
3 ? 1 : 2
true1false2int c;if(a > b) { c = a;} else { c = b;}int c = (a > b) ? a : b;
3
= > 1000 ? : ;
cint a = 5;int b = 3;int c = (a > b) ? a : b;int a = 5;int b = 3;int c = (a > b * 2) ? a + 1 : b - 3;int a = -5;int c = (a > 0) ? a : -a;(a)(b)(c)
age13 age65age >= 13 && age < 65age13 age 65 20age >= 13 && age < 65 && age !=20age13 age 65age < 13 || age >= 65
a + 10 > b * 5(a + 10) > (b * 5)a > 10 && b < 3(a > 10) && (b < 3)x && ( y || z )(x && y ) || z
if( == && != ) { }
a,b,c
abbc abac a,b,cc c>b>a acab ab2ab3
3forwhiledo while
forforfor(; ; ){ }true false for 2.
forfor(int i = 0; i < 5; i++) { System.out.println("");}
110011002312x-10101
whilewhilewhile(){ }true false while1.
for
whileint i = 0;while(i < 5) { System.out.println(""); i++; // }int i = 5;while(i > 0) { System.out.println(i); i--; // }
do whiledo whiledo { } while();true1. false dowhile
forwhile1
do whileint i = 0;do { System.out.println(""); i++;} while(i < 5);int i = 5;do { System.out.println(i); i--; } while(i > 0);
breakbreak; int sum = 0;for(int i = 1; i 20) { System.out.println("20"); break; }}System.out.println("" + sum );
1
int[] scores; scores = new int[5]; scores[0] = 50; scores[4] = 80; System.out.println(scores[2]);[]0(-1)
int[] scores;scores = new int[5];scores[0] = 50;scores[1] = 55;scores[2] = 70;scores[3] = 65;scores[4] = 80;
for(int i = 0; i < 5; i++) {System.out.println(scores[i]);}
int[] scores = {50, 55, 70, 65, 80};int n = scores.length;
011325364552class Example { public static void main(String args[]) { int counts[] = {1, 3, 5, 6, 5, 2};
}}0:*1:***2:*****3:******4:*****5:**
int[][] scores = new int[3][5];scores[0][0] = 50;scores[2][3] = 65;
4
classJava class
2(x=2,y=5)(x=-1,y=3)(0,0)1(3,4)5
2(x,y)class Point { int x; int y;}Pointxyintxy2Point
class { }
class { }
newnew Point();Point p = new Point();Point p = new Point();p.x = 10;p.y = 5;
Pointxy Point p = new Point(); p.x = 10; px
Pointclass Point { int x; // x int y; // y}
class PointInstanceExample { public static void main(String[] args) { Point p1 = new Point(); p1.x = 10; p1.y = 5;
Point p2 = new Point(); p2.x = 5; p2.y = 2;
System.out.println("p1x" + p1.x); System.out.println("p1y" + p1.y); System.out.println("p2x" + p2.x); System.out.println("p2y" + p2.y); }}
Javaint, double, boolean
Point p = new Point(); pPoint
class Dog { String name;}
class Example { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.name = "Taro"; Dog dog2 = new Dog(); dog2.name = "Pochi"; Dog dog3 = dog2; System.out.println(dog3.name); dog3.name = "Jiro"; System.out.println(dog2.name); }}
Point p1 = new Point();Point p2 = new Point();Point p3 = p2;p1.x = 0; p1.y = 0; p2.x = 5; p2.y = 10;
Point[] points = new Point[5];points[0] = new Point();points[1] = new Point();points[2] = new Point();points[3] = new Point();points[4] = new Point();
nullnullPoint[] points = new Point[5];points[0] = new Point();points[1] = new Point();points[2] = new Point();
nullnullPoint p;p = null;nullPoint p = new Point();if(p == null) { System.out.println("pnull");} else { System.out.println("pnull");}
5
Pointx,y Point
class { // // }void () {}
class Point { int x; int y;
// void printPosition() { System.out.println("(" + this.x + ", " + this.y + ")"); }}Point p = new Point();p.x = 10;p.y = 5;p.printPosition();this
void ( ) {}
()class Point { int x; int y; // xyn void multiply(int n) { this.x *= n; this.y *= n; }}Point p = new Point();p.x = 10;p.y = 5;p.multiply(2);Point
class Point { int x; int y; // xydxdy void add(int dx, int dy) { this.x += dx; this.y += dy; }}Point p = new Point();p.x = 10;p.y = 5;p.add(2,3);Point
() { return ;}
return 1class Point { int x; int y; // xy int getXY() { return this.x*this.y; }}Point p = new Point();p.x = 10;p.y = 5;int xy = p.getXY();Point
class Point { int x; int y;
boolean isSamePosition(Point p) { if(this.x == p.x && this.y == p.y) { return true; } else { return false; } }}Point p1 = new Point();p1.x = 10;p1.y = 5;Point p2 = new Point();p2.x = 10;p2.y = 10;if(p1.isSamePosition(p2)){ System.out.println("");} else { System.out.println("");}
Point
void () {}void ( ) {} ( ) {return ;}
if(this.x == p.x && this.y == p.y) { return true;} else { return false;}return (this.x == p.x && this.y == p.y);if(p1.isSamePosition(p2) == true) { }if(p1.isSamePosition(p2)) { }
() { }
class Point { int x; int y;
// Point(int x, int y) { this.x = x; this.y = y; }}Point p = new Point(5, 10);System.out.println(p.x);System.out.println(p.y);Point
class Rectangle { double width; // double height; // }
1.RectanglegetArea2.3.RectangletruefalseisLarger
6
class Point { int x; int y;
void set(int x, int y) { this.x = x; this.y = y; }
void set(Point p) { this.x = p.x; this.y = p.y; }}
Point p1 = new Point();p1.set(10, 0);
Point p2 = new Point(); p2.set(p1);2
class Point { int x; int y;
Point() { this.x = 0; this.y = 0; }
Point(Point p) { this.x = p.x; this.y = p.y; }
Point(int x, int y) { this.x = x; this.y = y; }}Point p1 = new Point();Point p2 = new Point(10, 20);Point p3 = new Point(p2);
thisthis.this.()this()
thisthisclass Point { int x; int y;
void set(int x, int y) { this.x = x; this.y = y; }
void set(Point p) { this.x = p.x; this.y = p.y; }}
static Point counterint
class Point { static int counter = 0; int x; int y;}
class Point { static int counter = 0; int x; int y;}
class Point { static int counter = 0; int x; int y;
Point(int x, int y) { this.x = x; this.y = y; Point.counter++; }}System.out.println( Point.counter);
Point p1 = new Point(0, 0);Point p2 = new Point(5, 0);Point p3 = new Point(10, 5);
System.out.println( Point.counter);.1
thisclass Point { static int counter = 0; int x; int y;
Point(int x, int y) { this.x = x; this.y = y; Point.counter++; }}
. static
class SimpleCalc { // static double getTriangleArea(double base, double height) { return base * height / 2.0; }}System.out.println("105" + SimpleCalc.getTriangleArea(10, 5) + "");
class { }
Java[ (1) ][ (1) ][ (1) ][ (2) ][ (3) ]new[ (4) ][ (3) ]intdouble[ (5) ][ (4) ] [ (6) ][ (6) ][ (7) ](a) (b) (c) (d) (e) (f) (g)null (h) (i)
7
JavaABABBABABAAB
Java1Object
extendsclass A { A}
class B extends A { }ABBABextends A
ObjectObjectextends Object class A extends Object { A}
class B extends A { }
class Point { int x; int y;
void printInfo() { System.out.println(x); System.out.println(y); }}class ColorPoint extends Point { String color;}ColorPoint cp = new ColorPoint(); cp.x = 5;cp.y = 10;cp.color = "";
class Point { int x; int y;
void printInfo() { // }}class ColorPoint extends Point { String color;
void printInfo() { // }}ColorPoint cp = new ColorPoint(); cp.printInfo();
(1) ABAB(2) (3) (4)
class Point { int x; int y;
void printInfo() { // }}class ColorPoint extends Point { String color;
void printInfo() { super.printInfo(); }}super.();
super();
class B extends A {}class B extends A { B() { super(); }}
class B extends A { B() { abc(); } B(int i) { def();}}class B extends A { B() {super();abc();} B(int i) {super(); def();}}
class X { X() { System.out.println("[X]");} void a() { System.out.println("[x.a]");} void b() { System.out.println("[x.b]");}}class Y extends X { Y() { System.out.println("[Y]"); } void a() { System.out.println("[y.a]"); }}X,YX x = new X();x.a();x.b();Y y = new Y();y.a();y.b();
class X { X() { System.out.println("[X()]");} X(int i) { System.out.println("[X(int i)]");}}class Y extends X { Y() { System.out.println("[Y()]"); } Y(int i) { System.out.println("[Y(int i)]"); }}class Z extends Y {}X,YY y0 = new Y();Y y1 = new Y(10);Z z = new Z();
Point p = new Point();ColorPoint cp = new ColorPoint();Point cp = new ColorPoint();PointColorPoint
class Person { void work() { // "" }}
class Student extends Person { void work() { // "" }}
class Teacher extends Person { void work() { // "" } void makeTest() { }}Person[] persons = new Person[3];persons[0] = new Person();persons[1] = new Student();persons[2] = new Teacher();
for(int i = 0; i < 3; i++) {persons[i].work();}
// 3void workThreeTimes(Person p) { p.work(); p.work(); p.work();}Personnew Teacher(), new Student()work
A,B,C class A { } class B extends A { } class C { }(1) A a = new A();(2) A a = new B();(3) A a = new C();(4) B b = new A();(5) B b = new B();(6) B b = new C();
Person p = new Teacher();Teacher t = (Teacher)p;t.makeTest();Person p = new Teacher();((Teacher)p).makeTest();
privateprivate
privateclass Car { private int speed; // (Km/h)
// speed180 public void speedUp() { if(speed < 80) {speed++; } }
// speed10 public void speedDown() { if(speed > 0) { speed--; } }}
class Example { private int valueA; private int valueB;
public int getValueA() { return valueA; } public void setValueA(int a) { valueA = a; } public int getValueB() { return valueB; } public void setValueA(int b) { valueB = b; }}
finalfinal
public final static double PI = 3.141592653589793;public final static int ADULT_AGE = 20;if(age == 20) { }if(age == ADULT_AGE) { }
static static int counter = 0;static double getSum(int x, int y) {return x + y;}public static void main(String args[]) { }
8
abstractabstract class Shape {}ShapeShape s = new Shape();
abstractabstract class Shape { abstract void draw();}
abstract class Shape { abstract void draw();}
class Polyline extends Shape { void draw() { // }}class Rectangle extends Shape { void draw() { // }}class Circle extends Shape { void draw() { // }}Shape[] shapes = new Shape[3];shapes[0] = new Polyline();shapes[1] = new Rectangle();shapes[2] = new Circle();
for(int i = 0; i < 3; i++) {shapes[i].draw();}
Java1
interface { }interface HasGetAreaMethod { double getArea();}class implements { }
interface HasGetAreaMethod { double getArea();}class Rectangle implements HasGetAreaMethod { double getArea() { return width*height;}}class Circle implements HasGetAreaMethod { double getArea() { return r*r*3.14;}}HasGetAreaMethod r = new Rectangle();HasGetAreaMethod c = new Circle();
interface I {}abstract class A {}class B extends A {}class C implements I {} A a = new A(); B b = new B(); C c = new C(); I i = new I();5. A b = new B();6. B a = new A();7. I b = new B();8. I c = new C();
*******************************************************************************************************************************************************************************