BOOKC#

182
1 Microsoft Hellas Καθηγητού: Ι.-Χ. ΠΑΝΑΓΙΩΤΟΠΟΥΛΟΥ ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΕΙΡΑΙΩΣ ΠΕΙΡΑΙΑΣ 2003

description

book c to java

Transcript of BOOKC#

  • 1

    Microsoft Hellas

    : .-.

    2003

  • 2

    ................................................................................. 2 ........................................................................ 5 1. ........................ 6

    1.1 ...................... 6 1.2 ................. 8 1.3 ........................ 8 1.4 .................................................... 10 1.5 ................... 10 1.6 ................... 12 1.7 .............................................................................. 15

    2. JAVA C# ..................................................... 16 2.1 ......................................... 16

    2.1.1 . FRAMEWORK ....................................... 16 2.1.2 ..................... 17 ............................................................................. 17 2.1.3 .............................. 17 2.1.4 .......................... 18 2.1.5 ................................. 18

    2.2 - .................................... 21 2.2.1 ................................ 21

    2.3 ...................................... 24 2.3.1 ................................................. 24 2.3.2 ...................................................... 25 2.3.2.1 .................................................................... 26 2.3.3 ............................................. 34 2.3.4 .............................................................. 36 2.3.5 (SEPARATORS) ........................................... 36

    2.4 .............................................................................. 37 3. .................................................................................. 39

    3.1 .............................................................................. 39 3.2 ....................................................... 42 3.3 ........................................................... 43 3.4 ............................................................ 45 3.5 ............................................................... 47 3.6 ........................................................ 50 3.7 .............................................................. 51 3.8 ................................................. 53 3.9 ................................................................ 53 3.10 ............................................................................ 55

    4. .................................................................................... 57 4.1 .............................................................................. 57

    Java .......................................................................................... 57 C# ............................................................................................. 57 ....................................................................................... 57

  • 3

    4.2 ..................................................... 58 4.3 H IFELSE ............................................................. 58 4.4 .............................................. 59 4.5 WHILE DO-WHILE ...................................... 60 4.6 FOR ..................................................................... 61 4.7 BREAK, GOTO................................................... 62 4.8 CONTINUE ........................................................... 64 4.9 SWITCH ............................................................... 64 4.10 FOREACH .......................................................... 66 4.11 C#..................................................... 67 4.12 ............................................................................ 67

    5. ..................................................... 69 5.1 ..................................................................... 69 5.2 ............................................. 71 5.3 ThreadStart .............. 74 5.4 ..................................................................... 75

    5.4.1 .................... 77 5.5 .............................................................................. 80

    6. ................................................................................ 83 6.1 .............................................................................. 83 6.2 ................................ 83 6.3 ...................................... 88

    ....................... 88 Java .......................................................................................... 88 C# ............................................................................................. 88 ....................................................................................... 88

    6.4 ............................................. 91 6.5 ...................................................... 94 6.6 ................................... 95

    Java .......................................................................................... 95 C# ............................................................................................. 95 ....................................................................................... 95

    6.7 CHECKED UNCHECKED ............................ 97 6.8 .............................................................................. 98

    7. ........................................... 100 7.1 ............................................................................ 100 7.2 ......................................................... 101 7.3 ........................................................ 102 7.4 ............................................................... 105 7.5 .................................... 108 7.6 CONSTRUCTORS ................................................................. 110 7.7 ................................................................. 113 7.8 ............................................................................ 113

    8. - ........................................ 115 8.1 .................................................... 115 8.2 ............................................................. 116 8.3 ........................................... 118 8.4 ....................................................... 119 8.5 .................................................. 122

  • 4

    8.6 ...................................................... 124 8.7 & ........... 125 8.8 ............................. 126 8.9 C# .......................................... 126

    8.9.1 java.lang .................................................. 127 8.9.2 java.math ................................................. 128 8.9.3 java.net ...................................................... 128 8.9.4 java.sql....................................................... 129 8.9.5 java.text ..................................................... 130 8.9.6 java.util ...................................................... 130 8.9.7 java.util.regex ............................................. 131 8.9.8 javax.swing ................................................ 132 8.9.9 java.awt ..................................................... 134 8.9.10 java.awt.event .......................................... 136 8.9.11 java.awt.print ........................................... 137 8.9.12 java.io ...................................................... 137 8.9.13 ............................... 138

    8.10 .......................................................................... 138 9. .. .......................................................................... 140

    9.1 ............................................................................ 140 9.2 .................................... 140 9.3 ...................................................... 141

    9.3.1 - ........................... 142 9.4 ...................................................... 145 9.5 BYTES ........................................................... 146 9.6 I/O .............................................................. 147 9.7 ............................................ 147 9.8 - ...................................... 149 9.8.1 ................................................... 150 9.8.2 ........................................................ 151 9.8.3 ................................. 151 9.9 ............................................ 153 9.10 SERIALIZATION.................................................................. 154 9.11 .......................................................................... 155

    10. ................................................... 159 10.1 .......................................................................... 159 10.2 ..................................................................... 161 10.3 .......................................................................... 170

    11. C# .................................................. 171 11.1 .......................................................................... 171 11.2 Data Provider ..................................................................... 171 11.3 .......................................................................... 175

    12. C# ........................................................... 177 12.1 .......................................................................... 177 12.2 C# XML........................................................................ 177 12.3 C# KAI ................................................................... 180 12.4 C# KAI WEB-SERVICES ..................................................... 181 12.5 .......................................................................... 182

  • 5

    JAVA (2++) C# Microsoft. . JAVA C# . Microsoft Hellas -. . . . , , JAVA, , C#, Microsoft, desktop , web smart phones PDA s. Microsoft . , 2003

    -.

  • 6

    1o 1. 1.1

    50 , (compiler) obj (object). mainframes obj (exe,com ) .

    3 , COBOL, FORTRAN RPG. COBOL - , . FORTRAN - . RPG , , .

    70 software (software crisis) . COBOL COBOL 74, FORTRAN , . FORTRAN II, IV, V, 77), RPG . ALGOL 4, PASCAL, ADA, BASIC, COBOL 74, FORTRAN 77. PASCAL TURBO PASCAL, ADA , BASIC Home computers .

    10 1983 Microsoft Microsoft Windows, MS-DOS, PC. Windows 1.0 (1985), 2.0 (1987), 3.0 (1990), 3.11 (1993), NT (1993), 95 (1995), NT4 (1996), 98 (1998), ME (2000), 2000 (2000), XP (2001), 2003 (2003). 1990 2000 visual programming Visual Basic Microsoft.

  • 7

    COBOL

    80. 90 VISUAL COBOL 2000 . FORTRAN FORTRAN 77 FORTRAN 90 VISUAL FORTRAN ++, MICROSOFT DIGITAL. , Object Oriented Programming . FORTRAN. Visual Basic object oriented . . Visual Basic Microsoft Access, 1992. ADA VISUAL ADA, ... TURBO PASCAL BORLAND DELPHI . DELPHI .

    .

  • 8

    , .

    1.2

    60 low-level Master Control Program (MCP). FORTRAN . 1965 Cambridge BCPL ASSEMBLY.

    H BCPL

    UNIX PDP-7/11 DEC. / , . Dennis Ritchie 1972 C ANSI Standard C UNIX.

    80 MICROSOFT C UNIX

    MS-C. MS-C C UNIX . , (.. PASCAL, ADA, FORTRAN) (.. , x, binary , ). C , , C++, JAVA, C#, .

    C C++ ANSI

    90,( UML). H C++ , input/output(iostream) . 1993 MICROSOFT studios MS VISUAL C++ . RDBMS, LAN(Local Area Network), Web , (. JAVA, C#, XML ).

    C++/VISUAL C++ VISUAL FORTRAN. . 90 .

    1.3

    Microsoft Windows, Internet

  • 9

    World Wide Web (WWW) :

    (portability) , , , . To 1991 SUN J. Gosling, E. Frank, M. Sheridan, C.

    Warth ( , ...). 1995 JAVA , Internet Multimedia . 1990 im Berners-Lee, CERN, (Hypertext). HyperText Markup Language (HTML) HyperText Transport Protocol (HTTP). Internet Web. , HTML , (browsers). Web programming :

    , - , (multimedia), (, ) JAVA .

    (mixed language programming). (Huge distributed software systems). JAVA Microsoft Windows Virtual Java Machine SUN. Microsoft Virtual Machine JAVA . Virtual Machine Microsoft Visual J++ JDK 1.2.1. .NET J++ J# (java syntax language) C# runtime VM. C# Anders Hejlsberg, .

    C# C, C++ Java,

    . C# C, C++ Java . C# Java .

  • 10

    C# C++

    JAVA JAVA C++, C# .

    1.4

    : (Package), *.exe/com/bat browser. 1>2>3>4, ... : 1. 2. 3. 4. , , . 1 2 3 4 FORTRAN 2 2 3 1 8

    ADA 3 2 3 1 9 PASCAL 2 2 3 1 8

    C++ 3 3 2 2 10 JAVA\C# 1 2 2 3 8

    11 11 13 8

    [ 1, 2, 3 ]

    JAVA\C# (interpreter) JAVA JIT ( Just In Time compilers) , C# . exe 4 . 1.5

    JAVA, C#, C++:

  • 11

    JAVA C++ Smalltalk, FORTRAN, Eiffel, ADA, LISP, MESA Cedar. COBOL, RPG. To 70% JAVA VISUAL C++. C# JAVA, C, C++. .

    JAVA . pointers C# . C# JAVA .

    JAVA C# . server clients Internet.

    . . Object.

    JAVA JAVA (JAVA Virtual Machine). JAVA *.class byte codes. JIT (Just In Time compiler). compiler JAVA CPU. . CLR .NET . C# , . , Microsoft (Microsoft Intermediate Language - MSIL), (CPU). MSIL (assembly language). CLR Java Visual Machine, CLR .

    JAVA C# .

  • 12

    (byte codes verifier) (memory management mistakes) (object- oriented exception handling).

    . . JAVA , C# . Framework .

    Java C# 20 50% 20% C++. . (multithreaded) object. JIT (Just In Time Compilers), , , .

    JAVA C# (multimedia), . (web-RDBMS) (JAVA Database Connectivity JDBC) ADO.NET JAVA C# . Java Beans Graphics UI (User Interface) Java C# .

    1.6

    , , JAVA C# , C++. (object-oriented) , , (objects), . , . :

  • 13

    :

    :

    .

    : .

    ,

    , (Class). type () PASCAL . , (instance) .

    (field),

    . , (, , .), .

    (Encapsulation) (Information Hiding) .

    ,

    (Association) . , . , (Role) . , .

    (Inheritance) (Generalization) . , . - , -. - - , :

    : ( ) :

    -

    , -. ,

  • 14

    , - .

    , -

    , -, . , 1 (Inheritance of order 1). , , (Inheritance of order K). JAVA C# () =1. Visual J++ Microsoft, C#.

    (Aggregation), . , (recovery systems), .

    , (Abstraction), , , . , , .

    ,

    . 1990 (Booch, Jacobson, Rumbaugh) . , UML (Unified Modeling Language), ( 1999 UML 1.3 ) OMG (Object Management Group: www.omg.org) .

    , , ! . , RECORD PASCAL / (CASE OF). TYPE PASCAL STRUCT C, . . functional Abstraction, .

  • 15

    (data abstraction) . , (Knowledge Engineering), data () () . , , .

    1.7

    1. Cobol, Fortran RPG .

    2.

    Java C#:

    3. ;

    4.

    .

    5. Java C#

    6.

    ; :

  • 16

    2o

    2. JAVA C# 2.1

    , Java, linking, JVM (java Virtual Machine), Bytecode Verifier. , Runtime System object . Java, , C# compilation Intermediate , , compilation . , .NET. , .NET - CLS (Common Language Specification). , Assembly. , Garbage Collectr . 2.1.1 . FRAMEWORK

    .NET Framework - components (highly-distributed component-based applications). , , Windows.

    C#, .NET Framework . (Common Language Runtime - CLR). . , CLR .NET Framework , .

    .NET (class library).

    (runtime environment). , I/O ( / ), , . features .NET, .NET . C# .NET .NET.

  • 17

    2.1.2

    CLR .NET . C# , . , Microsoft (Microsoft Intermediate Language - MSIL), (CPU). MSIL (assembly language).

    CLR MSIL . , MSIL CLR. .NET Framework .

    Microsoft (MSIL) JIT (Just-In-Time). : .NET , CLR JIT . JIT MSIL (native code) . , C# MSIL. , MSIL.

    , MSIL

    C# : metadata. metadata . Metadata MSIL. 2.1.3

    , C# , (managed code). CLR . CLR, . C#. MSIL CLR .NET Framework. , , , components .

  • 18

    (unmanaged code) CLR. .NET Framework . , C# .

    2.1.4

    CMS (Common Language Specification) CLR .NET . CMS CTS (Common Type System) .NET source .NET . 2.1.5 . JAVA ( X.java) Compiler JAVA, javac :

    javac X.java

    , bytecode ( X.class), .

    (bytecode),

    JAVA (JVM: Java Virtual Machine) JVM. :

    java X

    Linking

    , .

  • 19

    (Class Loader) JAVA, ( JDK ). (bytecode verifier), . , (Interpreter) JAVA, ,

    (JIT: Just-In-Time Compiler)

    . (Runtime System) JVM , .

    : JAVA X JAVA

    (CLASS LOADER)

    (BYTE CODE VERIFIER)

    JAVA INTERPRETER

    A A (JIT)

    (RUNTIME SYSTEM)

    OPERATING SYSTEM

    / HARDWARE

    JAVA (JVM)

  • 20

    , , C# , , IL ( ). , MyFile.cs, :

    csc MyFile.cs MyFile.cs MyFile.exe. , MyFile

    ASSEMBLY IL CODE

    JIT COMPILATION SECURITY PERMISSIONS GRANTED MEMORY TYPE CHECKED APP DOMAIN

    ASSEMBLIES LOADED

    GARBAGE COLLECTOR

    COM SERVICES

    CODE EXECUTES HERE

  • 21

    2.2 - 2.2.1

    JAVA C#. Java, Editor (.., Notepad MS-Windows) , Java001, Java001.java.

    /* Java001 . JAVA*/ // java Java001 class Java001 { public static void main(String args[]) { System.out.print("YOU GAVE THE PASSWORD ===> "); System.out.println(args[0]); if (args[0].startsWith("JAVA")) System.out.print("O.K. you gave correct" + "

    Password ... "); else System.out.print("I do not know you ! "); } }

    , C#. using System; namespace Password { class CPassword { [STAThread] static void Main(string[] args) { // TODO: Add code to start application here Console.Write("You Gave the Password===>"); Console.WriteLine(args[0]); if (args[0].StartsWith("JAVA")) Console.Write("Ok, You Gave Correct Password"); else Console.Write("I do not Know You");}}}

  • 22

    , JAVA,

    main. , Java001.java, java, JAVA C#, , , . , .

    ,

    , . , o (Command Line) main. :

    YOU GAVE THE PASSWORD ===> JAVAPassword O.K. you gave correct Password

    ( JAVA), (Input) . :

    JAVA-C# o . Java001 java001 Password, password , .

    JAVA main C/C++, JAVA () , Java001 . C# main . , . , Java main

    public static void main(String[] args)

    Main C# int void

    string arguments (). , Main:

    static void Main() static void Main(String[] args) static int Main() static int Main(String[] args)

  • 23

    1. public Main C#. (runtime) .

    2. int (termination static code) . Java Runtime.getRuntime.exit(int) System.exit(int).

    { (curly brace)

    }, , , , ., ( C++) ; (semicolon).

    (comments) , :

    /* */ , : // / .

    Write ( WriteLine ),

    Console ( stdout C/C++), System, C#. :

    System.Console.WriteLine ("") if-else,

    . C# , if , else. args[0] () string JAVA StartsWith , else.

  • 24

    2.3

    C# (Lexical Issues) (Keywords), Java. (Operators), . 2.3.1

    (names, identifiers) , , . (0,1,2,,9), ( ), underscore (_) ($). , (case-sensitive) (.. Some some ). :

    SPEED Speed speed $param param$ C4 a_long_name_variable last_value

    , :

    3ij iff(3) jcp.some class if

    cost-of-unit-in-$ Non/yes_variable null ,

    C#. - . , (keywords) C#. , C# true, false null, , C#,

    [ Modifier] DataType Identifier; C# , Java,

    public int i; , :

    int i=10;

  • 25

    2.3.2

    C# Java. C# (unsigned) (integer) decimal. decimal (high-precision fixed-point) .

    .NET System. , C# (keywords) . keywords . - Java. Java

    C# keywords

    .NET

    String string System.String Boolean Bool System.Boolean true false false Byte Sbyte System.Sbyte 8-bit

    (-128 127) 0

    (N/A) Byte System.Byte 8-bit (0 255)

    0

    Short Short System.Int16 16-bit (-32768 32767)

    0

    (N/A) ushort System.Uint16 16-bit (0 65535)

    0

    Int Int System.Int32 32-bit (-2147483648 2147483647)

    0

    (N/A) Uint System.Uint32 32-bit (0 4294967295)

    0

    Long Long System.Int64 64-bit (-9223372036854775808 9223372036854775807)

    0

    (N/A) ulong System.UInt64

    64-bit (0 18446744073709551615)

    0

  • 26

    Float Float System.Single 32-bit

    0

    Double double System.Double 64-bit

    0

    (N/A) decimal System.Decimal 128-bit 28

    \u0000

    Char Char System.Char 2-bit Unicode 0 2.3.2.1

    C# , , , . (constants literals) , (suffix) bits . 2.3.2.1.1 E E

    C# . , 8-bits, byte short 16-bits, 32-bits , , , (word size) 32-bits!

    , long suffix L ( l), int. , JAVA 150 150L, , . 32-bits 2 4 bytes, 8 bytes 2.3.2.1.2

    ( ) ( + ), (.). -754 e ( ). C# :

    (tandard notation): pn.m, p:

    ( + ), n: m: (scientific notation):

    pn.mEsi = pn.m*10si, p: , n: , m: , s: ( + ), i: ( e): (10).

  • 27

    , C#

    ( double) 64-bits, suffix F ( f), 32-bits, float.

    C#

    :

    3.14159 double (64-bits) 3.141597 float (32-bits) 0.077e2 double, 0.077*102 33-15F float, 33*10-15 3+200 double, 3*10200 -31459-05 double, -314159*10-5

    , float double, . 2.3.2.1.3

    Java, C#, 16-bits , Unicode. , ASCII 0 127 ISO-Latin-1 0 255. , Unicode , , , . 8-bits 16-bits!

    ( ASCII) (escape sequence) . '\141' '\u0061'.

    C#

    '\n', C#, (new line). , backslash (\) (.. '\141' 4 ). Unicode. , , .

    , C#

    88 , 'X', , , ''

  • 28

    C#

    /

    \n (LF) \r (CR) \0 \' (') \" (") \\ Backslash (\) \f form feed \t tab \b backspace \a (alert) \uyyyy (yyyy)

    2.3.2.1.4

    C# (Bool), true false. C# , if. , Write boolean, true false. , 1 0, , . 2.3.2.1.5 (STRINGS)

    Unicode , (") C# (String), . , C# strings, (string manipulation). string, Unicode, C#, , .

    string literals,

    : : \n ==>

  • 29

    \7431\ : \ua432! ******** , , strings

    C# string (line continuation). string 2.3.2.1.6

    final C# ,

    ReadOnly Sealed . , (Inheritance), Sealed . 2.3.2.1.7.

    (array) C# , , . , , [ ]. ( ) . C#, (subscripts) .

    . , . :

    __ _ [ ] [ ][ ];

    _=new __ [max1] [max2][maxk];

    , :

    __ _ [ ] [ ][ ]= new __ [max1] [max2] [maxk];

    , [ ], ( ):

    __ [] [][] _=

    new __ [max1] [max2][maxk];

    , new, , , , bytes :

    ram(_) = ram (__)*max1**maxk

  • 30

    maxi i ram(x) bytes x. , new , C#, , .

    :

    int products[];

    products = new int[1000]; float profits[] = new float[1000];

    double costs[][] = new double[100][50]; short[][][] other = new short[10][20][30];

    products 1000 int . profits 1000 float . costs double, 100x50, other short , 10x20x30.

    C#, :

    _ [i1] [i2][ik];

    , C# :

    products[999],profits[0],costs[0][49],other[1][2][3] :

    products[1000],profits[-3],costs[101][49],other[0][0][31]

    ,

    new, , . , :

    double matrix[]=new double[10];

    matrix :

    matrix[0], matrix[1],, matrix[9] , 0.0, 64-bits . , C#, Java ( ),

  • 31

    (array initializer) new, ( , ):

    __ _ [ ] = { r0, r1, , rmax1-1}; // __ _ [ ] [ ] = { { 00 , 01 , , 0 1-max2 } { 10 , 11 , , 1 1-max2 }, { 1-max10 , 1-max11 , , 1-max1 1-max2 } } //

    max1, max2 () () . ri [i] , ij [i][j] . ;

    int months[]={31,28,31,30,31,30,31,31,30,31,30,31}; boolean yes_no[] = {false, true, true, false};

    double values[] = {1.2*4.5, 2.3, 1.3*4.2,-1.0*4.0}; int twodim[][] = {{1,2,3},{5,4,6},{7,8,9},{0,1,0}};

    months () , yes_no 4 , values 4 double, (), twodim 4x3 :

    1 2 3 [0][0] [0][1] [0][2] 5 4 6 [1][0] [1][1] [1][2] 7 8 9 [2][0] [2][1] [2][2] 0 1 0 [3][0] [3][1] [3][2]

    C#, Java,

    . , twodim :

    1 2 3 5 4 6 7 8 9 0 1 0

    (12*32) bits.

    C# . , . ,

  • 32

    . , matrixd :

    int matrixd[][] = new int[5][6]; // : int matrixd[][] = new int[5][]; matrixdp[0] = new int[6]; matrixdp[1] = new int[6]; matrixdp[2] = new int[6]; matrixdp[3] = new int[6]; matrixdp[4] = new int[6]; ,

    . C# (irregular arrays), . :

    int matrixd[][] = new int[5][]; matrixdp[0] = new int[6]; matrixdp[1] = new int[5]; matrixdp[2] = new int[4]; matrixdp[3] = new int[3]; matrixdp[4] = new int[7];

    , (.., 7 , 6). , , . 2.3.2.1.8 STRING

    C# (strings), . strings. string C#, , . , , . , , string, .

    (literals) Unicode . C# :

  • 33

    string, , , . :

    string _= string;

    , , :

    string example = " "; System.Console.WriteLine(example); System.Console.WriteLine (" ");

    , string, length string. 2.3.2.1.9 STRUCT struct, constructor, , , , , . struct, , .

    struct (stack) (heap) .

    struct , .

    structs ,

    , struct.

    public struct FileInfo { public long id; public string Name; public string LastName; public decimal Account; } FileInfo File1; File1= new FileInfo(); File1.id=10; File1.Name=John; File1.LastName=Newton; File1.Account=50;

    , , .

  • 34

    2.3.2.1.10 ENUMERATION enumeration, . enumeration

    enum month {January, February, March};

    enumeration int . :

    enum month {January=1, February, March}; ,

    . enum month {January=31, February=28, March=31};

    2.3.3

    , C# / (functions/subroutines) . , , , , , . ain() ! .

    C# (block), curly brace ( { ) (}). block , block, (scope) . C#, . , , (Lifetime) (Visibility) :

    . (Local) (Global) . block , block, . , ,

  • 35

    .

    block block. , . , , block block, block. blocks

    (nesting ). , :

    { int x; x=3; if (x==3) { int z=4; System.Console.WriteLine("x=" + x + "z=" + z); } x=x*z; // z. System.Console.WriteLine("x="+x); }

    block, , block .

    C# . , ( ) (declaration part). , :

    { int x,y=3; x=y*z; // z. int z=1; }

    z , .

    , , JAVA, C# blocks! :

  • 36

    { int x=1; {int x=2; //, . } }

    x block, C++, JAVA, C#. x , x 2.3.4 C#. Abstract Do implicit params switch As Double in private this Base else int protected through Bool enum interface public true break event internal readonly try byte Explicit is ref typeof case Extern lock return uint catch false long sbyte ulong char finally namespace seald unchecked Checked fixed new Sort unsafe class float null sizeof usort const for object stackalloc using Continue foreach operator static virtual Decimal goto out string void Default If override struct while Delegate 2.3.5 (SEPARATORS)

    C# (Separators). (;) (semicolon), C# . , .

    C#

    ; Semicolon . . Period -

    . , Comma

  • 37

    . ( ) Parentheses ,

    , .

    [ ] Brackets .

    { } Braces .

    2.4 1.

    Java C#; 2.

    Java C#. 3. , .

    ; . 1

    using System; namespace Con2 { public class Class1 { int j=14; static void Main(string[] args) { int i=12; int j=15 for( int i=1;i

  • 38

    static void Main(string[] args) { int i=12; int j=15 int [] int=new int [-30]; string []agg=new string[(50)]; for( int i=1;i

  • 39

    3o 3. 3.1

    C++ (operators) , , . Java C# C++, - , x ( ), Pascal, C++, JAVA C#! C++. JAVA C# .

    (operators)

    :

    (unary): . r1, r2 x, , : r1r2x = r1(r2x) (binary): , . JAVA C#. (ternary): (conditional operator), .

    , . , :

    (arithmetic operators) bits (bitwise operators) (relational operators) (logical operators) (specific operators)

    Boolean. . true false .

  • 40

    false true. , JAVA C# (.., ) . , , ( ) [ ] .

    JAVA,

    C# , . JAVA/C# Java C# ( ) ( )

    ()

    () ()

    [] [] . . ++ ++

    1 -- --

    1 ++ ++

    1 -- --

    1 + +

    ( ) ()

    - - ( )

    ~ ~

    ! !

    New () new ()

    New [] new []

    (type) (type)

    * * / / % % + +

  • 41

    string+string string+string

    - - >>x >>x

    bits x > ()

    >= >= ()

  • 42

    %= %= =%

    &= &= =&

    |= |= =|

    ^= ^= =^

    >>>= (N/A) =>>>

    3.2

    . , Boolean , char , int.

    +, -, *, /

    , () , x=a x=-a. + (), , () :

    string1 + string2, .., "I " + "love" + " JAVA!" . , :

    int x=2; int y=2*x; int z=y/3; double w=y/3;

    x = 2, y = 4, z = 1 w = 1.333. % () modulo ,

    , , C++ (!):

    x mod y .

    = x % y , :

    int x = 20; double y = 19.3; Console.WriteLine(x%8 + ',' + y%8);

  • 43

    4 3.3 x y 8, .

    x++, x--

    ++x, --x 1 () . , x++ x--, x. , , ++x --x, 1. , : a = ++x, b = --x, c = x++ d = x-- , :

    x = x +1; x = x - 1; c = x; d = x; a = x; b = x; x = x + 1; x = x - 1;

    :

    x = 10; y = 10; Console.WriteLine(x++); Console.WriteLine(++y);

    : 10 11

    3.3

    ( ) (relational operators) . :

    a < b a b a > b a b a = b a b == b a b a != b a b

    true false,

    bool. a b (==, !=), , , , (, =), a, b , . , :

    int x = 1;

  • 44

    int y = 2; bool z = x != y; Console.WriteLine(z);

    true. , , . , x==y!=z, x, y , z bool, (!), , , (x==z)!=z, true false, z .

    , Java,

    C#, false . , C++ , C#, JAVA:

    int x = 1; if (x) x = 0; //

    JAVA /C# :

    int x = 1; if (x != 0) x = 0;

    (==, !=)

    , , (instance). , (strings). , :

    string s1 = "ZERO"; string s2 = "ZERO"; Console.WriteLine (s1==s2); string s3 = new string("ZERO"); string s4 = new string("ZERO"); Console.WriteLine (s3==s4); s3 = s4; Console.WriteLine (s3==s4);

    : true false true

    s1, s2 , s3, s4, , == false, , .

  • 45

    , s3 s4 == true. strings C# (.., equals()) . 3.4

    () (Bitwise operators) long, int, short, char byte, , bits . :

    ~a ( ) a & b AND ( ) a | b OR ( ) a b OR (

    )

    ~ . 8-bits 7 00000111 11111000, 248. , :

    byte a = 7; Console.WriteLine (~a);

    248, a 00000111. a1a2am :

    a1a2am =

    =

    1m

    0i

    ii 2a

    11111000 : (23+24+25+26+27) = 248 a . C# 2 (twos complement). , 1. , -7 00000111, 11111000 1, 11111010. -7, 11111010, 00000101 1, 00000111 7.

    A B a|b A&b ab ~a 0 0 0 0 0 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 1 0 0

  • 46

    AND &

    :

    bit i a 1 bit i b 1, bit i a&b 1, , 0. i .

    :

    A = 53 = 00110101

    B = 102 = 01100111

    &: a&b = 00100101 37.

    OR, |

    :

    bit i a 0 bit i b 0, bit i a|b 0, , 1. i .

    , a = 53 b = 102, :

    a|b = 01110111, 119.

    OR, :

    bit a bit i b, bit i ab 0, , 1. i .

    a = 53 b = 102, :

    an = 01010010, 92

  • 47

    bit i (1) (0), i (.. ~0), :

    ~ & | ( )

    bits:

    short i = 2, j = 5; short z = (i&j)|(ij); Console.WriteLine ("z =" + z);

    , short i j 2 5 , ; 0000000000000010 0000000000000101 , short z. |, . (i & j), : 00000000000000000 , (i j), : 0000000000000111 , |, : 0000000000000111 7. :

    z = 7 3.5

    (boolean logical operators) bool. bool . :

    a&b AND a|b OR ab OR (XOR) !a ( ) a&&b AND a||b OR

  • 48

    &, |

    bits 0 1, false true, . .

    ! (), . , :

    if(!e) e=true; e false, e true.

    && AND (logical AND),

    (short-circuit) & . , a false, false. .

    y = a&&b

    a = false,

    y = false

    b = false,

    y = false

    y = true

    || OR (logical OR),

    (short-circuit), | . , , a true, true ( ).

    y = a||b

  • 49

    a = true,

    y = true

    b = true,

    y = true

    y = false

    :

    if((x==y)&&(z>=3.33)) w=1; //1 if((p==0)||(b[i]>b[j])) w=1; //1 if((e&&f)||(r!=g(k,z)) w=1; //1 1, x y,

    z 3.33, w 1. , 4 . x, y z 3.33, :

    if((x==y)&(z>=3.33)) w=1; //4 2, p , w=1

    . , b[i]>b[j], w=1. 5 6:

    if((p==0)|(b[i]>b[j])) w=1; //5 if((p==0)^(b[i]>b[j])) w=1; //6

    5 p==0 b[i]>b[j]. , w=1. 6, , w=1.

    3, e f true, w=1. , r g. , w=1.

    , &, && bits, bool, . , && . , |, ||. :

  • 50

    &&, || &, | bool .

    &&, || , &, | . , . :

    bool x = true; bool y = false; bool z = (x&y)|(x&!y); bool w = (x&&y)||(x&&!y);

    z, w (true) , :

    bool t = (!x&y)|(x&!y); bool f = (!x&&y)||(x&&!y);

    f t, t, f true. 3.6

    . , , , (Assignment). , (statement) . C++ , JAVA, C#. ( ) (=). , :

    = (1) . , , , . , , . , bits , , .

    , ( bits), :

  • 51

    a=b a+=b A=a+b a-=b A=ab a*=b A=a*b a/=b A=a/b a%=b A=a%b a&=b A=a&b a|=b A=a|b a=b A=ab

    , : = (2) (1) (2) , (1) . :

    x + = y; :

    x = x + y; , , .

    C# JAVA, - . :

    1 = 2 = = k = ; , ( ) k, .. 1. , :

    int a, b, c, d; a = b = c = d = 10;

    10, d, c, b a. 3.7 ( ) a , bits, b. :

    ab bits a b ab

  • 52

    , , a int long, (0-31) (0-63), . , , b. a3;

    a : 00000000000000000000000000100011 , b : 00000000000000000000000000000100. , , . :

    int x=2; int y=3; x>>=1; y

  • 53

    x,y 1 6 . 3.8 Java C# (ternary) , , (conditional operator), : ? : , true, B, . , . , : (2+1!=4)?117:++ true, 117, . , . : minab =min(a,b), : minab =a

  • 54

    long y=x; y x, 32bits 64 bits, 123456789L. , , : _ _=(_) __ : int x=117; byte y=(byte)x; 117 y, 8bits 32bits x. Java C# new. : _ _ =new _ (__) . , , new. :

    class zero{ int x; double y; byte z;} class demo{ public static void main (String [] args){ zero object=new zero(); object.x=114; object.x=117.114; object.z=(byte)object.x;}}

    object, x, y, z 114, 117.114, 114 (32 +64+8) bits. . , X Y, X Y , . Java

  • 55

    instanceof . C# is :

    is , true, false. : a=new A(); B b=new B(); C c=new C(); D d=new D(); :

    bool x1, x2,x3,x4,x5,x6,x7; x1=a is A; x2=b is B; x3=c is C; x4=d is D; x5=c is A; x6=d is B; x7=c is B;

    x7 false, c C . 3.10

    1. N C#, 50x40, .

    2. .

    using System; namespace Csoper { class Class1

    D C

  • 56

    { [STAThread] static void Main(string[] args) { int i; int j; for(i=1;i

  • 57

    4o 4. 4.1

    JAVA C# C# , foreach goto, C# Continue Break label JAVA. break continue JAVA goto foreach (). JAVA C# (statements) (control flow), (loops), , . : Java C# If...else If...else . Switch...case switch...case . For For loop Do Do Loop . While While Loop . - Foreach . - Goto Java goto

    . (label) .

    continue(label) continue loop. Break(label) Break loop. Return Return compound statement ( ), ( ) (expression statement).

    (exception handling statements) try, catch .

    (keywords), , , . (body), ( nesting). , , . JAVA C# break, continue, goto , JAVA (/),

  • 58

    . C#.

    4.2

    ,

    . ; , . , , , , , . (assignment statement) :

    _ = ;

    . .

    x++; y=(y+x-2); a.f(y);

    4.3 H IFELSE

    ifelse

    . : If (condition) { ..1; } Else { ...2;}

    if 1, 2. else . JAVA {...}. . if (nested ifs) elseif. :

    If (condition) { ..1; } Else if { ...2;}

  • 59

    if :

    using System; class iftest { static void Main(string[] args) { string MyName = "Periklis"; string herName = "Aspasia"; Bool married = true; Console.WriteLine("My name is " + MyName); if(married==true) Console.WriteLine("and my wife is" + herName + "."); } }

    4.4

    (iteration statement) , , (escape statement) . (loops) , . . (), . () . , .

    , .

    While Do-while For

    while, do-while, for , .

  • 60

    , , , . . , , , . . , , . , , (run-time-errors), , , , . 4.5 WHILE DO-WHILE

    : While () { ...; } true, while, . :

    while (i

  • 61

    . : Do { ...; } while(); .

    While () ;

    , . while do-while

    If() Do ; While();

    do-while, if. 4.6 FOR

    for, (control variable) (initialization), , . for : for (; ; ;) { ; }

    for , I, :

    using System; namespace ConsoleApplication3 { class Class1 { static void Main(string[] args) { int i=0;

  • 62

    for (i=1;i

  • 63

    for(step=1;step 6) break theBlock; } System.out.println(end of program); } } C#, break .

    switch. block goto .

    C# goto (label). C# goto JAVA. goto , label . Java goto , break continue, . goto :

    goto try catch finally.

    goto finally

    C#.

    using System; class LabelTest { static void Main(string[] args) { int step; theBlock: for (step=1;step 6) goto theBlock; } Console.WriteLine("end of program"); } } .

    . Java :

  • 64

    step is 1 step is 2 step is 3 step is 4 step is 5 step is 6 step is 7 end of program

    C# , step is 1 step is 7 . goto break . 4.8 CONTINUE

    break .

    4.9 SWITCH

    , , (Iteration). continue.

    (.. ) (multi-way decision making). , Pascal CASE-OF, (.., Basic, Fortran, C++), .

    C#, ,

    switch, C++, , :

    : loop C#

    Ctrl BreakPause

    . Alt, Ctrl Delete Task Manager tab Applications End Task .

  • 65

    switch ( ) { case 1: 1; case 2: 2; case k: k; default: (k+1); } , byte short int char, i (literals) . i, w, w=i, i+1, , k, , (k+1) default, switch. i switch, switch. , cases, ( { }), , cases (labels) break. , , month ():

    int month = 3; string season; switch (month) { case 9: case 10: case 11: season = "Autumn"; break; case 12: case 1: case 2: season = "Winter"; break; case 3: case 4: case 5: season = "Spring"; break; case 6: case 7: case 8: season = "Summer";

  • 66

    break; default: season = "Unknown!"; } Console.WriteLine(the month:"+month+" is in "+season); break

    , , Summer, Spring month 3. , cases , , switch.

    , ,

    case , case. , (.. Pascal). , C++ JAVA, ( ), , break, , default. switch switch/case-of CASE OF Pascal. , switch :

    switch ( ) { case 1: 1; break; case 2: 2; break; case k: k; break; default: (k+1); }

    4.10 FOREACH C# foreach . foreach, :

    System.IEnumerable. System.Array IEnumerable. IEnumerable.

  • 67

    foreach : foreach ( in ) { ; } foreach string :

    string[] days = new string[] {"Monday", "Tuesday", "Wednesday",

    "Thursday", "Friday", "Saturday", "Sunday"}; foreach (string theDays in days) { System.Console.WriteLine(theDays); }

    string days. foreach :

    ( theDays) ( days).

    foreach.

    4.11 C#

    , C#. , . :

    return, try, catch, finally, throw.

    check uncheck lock , using. 4.12 1. C#, (command line) a,b,c e ax2+bx+c e (.. 0.0001). JAVA.

    import java.math.*; public class Trionym { public static void main(String args[ ]) { double a,b,c,d,r1,r2,e; a=(new Double(args[0])).doubleValue();

  • 68

    b=(new Double(args[1])).doubleValue(); c=(new Double(args[2])).doubleValue(); e=(new Double(args[3])).doubleValue(); if((Math.abs(a)

  • 69

    5

    5. 5.1 / (Concurrent Programming) mainframes 70 (Multitasking). , ( ) (Parent process) ( ) (child process) . , ( ), . , ( / ) Problem manager, (Problem Solvers), , .

    1

    2

    1 2 ....

    -1

    ,,,,,, ,,,,,,

    ,,,,,, ,,,,,,

    ,,,,,, ,,,,,,

  • 70

    , multitasking 90 , , : Multitasking-Process- Based (MPB): . (Units) ( *.exe, *.com, *.bat) , , , . Multitasking- Thread-Based ():

    multitasking, (threads) , . , , . C++, Java C#, .

    1 2 Problem Solvers

    m-1 m .......

    Problem Manager

    1 2

    t1

    3 4 5

    t2

    MTB: t1 t2

  • 71

    , . 1, 2, ...., 5. 2 t1,t2. 3 4. t1,t2, , , : 1, 2, t1, t2, 3, 4, 5. , :

    (t1)+

    (t2) >= (t1+t2)

    , (t1+t2) t1 t2. 5.2 Java C#, . , . , , Java C#, , . Java , , , :

    Ready to Run: CPU-time

    new. Running: ready to run

    , start run

    Waiting: running

    Done: run running, .

    .NET

    . System.Threading.ThreadState. C# . : System.Threading.ThreadState Aborted Stopped AbortRequested Abort ,

    ThreadAbortException . Background

    . daemon Java.

    Running .

  • 72

    Abort. Stopped . StopRequested . Suspended

    Suspend. SuspendRequested . Unstarted

    Start. WaitSleepJoin

    : Wait, Sleep Join. (read-only) Thread.ThreadState. , . , Monitor.Wait Abort, WaitSleepJoin AbortRequested Wait , AbortRequested ThreadAbortException , Aborted. :

    IsAlive true Unstarted, Stopped Aborted.

    IsBackground true

    . isDaemon Java . (background threads) Normal . (active foreground thread), Abort .

    .

    :

    Unstarted Start Running Running Sleep WaitSleepJoin Wait Object

    WaitSleepJoin

    Interrupt Running Suspend SuspendRequested Suspended Resume Running Abort AbortRequested

  • 73

    (abort)

    Stopped

    Stopped

    , . Suspended Resume.

    , ,

    , , :

    Thread [, , ]

    , , , . , System.Threading. . Java C#.

    Public class NewThread implements Runnable { public void run () { for (int i=0;i

  • 74

    for (int i=0;i

  • 75

    , Java C#. : Java C#

    Java C# Runnable ThreadStart (delegate) Thread.currentThread Thread.CurrentThread (property) Thread.getName Thread.Name Thread.getPriority Thread.Priority

    . To .NET .

    Thread.setPriority Thread.Priority . To .NET .

    Thread.getThreadGroup

    N/A

    Thread.interrupt Thread.Interrupt (method) Thread.interrupted N/A Thread.isInterrupted N/A Thread.isDaemon Thread.isBackGround N/A Thread.isAlive N/A Thread.isThreadPoolThread Thread.join Thread.Join Thread.suspend(dep) Thread.Suspend Thread.resume(dep) Thread.Resume Thread.run N/A .NET

    ThreadStart .

    Thread.sleep Thread.Sleep Thread.start Thread.Start Thread.stop Thread.Abort N/A Thread.ResetAbort N/A Thread.SpinWait Thread.yield Thread.Sleep(0) 5.4 , , . , . , . (synchronization). Java ,

  • 76

    . monitor / semaphore. monitor (lock) , . Java :

    Public void dosomething () { synchronized(this) { //some synchronized operation } }

    C# Java keyword lock keyword synchronized. , synchronization C# .

    Public void dosomething () { lock(this) { //some synchronized operation

    } }

    H C# Java

    keyword synchronized , MethodImpl (MethodImplOptions.Synchronized) . Java :

    private synchronized void doLock() { // o }

    C# : [MethodImpl(MethodImplOptions.Synchonized)] private void doLock() { // o }

    MethodImpl

    (namespace) System.Runtime.CompilerServices, . :

    lock,

  • 77

    . .

    static , this typeof(MyClass). 5.4.1

    Java, Object wait, notify, notifyAll. Object C# , System.Threading.Monitor , . :H Object Java Threading C#

    Java C# Object.wait() Monitor.Wait() Object.notify() Monitor.Pulse() Object.notifyAll() Monitor.PulseAll()

    Monitor . Java, C# , keyword lock.

    Monitor

    Java, . Java.

    // class Cshare { synchronized void display1(int numb, String threadname) { System.out.println(i=+numb+ +threadname); } synchronized void display2(int numb, String threadname) { System.out.println(i=+numb+ +threadname); } } class NewThread implements Runnable { String name; Cshare object; public NewThread1(Cshare obj,String strname) { object=obj; name=strname;

  • 78

    System.out.println(Starting:+name); } public void run() { for (int i=1;i

  • 79

    { System.Console.WriteLine("i =" + numb +" " + threadname); } } } class NewThread1 : SupportClass.IthreadRunnable { internal System.String name; internal Cshare object_Renamed; public NewThread1(Cshare obj, System.String strname) { object_Renamed = obj;

    name = strname; System.Console.WriteLine("Starting:"+ name); } public void Run() { for (int i = 1; i < 6; i++) { object_Renamed.display1(i, name); } System.Console.WriteLine("ok==>" + name); } } class NewThread2 : SupportClass.IthreadRunnable { internal System.String name; internal Cshare object_Renamed; public NewThread2(Cshare obj, System.String strname) { object_Renamed = obj; name = strname; System.Console.WriteLine("Starting:"+ name); } public void Run() { for (int i = 1; i < 6; i++) { object_Renamed.display2(i, name); } System.Console.WriteLine("ok==>" + name); } } class Java041 { [STAThread] public static void Main(System.String[] x) { Cshare common = new Cshare(); new System.Threading.Thread(new System.Threading.ThreadStart(new NewThread1(common, "Given").Run)).Start(); new System.Threading.Thread(new System.Threading.ThreadStart(new NewThread2(common, "Taken").Run)).Start(); } }

  • 80

    , display1, display2 , :

    Starting: Given Starting: Taken i=1 Taken i=2 Taken i=3 Taken i=4 Taken i=1 Given i=5 Taken ok Taken i=2 Given i=3 Given i=4 Given i=5 Given ok Given

    5.5

    1. Java C#. class NewTread extends Thread { NewThread(String name) { super(name); } public void run() { for (int i=1;i

  • 81

    boolean flag = true; synchronized void display1(int numb, String threadname) { if (!flag) try { wait(); } catch(InterruptedException e){} System.out.println(i= +numb + +threadname); flag =false ; notify(); } synchronized void display2(int numb, String threadname) { if (!flag) try { wait(); } catch(InterruptedException e){} System.out.println(i= +numb + +threadname); flag =true ; notify(); } } class NewThread1 implements Runnable { String name; Cshare object; public NewThread1(Cshare obj, String strname) { object=obj; name=strname; System.out.println(Starting: +name); } public void run() { for (int i=1;i

  • 82

    System.out.println(ok +name); } } class Java040 { public static void main(String x[]) { Cshare common=new Cshare(); new Thread(new NewThread1(common, Given)).start(); new Thread(new NewThread2(common, Taken)).start(); } }

    Java041 ;

  • 83

    6o

    6. 6.1

    , (run-time errors) . , , . .

    (Java C#)

    C++/Visual C++, (3) . (exceptions) (events), . , , , . , , . , , (exception throwing). , - (exception handler), . , (catches the exception). , . 6.2

    . ( ) :

    :

    . (errors):

    JAVA/C# .

    Exception . JAVA , , import . Runtime Exception .

  • 84

    , JAVA . , .

    Java,

    C#. , System System.IO

  • 85

    Object

    Throwable

    Exception

    CloneNotSupportedException

    IllegalAccessException

    InstantiationException

    InterruptedException

    NoSuchMethodException

    ClassNotFoundException

    RuntimeException

    ()

    AWTException

    EOFException

    InterruptedIOException

    FileNotFoundException

    UTFDataFormatException

    MalformedURLException

    ProtocolException

    SocketException

    UnknownHostException

    UnknownServiceException

    java.*

    java.lang

    java.awt

    java.io

    java.net

  • 86

    () RuntimeException java.*

    java.lang

    java.util

    NegativeArraySizeException

    NullPointerException

    SecurityException

    IllegalMonitorStateException

    ClassCastException

    ArrayStoreException

    ArithmeticException

    RuntimeException

    IndexOutOfBoundsException

    ArrayIndexOutOfBoundsException

    StringIndexOutOfBoundsException

    IllegalArgumentException

    IllegalThreadStateException

    NumberFormatException

    NoSuchElementException

    EmptyStackException

  • 87

    java.lang

    java.awt

    Object

    Throwable

    Error

    ThreadDeathError

    IncompatibleClassChangeError

    LinkageError

    NoClassDefFoundError

    UnsatisfiedLinkError

    VerifyError

    ClassFormatError

    ClassCircularityError

    IllegalAccessError

    AbstractMethodError

    InstantiationError

    NoSuchFieldError

    NoSuchMethodError

    AWTError

    VirtualMachineError

    IllegalAccessError

    NoSuchFieldError

    NoSuchMethodError

    AbstractMethodError

    InstantiationError

  • 88

    6.3

    (exception handling) Java C#, C#. C# . , . Java C# try Try .

    catch.

    catch Catch H C# catch. finally Finally ,

    trycatch . throw Throw . throws N/A C# .

    ,

    Object

    SystemException Exception IOException

    OverflowException

    ArgumentOutOfRangeException ArgumentNullException

    ApplicationException

    StackOverflowException

    UnauthorizedAccessException

    ArgumentException

    FileLoadException

    FileNotFoundException

    EndOfStreamException

    DirectoryNotFoundException

  • 89

    throws. N/A Checked

    . N/A Unchecked

    , , try catch , finally :

    try { ;} catch (1 1) { 1;} catch (2 2) { 2;} .. finally { try;} // try, . , finally , catch. , try. catch , catch. , finally ( ). try catch, try/catch. , , C# ! , :

  • 90

    Java 5 . Java015. . 5 Exception ArrayIndexOutOfBoundsException -. Exception catch finally.

    class Java015 { public static void main(String x[]) { int i=0; try { for(i=0; i

  • 91

    } finally { System.out.println("Last value of i="+i); } System.out.println("normal End..."); } }

    C#.

    using System; class Java015 { public static void Main(System.String[] x) { int i = 0; try { for (i=0; i < 5; i++) System.Console.WriteLine(x[i]); } catch (System.IndexOutOfRangeException exception_object) { System.Console.WriteLine("Error : less than 5 arguments"); System.Console.WriteLine("Name of Exception: " + exception_object); } finally { System.Console.WriteLine("Last value of i=" + i); } System.Console.WriteLine("normal End..."); } }

    .

    try,catch,finally . Exception , C#, IndexOutOfRangeException. 6.4

    , try, catch, (multiple catch clauses). , catch .

    Java016.java ,

    try, . , catch:

  • 92

    (1) , (Arithmetic Exception), :

    System.out,println("Divide by zero...!");

    (2) , (ArrayIndexOutOfBoundsException), try x[4], . , catch:

    System.out,println("Error:less than 5 arguments"); // multiple Exceptions class Java016 { public static void main(String x[]) { int i=0,f=10; int args = x.length; try { int y=f/args; for(; i

  • 93

    { static void Main(string[] args) { int i=0; int f=10; int x=args.Length; try { int y=f/x; for (i=0;i

  • 94

    try catch (nesting try phenomenon). , catch . catch, catch try. , , .

    6.5

    , run time system C#. , throw :

    throw _Exception

    throw _Exception

    , Exception - . , throw new / try/catch . , throw try/catch throw, . , Proklisi.java.

    class Proklisi { public static void main(String x[]) { int i=0; int len = x.length; if (len

  • 95

    int len = args.Length; if (len

  • 96

    e .

    getCause() InnerException null .

    getLocalizedMessage()

    N/A .NET Message. Java , .

    getMessage() Message .

    getStackTrace() StackTrace string .

    initCause() N/A . .NET .

    printStackTrace() N/A . ToString Stream Console.

    setStackTrace() N/A C#.

    N/A HelpLink . URN URL.

    N/A Source .

    N/A TargetSite MethodBase .

    N/A GetBaseException()

  • 97

    .

    N/A GetObjectData() .

    N/A HResult . .

    6.7 CHECKED UNCHECKED

    integer . , Java C# short 32768 32767. Java :

    short x = 32767; x++; System.out.println(x);

    32768.

    Java (high-order bit), .

    keywords checked unchecked . checked :

    // checked as operator int num = checked (x * 3) // checked as statement block checked { int a = x++; int b = x * 3; int c = a * b ; }

    checked . checked , System.OverflowException . unchecked keyword ,

  • 98

    Java.

    checked unchecked :

    ,

    , checked keywords.

    , (runtime)

    , unchecked keywords.

    6.8

    1. N nxm, max(n,m)verticals?lines:verticals)>500

  • 99

    ||(lines>verticals?lines:verticals)

  • 100

    7o

    7. 7.1 (Object Oriented Software Development) , ( ) . , (class) , (objects). , (instance variables), , (methods) , () (template/instance). , , , .

    , . (class members) , .

    1 2 .................... k

    Template

    Objects

    Instances

    1

    k

    ......................

    2

  • 101

    7.2 Readonly Modifier C# constant Java final static. , . enum, struct. Java C# . Java final , a priori compilation . C# Java, const. C#, : [attributes] [ modifiers] const type identifier =value; .

    public const int i=60; protected const string name =Pavlos surname= null; protected internal const int j=j*2;

    C# string, literals, null. (modifier) struct : Member of class Member of struct Public Protected / Private Internal protected internal / New / Abstract / / Sealed / implicit Virtual / / Override / / Readonly / / Volatile / / Static Implicit / Extern / / :

    1. public .

    2. protected .

    3. private

  • 102

    4. internal

    5. protected internal .

    6. new .

    7.3 , . : [modifiers] type identifier = value; .

    public int i; public byte x=5,z=100;

    modifier, . Member of class Member of struct Public Protected / Private Internal Protected internal / New / Abstract / / Sealed / Implicit Virtual / / Override / / Readonly Volatile Static Extern / / :

    1. static (global variable). , :

    _. _

    static,

  • 103

    . 2. Volatile,

    Java ( ) (multithreaded programs). Readnly Volatile

    ,

    , . , . , . , , ; , this :

    This. __

    this . , MyClass rest, width height , , , this. , x1, x2, x3, x4 x1, x2 x3, x4 . java020 .

    class MyClass { double width; double height; double rest(double width, double height) { return (this.width)*(this.height)-(width*height); } } class java020 { public static void main(String x[]) { double pw=0, ph=0; MyClass object= new MyClass(); try { object.width=(new Double(x[0])).doubleValue(); object.height=(new Double(x[1])).doubleValue(); pw= (new Double(x[2])).doubleValue(); ph= (new Double(x[3])).doubleValue(); } catch(ArrayIndexOutOfBoundsException e1)

  • 104

    { System.out.println(incomplete data...); return; } catch (NumberFormatException e2) { System.out.println(errors in data...); return; } System.out.println(rest area =+object.rest(pw, ph)); }

    } C# :

    using System; class MyClass { internal double width; internal double height; internal virtual double rest(double width, double height) { return (this.width) * (this.height) - (width * height); } } class java020 { [STAThread] public static void Main(System.String[] x) { double pw = 0, ph = 0; MyClass object_Renamed = new MyClass(); try{ object_Renamed.width = (System.Double.Parse(x[0])); object_Renamed.height = (System.Double.Parse(x[1])); pw = (System.Double.Parse(x[2])); ph = (System.Double.Parse(x[3])); } catch (System.IndexOutOfRangeException e1) { Console.WriteLine("incomplete data..."); return ; } catch (System.FormatException e2) { Console.WriteLine("errors in data...");

    return ; }

    Console.WriteLine("rest area="+ object_Renamed.rest(pw,ph)); } }

    : Java020 4 3 2 1 Rest area =10.0

  • 105

    Java020 s 3 2 1 Errors in data Java020 4 3 Incomplete data MyClass width height. , compilation java020. . , MyClass java020, object_Renamed. 7.4 , , (method) : [Modifiers] return type identifier (parameter list) { body } : protected static double calc(int j, double b) { body code }

    modifier, . Member of

    class Member of

    struct Member of Interface

    Public Implicit Protected / / Private / Internal / protected internal / / New / Abstract / Implicit Sealed / Virtual / / Override / / Readonly / / / Volatile / / / Static / Extern /

    abstract

    . , ( ) , () . abstract

  • 106

    abstract . . (subroutine) void. , (constructors) void. , return. , , , . , Repos days () epitokio (), foros ( %). Main invest Repos. compute call by reference invest call by value foros. compute , () invest invest.tokos. invest object compute Repos.

    class Repos {double kefalaio, epitokio, tokos; int days; double compute(Repos object, double foros) {

    object.tokos=(object.kefalaio*object.epitokio*object.days)/36500.0;

    return(foros*object.tokos)/100.0;//poso forou } } class java022 {public void static main(String x[]) { Repos invest= new Repos(); double eforia, foros ;

    try { invest.kefalaio=(new Double(x[0])).doubleValue(); invest.epitokio=(new Double(x[1])).doubleValue(); invest.days=(new Integer(x[2])).intValue(); foros= (new Double(x[3])).doubleValue(); if(invest.kefalaio

  • 107

    } catch(ArrayIndexOutOfBoundsException e1) { System.out.println(Incomplete data); return; } catch(NumberFormatException e2) { System.out.println(Errors in data); return; } System.out.println(Tokos=+invest.tokos); System.out.println(Foros=+eforia); System.out.println(Clear Tokos=+(invest.tokos-eforia)); System.out.println(Investment=+(invest.tokos-

    eforia+invest.kefalaio )); }

    }

    C# :

    using System; class Repos { internal double kefalaio, epitokio, tokos; internal int days; internal virtual double compute(Repos object_Renamed, double

    foros) {

    object_Renamed.tokos=(object_Renamed.kefalaio*object_Renamed.epitokio * object_Renamed.days) / 36500.0; return (foros * object_Renamed.tokos) / 100.0; }

    } class java022 { static void Main(System.String[] x) { Repos invest=new Repos(); double eforia, foros; try{ invest.kefalaio=(System.Double.Parse(x[0])); invest.epitokio=(System.Double.Parse(x[1])); invest.days=(System.Int32.Parse(x[2])); foros=(System.Double.Parse(x[3])); if(invest.kefalaio

  • 108

    return ; } catch(System.FormatException e2) { Console.WriteLine("errors in data..."); return ; } Console.WriteLine("tokos="+invest.tokos); Console.WriteLine("foros="+eforia); Console.WriteLine("clear tokos="+(invest.tokos-eforia)); Console.WriteLine("investment="+(invest.tokos-

    eforia+invest.kefalaio)); } }

    : Java022 400000 2.7 30 7 Tokos = 887.6712328767123 Foros=62.13698630136986 Clear tokos=825.534246573424 Investment=400825.5342465753 Java022 100000 3 Incomplete data Java022 100000 3 s 4 Errors in data 7.5 Java C# , . (overloading methods), , (parametric polymorphism). Java . Math abs , . Java C# o . . . :

  • 109

    class polymorphism { void compute(int n, int m) { Console.WriteLine(version1+(n+m)); } void compute(int n, double m) { Console.WriteLine(version2+(n+m)); } void compute(double k) { Console.WriteLine(version3+(k+k)); } public static void Main(String[] x)

    { polymorphism object = new polymorphism(); object.compute(5,9); object.compute(5,2.5); object.compute(2.5); object.compute(5); }

    }

    polymorphism compute. : Polymorphism Version 1 14 Version 2 7.5 Version 3 5.0 Version 3 10.0 compute, C# . , C# (virtual) , . , ( ) , compilation, . , (reference) ( ) :

    1. 2. ( ) 3. () 4.

    , common.

  • 110

    class reference { static void Main(String[] x)

    { Parent object_parent=new Parent(); Son1 object_son1=new Son1(); Son2 object_son2=new Son2(); Parent ref; ref=object_parent; ref.common(); ref=object_son1; ref.common(); ref=object_son2; ref.common(); }

    }

    , ref object_parent, object_son1 object_son2. 7.6 CONSTRUCTORS , (constructors), , , . : [modifier] identifier([parameters])[:initializer([parameters])] {body code} :

    class MyClass:MySuperClass{ public MyClass(int x,string y):base(x,y){ constructor code}}

    keyword Java Super constructor , keyword base. Modifier : Member of class Member of struct Public Protected / Private Internal protected internal /

  • 111

    New / / Abstract / / Sealed / / Virtual / / Override / / Readonly / / Volatile / / Static Extern , constructors, :

    void

    java028 , Squares platos mikos, Square() . , .

    class Squares { double platos; double mikos; Squares(double p, double m) { platos=p; mikos=m; } Squares() { platos=0; mikos=0; } Squares(double dim) { platos=mikos=dim; } double square() { return platos*mikos; } } class java028 { public static void main(String args[]) { Squares shape1=new Squares(); Squares shape2=new Squares(30,20);

  • 112

    Squares shape3=new Squares(10); System.out.println(Square of Shape1=+shape1.square()); System.out.println(Square of Shape2=+shape2.square()); System.out.println(Square of Shape3=+shape3.square()); } }

    C# using System; class Squares { internal double platos; internal double mikos; internal Squares(double p, double m) { platos = p; mikos = m; } internal Squares() { platos = 0; mikos = 0; } internal Squares(double dim) { platos = mikos = dim; } internal virtual double square() { return platos * mikos; } } class java028 { [STAThread] public static void Main(System.String[] args) { Squares shape1 = new Squares(); Squares shape2 = new Squares(30, 20); Squares shape3 = new Squares(10); Console.WriteLine("Square of Shape1="+shape1.square()); Console.WriteLine("Square of Shape2="+shape2.square()); Console.WriteLine("Square of Shape3="+shape3.square()); } }

    : Square of Shape1= 0.0 Square of Shape2= 600.0 Square of Shape3= 100.0 java028 shape1, shape2, shape3 Squares. , , , Squares .

  • 113

    , , , platos mikos .

    . , . Java finalize() Java (garbage collection). C# finalize Java, destructor, (~). 7.7 , , . . (class) : [] class _[: _] { ; ; ; } : : :

    Public Final sealed abstract

    7.8

    1. : Class finalclass { final int finalvar; public void setfinalvar() { finalvar=1; } } C# .

    2. :

    Class zero { static int a=5,y; static void write(int b) { System.out.println(b=+b+a=+a+y=+y); }

  • 114

    static{y=a++;} public static void main(String s[]) { write(10); } } C#, .

    3. . C# ( ) . . .

  • 115

    8o

    8. - 8.1 (package) . 1983 DOD(Department Of Defense) , ANSI Standard ADA. , , , Java C# . (FORTRAN, BASIC, COBOL) (compilation units) (*.lib *.dll), ADA . programming holons (, , , ) , , :

    1. (libraries): , *.obj. *.exe linker . linking , *.exe , . , *.lib. , *.dll, . , , , .

    2. (packages): OOP (OBJECT ORIENTED PROGRAMMING), , . , (Taxonomy). Java / , java.io, , . / . , , .

  • 116

    , . (environmental variable) CLASSPATH *.class . , . Java C# , , Java ( ) , - import. C# - using. , - package Java C# - namespace. 8.2 Java, , / . , :

    package p1[. p2[ .pm]]];

    . : p1/p2//pm. , : package jcp.one.two; jcp one two . , Java , . , package, *.class . Java.

    package jcp; class Telephones { String name; String homenumber; String mobile; Telephones(String str1, String str2, String str3) { name=str1; homenumer=str2; mobile=str3;

  • 117

    } void show_list() { System.out.println(name+ +homenumber+ +mobile); } } class Java029 { public static void main( String x[]) { Telephones Record[]= new Telephones[4]; Record[0]= new Telephones(name1,

    2101111111,6941111111); Record[1]= new Telephones(name2,

    2102222222,6942222222); Record[2]= new Telephones(name3,

    2103333333,6943333333); Record[3]= new Telephones(name4,

    2104444444,6944444444); for (int i=0;i

  • 118

    public static void Main(System.String[] x){ Telephones[] Record = new Telephones[4]; Record[0]= new Telephones("name1", "2101111111","6941111111"); Record[1]= new Telephones("name2", "2102222222","6942222222"); Record[2]= new Telephones("name3", "2103333333","6943333333"); Record[3]= new Telephones("name4", "2104444444","6944444444"); for (int i = 0; i < 4; i++) Record[i].show_list();}}}

    8.3 , , , . , , . , (library) , object linker, *.exe *.dll. Java C# . , . :

    1. M , . , Java :

    java.util.Stack= new java.util.Stack();

    C#:

    System.Collections.Stack= new System.Collections.Stack(); object Stack . , , , .

    2. M ,

    :

    import p1.[.p2].( /* ); C# :

  • 119

    using p1.[.p2].( );

    . , using .

    ,

    ( ). . ( ) public, , .

    . , using, , , :

    System.MyLib.Cl1 object1= new System.MyLib.Cl1();

    System.YourLib.Cl1 object2= new System.YourLib.Cl1();

    o object1, object2 Cl1, System.MyLib, System.YourLib. 8.4 , . Main(), -, . , , :

    1. public 2. ,

    using 3. namespace. 4. -

    Main(). 5. C# Java

    Main(). , . classpath Java -

  • 120

    6. , , Internet. , a.b.com com.b.a.

    , jcp jcp. Telephone Show_List() . public, Telephones.

    package jcp; public class Telephones{ String name; String homenumber; String mobile; public Telephones(String str1, String str2, String str3){ name=str1; homenumber=str2; mobile=str3; } public void show_list(){ System.out.println(name+ +homenumber+ +mobile);}} , jcp, Java030

    jcp import import jcp.*; class Java030 { public static void main( String x[]) { Telephones Record[]= new Telephones[4]; Record[0]= new Telephones(name1,

    2101111111,6941111111); Record[1]= new Telephones(name2,

    2102222222,6942222222); Record[2]= new Telephones(name3,

    2103333333,6943333333); Record[3]= new Telephones(name4,

    2104444444,6944444444); for (int i=0;i

  • 121

    public class Telephones { System.String name; System.String homenumber; System.String mobile; public Telephones(System.String str1,System.String str2, System.String str3) { name = str1;

    homenumber = str2; mobile = str3;

    } public void show_list() { System.Console.WriteLine(name +"" + homenumber + ""+ mobile); } } Java030,

    using System jcp.

    namespace Java030 { using System; using jcp;

    class Java030 {

    [STAThread] public static void Main(System.String[] x) { Telephones[] Record = new Telephones[4]; Record[0]= new Telephones("name1", "2101111111","6941111111"); Record[1]= new Telephones("name2", "2102222222","6942222222"); Record[2]= new Telephones("name3", "2103333333","6943333333"); Record[3]= new Telephones("name4", "2104444444","6944444444"); for (int i = 0; i < 4; i++) Record[i].show_list(); } } } Java031,

    , jcp .

    namespace Java031 { using System; class Java031 {

  • 122

    [STAThread] public static void Main(System.String[] x) { jcp.Telephones[] Record = new jcp.Telephones[4]; Record[0]= new jcp.Telephones("name1", "2101111111","6941111111"); Record[1]= new jcp.Telephones("name2", "2102222222","6942222222"); Record[2]= new jcp.Telephones("name3", "2103333333","6943333333"); Record[3]= new jcp.Telephones("name4", "2104444444","6944444444"); for (int i = 0; i < 4; i++) Record[i].show_list(); } } }

    8.5

    C++

    . Java , (single inheritance). . , Java C#, (polymorphism), (interface), , .

    , ,

    . (complete abstract class) . , , . , , . :

    interface _ interface[:interface ]

    { 1( );

    ...........

    m( ); }

    , public, , . public, public

  • 123

    . , (implicitly) , ( ). . , call by value :

    interface Inter1 { int method1(int param1); void method2(); }

    Example1 Inter1, :

    class Example1 implements Inter1{ double va