© tomáš kozel, pavel Čech java basics class structure, declaration, primitive data types,...

22
© Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

Upload: belen-stabler

Post on 01-Apr-2015

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

© Tomáš Kozel, Pavel Čech

Java Basics

Class structure, declaration, primitive data types,

statements

Page 2: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

2

© Tomáš Kozel, Pavel Čech

History Sun Microsystems – for

home electronics (microvaves, mobiles, …) - 1990

1993 - WWW Applets Netscape Navigator 2.0 –

firs support it is derived from Eiffel,

SmallTalk, Objective C, C

Page 3: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

3

© Tomáš Kozel, Pavel Čech

Program = An organized list of instructions

Types of instructions Declarations Statements

Page 4: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

4

© Tomáš Kozel, Pavel Čech

Declarations The names of elements used in a

program (eg.: variables, constants, types, methods,…) are called identifiers. (Numeric constants like 26057 are not identifiers.)

Identifiers must be declared before you can use them

A declaration defines an identifier and allocates memory for it.

Page 5: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

5

© Tomáš Kozel, Pavel Čech

StatementsStatement = notation for some

computational action

ControlControl• sequencesequence• seleselectionction• iterationiteration

executiveexecutive• value value assignmentassignment• function callfunction call

Page 6: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

6

© Tomáš Kozel, Pavel Čech

Indentifiers provides a name for variables, constants,

attributes, etc. can contain letters, number, or „_“ cannot contain spaces, diacritics,

operators must not match with reserved words

Java is Case-senzitive!!!

Page 7: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

7

© Tomáš Kozel, Pavel Čech

Naming conventionsClasses, Interfaces – noun, each begins with

capital letter (MainWindow, AppEvent, MujProgram)

Methods - verbs, first letter small, every other word start with capital letter (otevriSoubor, actionPerformed, switchView)

Variables – the same as methods, do not use „$“ and „_“,

Constants – capital letters, words separateed with „_“ (VIEW_GRAPH, COORDINATE_X)

Page 8: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

© Tomáš Kozel, Pavel Čech

Coding conventions public void run() { while (true) { try { Socket s = ss.accept(); BufferedReader in = new BufferedReader(new

InputStreamReader(s.getInputStream())); String gets = in.readLine(); System.out.println(gets); StringTokenizer st = new StringTokenizer(gets," "); String method = st.nextToken(); String what = st.nextToken(); String vers = st.nextToken(); PrintWriter out = new PrintWriter(s.getOutputStream()); out.println("http/1.1 200 OK"); out.println("Content-type: text/html"); //out.println("Content-length: 20"); out.println(); out.println("<h1>To je blbost, ne?</h1>"); out.println("Metoda : "+method+"<br>"); out.println("DocPath : "+what+"<br>"); out.println("HTTP VERSION : "+vers+"<br>"); System.out.println("HOTOVO"); out.close(); in.close(); s.close(); } catch (IOException e) { System.out.println("Chybicka : "+e.getMessage()); } } }

Rules for readable source codes in Java

Page 9: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

9

© Tomáš Kozel, Pavel Čech

Why? 80% of the SW life cycle consumes

maintenance Minimum of projects is maintained

by its author Conventions improves readability

and helps understanding

Page 10: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

10

© Tomáš Kozel, Pavel Čech

FilesSource code - extension .javaBytecode - extension .class Source code should be separated so that

none of the files do not exceed 2000 lines Each file can contain only one public class

or interface The number of private classes or

interfaces can be unlimited but should correspond in meaning with the public one

Page 11: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

11

© Tomáš Kozel, Pavel Čech

Structure of the file

1. Coments

2. Package declaration

3. Package import

4. Class and/or interface declaration

/* * Jméno třídy * * Verze * * Datum * * Copyright */

Page 12: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

12

© Tomáš Kozel, Pavel Čech

Structure of the class1. Documentary comment /** … */2. Head of the class or interface3. Implementation comment /* … */4. Class variables (static) in order public,

protected, package, private5. Instance variables the same order6. Constructors7. Methods logically grouped not

necessarily according to visibility

Page 13: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

13

© Tomáš Kozel, Pavel Čech

Comments

Documentary – mainly specificationImplementation – notes to code,

parametres

Do not duplicate and overuse comments Do not use special characters (FF,ESC,

…) Do not put into boxes

Page 14: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

14

© Tomáš Kozel, Pavel Čech

Implementation comments Blokový

before methods to denote main parts

One-line Leave empty one line

before Line-end

to temporarily comment one line of code

/* * Block comment */

/* Line */

// comment

Page 15: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

15

© Tomáš Kozel, Pavel Čech

Documentary comments Description of classes, interfaces,

methods for the purposes of code specification

/** Comment */ or/** * Comment */

Can contain arguments beginning with @ (eg. @autor, @param, @see)

Page 16: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

16

© Tomáš Kozel, Pavel Čech

Declaration & Definition

Declaration is an introduction of names for new variables, data types, constants, attributes, classes and methods

Definition is the actual code of a method, procedure or function; it defines how a particular action is implemented

In Java declaration and definition appears together

Page 17: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

17

© Tomáš Kozel, Pavel Čech

Executable classExecutable class = class that contains main method, which code is executed when running the class.

Mostly, the task of main methods is to create an instance (object) of a given class

public static void main(String[] args) {

//code

}

Page 18: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

18

© Tomáš Kozel, Pavel Čech

Examplepublic class HelloWorld {

//main methods followspublic static void main(String[] args) {

// write to the output deviceSystem.out.println(”Hello world!”);

}}

Save to file HelloWorld.java CompilationCompilation

Page 19: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

19

© Tomáš Kozel, Pavel Čech

Compiling

Assumptions: J2SDK is present on the computer J2SDK is in PATH (environment

variable) can be set like this:set PATH=%PATH%;c:\

J2SDK1.4.2_02\bin

kompilátor Javy

Page 20: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

20

© Tomáš Kozel, Pavel Čech

Command line compilation1. Run command line window2. Check whether J2SDK in path variable (set if

necessary)3. Change directory to where source file is

located4. Write

javac HelloWorld.java

Page 21: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

21

© Tomáš Kozel, Pavel Čech

Running After compilation write

java HelloWorld

Be aware that Java is case-sensitive ! See the relation between files (.java

a .class) and the name of the class

interpret Javy

Page 22: © Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements

© Tomáš Kozel, Pavel Čech

22

Using Eclipse and FIMUtilsimport fim.utils.Application; //package used

public class HelloWorld2 extends Application {

public void start() {out.println(“Hello world !");

}

public static void main(String[] args) {new HelloWorld2().start(); //create an instance

}}