Java Lecture2 OVR

Embed Size (px)

Citation preview

  • 8/23/2019 Java Lecture2 OVR

    1/18

    1

    Java 2

    . .

    (Statement)

    (Expression)

  • 8/23/2019 Java Lecture2 OVR

    2/18

    2

    . .

    // , /* */

    !

    !

    ()

    ()

    (;)

    { } ()

    (;)

    . .

    , TAB

    C /

    string String !!!

    /

    : java.packetname.Classname

    : oneword(args), twoWords(args)

  • 8/23/2019 Java Lecture2 OVR

    3/18

    3

    . .

    ()

    : class _

    {public static void main(String[] arguments) { }

    }

    (web browser): public class _extends java.applet.Applet { }

    . .

    $ _ ,

    int x; int x, y, z;

    ( 0, /0, false, null) int x = 0;

    Integer x = new Integer(0); // int x = 0;

    Integer x;( x Integer)

    x = new Integer(0);( 0)

  • 8/23/2019 Java Lecture2 OVR

    4/18

    4

    . .

    - ( !)

    boolean true, false

    char a, b, ...

    byte 8bit (28 )

    short 16bit

    int 32bit

    long 64bit

    float 32bit

    double 64bit String Blah blah ( new!)

    .. Float, Character, Integer

    . .

    !!! Java :

    .. int, float

    .. Integer, Float

    ?

    .. System.out.println(Float.MAX_VALUE); - float

    .. System.out.println(Float.floatValue()); -

    . !

  • 8/23/2019 Java Lecture2 OVR

    5/18

    5

    . .

    (.. main)

    int x;

    System.out.println(x);

    !!!

    /

    . .

    typedefC

  • 8/23/2019 Java Lecture2 OVR

    6/18

    6

    . .

    /

    :

    int x[] = new int[];

    int[] x = new int[];

    / int x[][] = new int[][];

    int[][] x = new int[][];

    . .

    (new)

    int[] x = { 1, 2, 3 }; int[][] x = { { 1, 2, 3 },

    { 4, 5, 6 }{ 7 ,8 } };

    , int[][] x = new int[5][6]; int x[0][0] = 1; int x[0][1] = 2; ...

    length ..System.out.println(x.length);

  • 8/23/2019 Java Lecture2 OVR

    7/18

    7

    . .

    (? - )

    int x[] = new int[ ];

    int x[][] = new int[ ][];

    !

    int x[] = new int[ ]; ( !)

    int x[k] = new int[k-]; ..

    int x[][] = new int[6][];x[0] = new int[1];x[1] = new int[2]; ...

    x[0] System.out.println(x[0].length);

    . .

    class ArrayTest {public static void main (String arguments[]) {

    int[] test = new int[3];test[0] = 10;test[1] = 20;test[2] = 30;

    System.out.println( = + test[0] + ,

    + test[1] + , +test[2] + = + test.length);}

    : = 10, 20, 30 = 3

  • 8/23/2019 Java Lecture2 OVR

    8/18

    8

    . .

    T arguments main

    Strings ..

    public static void main(String[] arguments) { }java _arguments[0] arguments[1] ...

    (arguments)

    class ArgsTest {

    public static void main (String arguments[]) {

    System.out.println(arguments[0]);System.out.println(arguments[1]);

    System.out.println(arguments[2]);

    }

    : java ArgsTest first second third

    : first

    second

    third

    . .

    final : final double PI = 3.14

  • 8/23/2019 Java Lecture2 OVR

    9/18

    9

    . .

    final int MAX = 5;

    int[] myarray = new int[MAX];

    .

    . .

    (literals)

  • 8/23/2019 Java Lecture2 OVR

    10/18

    10

    . .

    (Integers) (Decimal)

    10, [0, 9] , .. 1, 2, -50, 400000

    (Octals) 8, [0, 7]

    .. 0777 (Hexadecimals)

    16, [0, 9] , , C, D, E, F .. 0x12, 0xAF

    . .

    (long)

    L l Long .. 5L, 100l, 0xFFL

    (float) double

    F ffloat

    .. 3.2F

  • 8/23/2019 Java Lecture2 OVR

    11/18

    11

    . .

    (char)

    ..

    : .. \n, \t, \b, \r, \f (form feed), \\, \, \, \0

    (Strings) .. Blah blah \n

    . .

    :

    (..1 ..2)

    (.. ..)

    (.. ..)

    (..1 ..2)

  • 8/23/2019 Java Lecture2 OVR

    12/18

    12

    . .

    ..1

    ..2 casting C

    .. int float: int x = 5; float y;

    y = (float)x;

    y = 5F y = x

    5

    .. float int: int x;

    float y = 3.14F; // F doublex = (int)y;

    3

    . .

    .. ..

    .. float Float

    float x = 5.8F;Float Y = new Float(x);

    .. float String float x = 5.8F;

    String S = String.valueOf(x);

    : new valueOf() String

  • 8/23/2019 Java Lecture2 OVR

    13/18

    13

    . .

    ..

    ..

    :

    .. Float float float x;

    Float Y = new Float(3.14);x = Y.floatValue();

    3.14

    .. Float int int x;

    Float Y = new Float(3.14);x = Y.intValue();

    3

    . .

    .. ..

    :

    .. String floatfloat x = Float.parseFloat(3.14);

    class prosthesi{

    public static void main(String a[]){

    float f1 = Float.parseFloat(a[0]);float f2 = Float.parseFloat(a[1]);System.out.println(f1+f2);

    }}

    : java prosthesi 2.5 3: 5.5

  • 8/23/2019 Java Lecture2 OVR

    14/18

    14

    . .

    ..1

    ..2

    .. Float Integer Float = new Float(3.14);

    Integer X = new Integer(Y.intValue());

    .. String Float Float F = Float.valueOf(3.14)

    . .

    ..1 ..2

    casting

    Emploee emp = new Emploee();VicePresident veep = new VicePresident();

    emp = veep;// cast ()

    veep = (VicePresident)emp;// cast ()

  • 8/23/2019 Java Lecture2 OVR

    15/18

    15

    . .

    : +, -, /, *, %

    : =, +=, -=, /=, *=, %=

    .. x = y = z = 4;

    / : ++, --

    x++ ++x

    : ==, !=, , = y = 5 x = y < 2 x =

    false

    . .

    : :

    && (AND), || (OR)

    bit:

    & (AND), | (OR), ! (NOT), ^ (XOR),>> (BIN SHIFT RIGHT), 5) && (y > 5) => z = falsez = (x > 5) & (y > 5) => z = false false

    z = 4 >> 2 => z = 1 (100 => 001)

  • 8/23/2019 Java Lecture2 OVR

    16/18

    16

    . .

    1. [ ], ( ) 10. |

    2. ++, --, ! 11. &&

    3. new, (cast) 12. ||

    4.*, /, % 13. ? : {.. z = (x>y)?2:8 }

    5. 14. =, +=, -=, /=, %=, =,|=

    6. , =

    7. ==, !=8. &

    9. ^

    . .

    java.lang ().

    import

    : Math.sqrt(4); - sqrt() = Math

    Math.cos(230); - cos() = Math

    Math.PI; - PI = Math

  • 8/23/2019 Java Lecture2 OVR

    17/18

    17

    . .

    :

    :String s1 = new String("Dimitris");

    String s2 = new String("Dimitris");

    System.out.println((s1==s2) + " " + (s1.equals(s2)));

    : false true

    ()String s1 = "Dimitris";String s2 = "Dimitris";

    System.out.println((s1==s2) + " " + (s1.equals(s2)));

    : true true

    . .

    (-)

    main public static void main(String args[])

    Java

    Java

  • 8/23/2019 Java Lecture2 OVR

    18/18

    . .

    .. 3.14, double. float 3.14F

    casting

    , java.lang, ..

    . .

    Math java.lang