Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "© Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements."— Presentation transcript:

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

2 © Tomáš Kozel, Pavel Čech 2 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

3 © Tomáš Kozel, Pavel Čech 3 Program = An organized list of instructions Types of instructions Declarations Statements

4 © Tomáš Kozel, Pavel Čech 4 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.

5 © Tomáš Kozel, Pavel Čech 5 Statements Statement = notation for some computational action Control sequence sequence selection selection iteration iteration executive value assignment value assignment function call function call

6 © Tomáš Kozel, Pavel Čech 6 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!!!

7 © Tomáš Kozel, Pavel Čech 7 Naming conventions Classes, 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)

8 © 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(" To je blbost, ne? "); out.println("Metoda : "+method+" "); out.println("DocPath : "+what+" "); out.println("HTTP VERSION : "+vers+" "); 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

9 © Tomáš Kozel, Pavel Čech 9 Why? 80% of the SW life cycle consumes maintenance Minimum of projects is maintained by its author Conventions improves readability and helps understanding

10 © Tomáš Kozel, Pavel Čech 10 Files Source code - extension.java Bytecode - 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

11 © Tomáš Kozel, Pavel Čech 11 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 */

12 © Tomáš Kozel, Pavel Čech 12 Structure of the class 1. Documentary comment /** … */ 2. Head of the class or interface 3. Implementation comment /* … */ 4. Class variables (static) in order public, protected, package, private 5. Instance variables the same order 6. Constructors 7. Methods logically grouped not necessarily according to visibility

13 © Tomáš Kozel, Pavel Čech 13 Comments Documentary – mainly specification Implementation – notes to code, parametres Do not duplicate and overuse comments Do not use special characters (FF,ESC, …) Do not put into boxes

14 © Tomáš Kozel, Pavel Čech 14 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

15 © Tomáš Kozel, Pavel Čech 15 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)

16 © Tomáš Kozel, Pavel Čech 16 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

17 © Tomáš Kozel, Pavel Čech 17 Executable class Executable 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 }

18 © Tomáš Kozel, Pavel Čech 18 Example public class HelloWorld { //main methods follows public static void main(String[] args) { // write to the output device System.out.println(”Hello world!”); } Save to file HelloWorld.java Compilation Compilation

19 © Tomáš Kozel, Pavel Čech 19 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

20 © Tomáš Kozel, Pavel Čech 20 Command line compilation 1. Run command line window 2. Check whether J2SDK in path variable (set if necessary) 3. Change directory to where source file is located 4. Write javac HelloWorld.java

21 © Tomáš Kozel, Pavel Čech 21 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

22 © Tomáš Kozel, Pavel Čech 22 Using Eclipse and FIMUtils import 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 }


Download ppt "© Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements."

Similar presentations


Ads by Google