Java 2016/4/17
Java 1 Java1 : 280 : (2010/1/29) ISBN-10: 4798120987 ISBN-13: 978-4798120980 2010/1/29
1 Java
1 Java
Java Java class FirstExample { public static void main(string[] args) { System.out.println(" ");
Java
Java Java Windows MacOS Linux OS
C/C++ 1/2 B (#include # #if #ifdef ) C/C++
C/C++ 2/2 B boolean true/false new int[] a = new int[5]; String String str = "Hello"; C/C++
Java class { public static void main(string[] args) { Example public static void main(string[] args) { {
Java class FirstExample { public static void main(string[] args) { System.out.println(" ");
class FirstExample { public static void main(string[] args) { System.out.println(" "); { 1 1 {
/* 2010 12 1 */ class FirstExample { public static void main(string[] args) { // System.out.println(" "); 1 /* */ 2 // 1
1 > javac FirstExample.java > java FirstExample 2 Eclipse
Eclipse 1. [ ] [ ] [Java ] 2. [ ] [ ] [ ] 3. [ ] [ ] [Java ]
Compile Error (0) (o) (1) (I) (;) (:) (.) (,)
.java.class FirstExample.java.class FirstExample.class Eclipse workspace
1.Java 2. Java 3.FirstExample.java 4..java.class
2 Java
Ecli System.out.println( class FirstExample { public static void main(string[] args) { System.out.println(" ");
System.out.println( " "Java " ");
1. 2. System.out.println 3. "Java "
int i; // i = 5; // System.out.println(i); //
; 1 int i; 2 double d; 3 boolean boo = false; 4 char c = ' ';
Java
1. double boolean char class 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); // 5 System.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); // 20 int i = 10; i = i + 3; System.out.println(i); // 13 int i = 10; i += 3; // System.out.println(i); // 13
1. a = a + 5; 2. b = b - 6; 3. c = c * a; 4. d = d / 3; 5. e = e % 2; 6. f = f + 1; 7. 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 + 3 int i; int j = (i = 2 + 3) * 2; System.out.println(i); // 5 System.out.println(j); // 10
i++; ++i; i 1 j=i++; j=++i; j j = 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 d = 9.8; int i = d; double d = 9.8; int i = (int)d; (double) (int)
int i = 5; double d = 0.5; System.out.println(i + d); // 5.5 double
int a = 5; int b = 2; double c = a / b; System.out.println(c); // 2.0 5/2 2 d double c = (double)a/(double)b;
7 2 3.5 class Example { public static void main(string[] args) { int a = 7; int b = 2; double d = a / b; System.out.println(d);
String String String 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
1. a 20 2. a 20 3. a 4. a 5. a 3 6. a 7. a 5 2
if else if( ) { ; else { ; if( ) { // true 1 ; else { // false 2 ;
if else int age; age = 20; if(age < 20) { System.out.println(" "); else { System.out.println("
if else if else int age; age = 20; if(age < 4) { System.out.println(" else if(age < 13) { System.out.println(" else { System.out.println("
if else if( == ) { 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 1 { 2 if(age >= 20) System.out.println(" "); if(age >= 20) { System.out.println(" "); { if(age >= 20) System.out.println(" "); System.out.println(" ");
switch break;
switch switch (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("
switch i 1,2,3,4,5 switch(i) { case 1: System.out.println("A"); case 2: break; case 3: System.out.println("B"); case 4: default: System.out.println("C");
3 int c; if(a > b) { c = a; else { c = b; int c = (a > b)? a : b;? 1 : 2 true false 2
3 = > 1000? : ;
c (a) (b) (c) int 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;
age 13 age 65 age >= 13 && age < 65 age 13 65 age < 13 age >= 65 age 13 65 20 age >= 13 && age < 65 && age!=20
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 (a) a b b c (b) a b a c (c) a,b,c c (d) c>b>a (e) a c a b (f) a b 2 a b 3
3 for while do while
for for for( ; ; ){ 1. 2. true false for 3. 4. 2.
for for(int i = 0; i < 5; i++) { System.out.println(" ")
for for 1 100 int sum = 0; for(int i = 1; i <= 100; i++) { sum += i; System.out.println(i + " System.out.println(" " + sum );
1. 1 100 2. 1 100 2 3 3. x - 10 10 1 x 2-2x + 1
{ class ForExample2 { public static void main(string[] args) { int sum = 0; for(int i = 1; i <= 100; i++) { sum += i; System.out.println(i + " "); System.out.println(" " + sum );
while while while( ){ 1. true false while 2.1. for
while int i = 0; while(i < 5) { System.out.println(" "); i++; // int i = 5; while(i > 0) { System.out.println(i); i--; //
do while do while do { while( ); 1 1. 2. true 1. false do while for while
do while int i = 0; do { System.out.println(" "); i++; while(i < 5); int i = 5; do { System.out.println(i); i--; while(i > 0);
break; int sum = 0; for(int i = 1; i <= 10; i++) { sum += i; System.out.println(i + " if(sum > 20) { System.out.println(" 20 break; System.out.println(" " + sum );
continue; int sum = 0; for(int i = 1; i <= 10; i++) { if(i % 2 == 0) { continue; sum += i; System.out.println(i + " System.out.println(" " + sum );
for(int a = 1; a <= 3; a++) { System.out.println("a = "+ a); // for(int b = 1; b <= 3; b++) { System.out.println("b = "+ b); // 3 9
1x1=1 2x1=2 3x1=3 4x1=4 5x1=5 6x1=6 7x1=7 8x1=8 9x1=9 1x2=2 2x2=4 3x2=6 4x2=8 5x2=10 6x2=12 7x2=14 8x2=16 9x2=18 1x3=3 2x3=6 3x3=9 4x3=12 5x3=15 6x3=18 7x3=21 8x3=24 9x3=27 1x4=4 2x4=8 3x4=12 4x4=16 5x4=20 6x4=24 7x4=28 8x4=32 9x4=36 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 6x5=30 7x5=35 8x5=40 9x5=45 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 7x6=42 8x6=48 9x6=54 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 8x7=56 9x7=63 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 9x8=72 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81 class Example { public static void main(string args[]) { for(int i = 1; i <= 9; i++) { for(int j = 1; j <= 9; j++) { System.out.println(); //
1
1. int[] scores; 2. scores = new int[5]; 3. scores[0] = 50; scores[4] = 80; [] 0 ( - 1) 4. System.out.println(scores[2]);
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;
0 1 1 3 2 5 3 6 4 5 5 2 0:* 1:*** 2:***** 3:****** 4:***** 5:** class Example { public static void main(string args[]) { int[] counts = {1, 3, 5, 6, 5, 2;
int[][] scores = new int[3][5]; scores[0][0] = 50; scores[2][3] = 65;
4
class Java class
2 (x=2,y=5) (x= -1,y=3) (0,0) 1 (3,4) 5
2 ( x,y) class Point { int x; Point int y; Point x y int x y 2
class { class {
new Point(); Point p = new Point(); p.x = 10; p.y = 5; Point p = new Point();
Point x y Point p = new Point(); p.x = 10; x
class Point { int x; // x int y; // y Point 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("p1 x " + p1.x); System.out.println("p1 y " + p1.y); System.out.println("p2 x " + p2.x); System.out.println("p2 y " + p2.y);
Java int, double, boolean
Point p = new Point(); p Point
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();
null Point[] points = new Point[5]; points[0] = new Point(); points[1] = new Point(); points[2] = new Point();
null null Point p; p = null; null Point p = new Point(); if(p == null) { System.out.println("p null"); else { System.out.println("p null
5
Point x,y Point
class { // // void () {
class Point { int x; int y; // void printposition() { System.out.println(" (" + this.x + ", " + this.y + ") "); this Point p = new Point(); p.x = 10; p.y = 5; p.printposition();
void ( ) {
() Point class Point { int x; int y; // x y n 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; // x y dx dy 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);
( ) { return ;
return 1 Point class Point { Point p = new Point(); int x; p.x = 10; int y; p.y = 5; // x y int getxy() { return this.x*this.y; 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(" ");
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)) {
( ) {
Point class Point { int x; int y; Point p = new Point(5, 10); // System.out.println(p.x); Point(int x, int y) { System.out.println(p.y); this.x = x; this.y = y;
class Rectangle { double width; // double height; // 1.Rectangle getarea 2. 3. Rectangle true false
6
2 class Point { int x; int y; void set(int x, int y) { this.x = x; this.y = y; Point p1 = new Point(); p1.set(10, 0); Point p2 = new Point(); p2.set(p1); void set(point p) { this.x = p.x; this.y = p.y;
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 p1 = new Point(); Point p2 = new Point(10, 20); Point p3 = new Point(p2); Point(int x, int y) { this.x = x; this.y = y;
this this. this. ( ) this( )
this class Point { int x; int y; this.x = x; this.y = y; this void set(int x, int y) { void set(point p) { this.x = p.x; this.y = p.y;
Point counter int 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
class Point { this int x; int y; static int counter = 0; 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(" 10 5 + SimpleCalc.getTriangleArea(10, 5) + " ");
class {
Java [ (1) ] [ [ (1) ] [ (3) ] new [ (3) ] int double [ (5) ] [ (4) ] [ (6) ] [ (6) ] ] (a) (b) (c) (d) (e) (f) (g)null (h) (i)
7
Java A B A B A B B A B A B A
Java Object
extends class A { A class B extends A { B A extends A
Object Object extends 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) A B B (2) (3) (4)
super. ( ); class Point { int x; int y; void printinfo() { // class ColorPoint extends Point { String color; void printinfo() { super.printinfo();
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,Y X 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,Y Y y0 = new Y(); Y y1 = new Y(10); Z z = new Z();
Point p = new Point(); ColorPoint cp = new ColorPoint(); Point ColorPoint Point cp = new ColorPoint();
class Person { void work() { // " " Person[] persons = new Person[3]; persons[0] = new Person(); class Student extends Person { persons[1] = new Student(); void work() { persons[2] = new Teacher(); // " " class Teacher extends Person { void work() { // " " void maketest() { for(int i = 0; i < 3; i++) { persons[i].work();
// 3 void workthreetimes(person p) { p.work(); p.work(); p.work(); Person new Teacher(), new Student()
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();
private private
private class Car { private int speed; // (Km/h) // speed 1 public void speedup() { if(speed < 80) { speed++; // speed 1 0 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;
final
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
abstract abstract class Shape { Shape Shape s = new Shape();
abstract abstract 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();
Java 1
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 { 1. A a = new A(); 2. B b = new B(); 3. C c = new C(); 4. I i = new I(); 5. A b = new B(); 6. B a = new A(); 7. I b = new B(); 8. I c = new C();