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

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

CS Network Programming CS 3331 Fall CS 3331 Outline Socket programming Remote method invocation (RMI)
Designing a Program & the Java Programming Language
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Basic Java Constructs and Data Types – Nuts and Bolts
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
1 Streams and Input/Output Files Part 3. 2 Handling Primitive Data Types The basic input and output streams provide read/write methods that can be used.
3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions.
1 Copyright © 2005, Oracle. All rights reserved. Introducing the Java and Oracle Platforms.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
Introduction to programming in java. Input and output to screen with Java program Structure of Java programs Statements Conditional statements.
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Addition 1’s to 20.
Week 1.
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
Introduction to Programming G51PRG University of Nottingham Revision 1
Coding Standards for Java An Introduction. Why Coding Standards are Important? Coding Standards lead to greater consistency within your code and the code.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
JAVA PROGRAMING LANGUAGE. Content of Java 2 SDK  Development Tools (In the bin subdirectory.) Tools and utilities that will help you develop, execute,
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
S.W. Ma/CIM/LWL41211/2 Prog. IIA Page 1 HKIVE (Lee Wai Lee Campus) Department of CIM Course : Year 2 Module : Programming IIA Textbook : Introduction.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Language and SW Dev’t
© 2006 Pearson Education 1 Obj: cont 1.3 and 1.4, to become familiar with identifiers and to understand how programming languages work HW: p.51 #1.8 –
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Copyright © Curt Hill Java Looking at our first console application in Eclipse.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 2: Java Fundamentals
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
A First Simple Program /* This is a simple Java program. Call this file "Example.java".*/ class Example { // Your program begins with a call to main().
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Creating a Java Application and Applet
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
Introduction to Java Programming, 4E Y. Daniel Liang.
The eclipse IDE IDE = “Integrated Development Environment”
Working with Java.
GC101 Introduction to computer and program
Chapter No. : 1 Introduction to Java.
Introduction to.
Java programming lecture one
Introduction to Programming in Java
Statements, Comments & Simple Arithmetic
Introduction to Java Programming
Chapter 1: Computer Systems
Applying OO Concepts Using Java
Tutorial 10: Programming with javascript
Classes CS 21a: Introduction to Computing I
Java Looking at our first console application in Eclipse
Corresponds with Chapter 5
Presentation transcript:

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

© Tomáš Kozel, Pavel Čech 2 History Sun Microsystems – for home electronics (microvaves, mobiles, …) WWW Applets Netscape Navigator 2.0 – firs support it is derived from Eiffel, SmallTalk, Objective C, C

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

© 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 are not identifiers. ) Identifiers must be declared before you can use them A declaration defines an identifier and allocates memory for it.

© 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

© 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!!!

© 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)

© 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/ 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

© 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

© 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

© 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 */

© 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

© 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

© 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

© 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

© 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

© 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 }

© 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

© 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

© 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

© 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

© 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 }