Introduction to Java

farrightΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 4 χρόνια και 10 μήνες)

641 εμφανίσεις

1


Introduction to Java



Why?



Security


Object oriented but also classical procedural


Portable programs


Basic implementation

(JDK


Java Development Kit) Sun
you may find
on

http
://java.sun.com


Development environment

NetBeans 5.0
-

BlueJ, fy. Sun Microsystem.

http://www.netbeans.org/

2

Introduction to

Java


Interpret implementation


program one or more source programs with extension
.java:




Program.java



Source progams may be compile by
javac

to internal form (byte code)
with extension
.class
:

Program.java >
javac

> Program.class




Interpretation done by program

java

(
JVM


Java Virtual Machine in
package JRE
-

Java Runtime Environment) and it is executed as
execution


Program.class >
java

> „execution “


Comment

program uses libraries for compilation and interpretation!!!

3

Java program processing

Program.java

public class Program
{

public static void main(String[] args) {

System.out.println(“Hi,this is your first

program
”);


}

}


Compilation to byte
-
code:

javac Program.java


Result:

Program.class


Interpretation:

java Program

Hi, this is your first program

4

Java
Development Environment



WE will create source programs in Java by
development environment

NetBeans

/
NetBeans
-
BlueJ


In computer lab


5

First program in
Java


Copy of text on screen:

public class PrvniProgram {


public static void main(String[] args) {


System.out.println(“
Hi, this is first program
");


}

}


After compilation and interpretation output will be

Hi, this is first program



Simpleset source program
-

one file



Public class declaration

(
public class
),


Main function

main

(public static method)


File has to have name same as public class with
.java

extension


Function heading of main function

main

():


Key words

public static void

(
void
)


//procedure


(String[] args)

parameters specification


Convention
:
class names starts with capitol letters

6


Directory structure









alg1
,
alg2

atd.
are
packages


Each source program in package starts with package specification

package alg1
;

public class PrvniProgram {


public static void main(String[] args) {


System.out.println("
Hi, this is first program

");


}

}

Examples

source

classes

7

Name and conventions


package

-

only small letters, may be separated by
.

ex.: alg1, java.util




class,
-

starts with capitol letrter, ex.: String, MyFirstClass, Serializable,
Comparable



method



first word starts with small

letter next with capitol
:

setBorder,
isEmpty, getNumber



variable

-

first word starts with small

letter next with capitol ex.: diskriminant,
totalCount



constant
-

final

variable
and
label


all
capitol letters separated by _ ex.:
MAX_COUNT, RED

8

Celá čísla

Primitiv
e
typ
es

Type Format



Wrap class



Integers

byte

8
-
bit
-
128 … 127
Byte

short

16
-
bit
-
32768 … 32767
Short

int

32
-
bit
-
2147483648 … 2147483647
Integer

long

64
-
bit
-
9223372036854775808 … 9223372036854775807
Long


char

16
-
bit
Unicode
'
\
u0000' to '
\
uffff' 0 ... 65535
Character



Real numbers

-


by IEEE 754

float

32
-
bit 2
-
149

… (2
-
2
-
23
)*2
127

, NaN, pos/neg Infinity
Float

double

64
-
bit 2
-
1074

… (2
-
2
-
52
)*2
1023
, NaN, pos/neg Infinity
Double


boolean true

false

Boolean


void
empty type





Void


9

Conversion of primitive types

exp

byte

long

int

short

float

double

char

exp

mantisa

mantisa

By assignment

overloading

sign
: 0/1
~

+/
-

They are not compatible but we may convert them

10

Oper
a
tor
s and their priority

priority

operator

operand type

associability

operation

1

++

arithmetic

P

pre/post
incrementasion

--


arithmetic

P

pre/post
decrementasion

-


arithmetic

P

Unary plus/minus

~

numerical

P

bit inversion

!

logical

P

logical negation

(type)

arbitrary

P

overloading

2

*, / ,%

arithmetic

L

Multiplication,
division, remainder

3

-


arithmetic

L

subtraction

+

arithmetic, string

L

addition,
concatenation

11

Oper
a
tor
s and their priority

II

priority

operator

operand type

associability

operation

4

<<

integer

L

Shift left



>>

integer

L

Shift right



>>>

integer

L

Shift right

with zeros additions



<,>, <=, >=

arithmetic

L

relation



instanceof

object

L

Class test



==, !=

primitive

L

Equal, not equal

7

&

integer or
logical

L

Bit or logical AND

8

^

-
"
-


L

Bit or logical XOR

9

|

-
"
-


L

Bit or logical OR

12

Oper
a
tor
s and their priority

III

priority

operator

operand
type

associability

operation

10

&&

logical

L

logical AND

11

||

logical



L

Logical OR

12

? :

logical

P

condition

13

=,
-
=, *=, /=, %=,
==, &=, ^=, |=

arbitrary

P

assigment

13

Basic constructors

Compound statement { statement1; statement2; definision1 ... ; }


if

( condition ) statement1 [
else

statement2 ]


switch

( index ) {


case

k1 : [statements]


case

k2 : [statements]


case

k3 : [statements]


.......


[
default

: statements]


}

use
break

for interruption

14

Basic constructors

-

loops




while

( condition ) statement


do

statement
while

(condition) ;


for

( initialization ; condition ; modification ) statement



for

( type element : kolekce nebo pole ) statement


-

from Java version 5.0






interruption of the loop:
break, continue