Literature Pecinovský, Rudolf. 2009. Myslíme objektově v jazyku JAVA. 2nd ed. GRADA Publishing. Isaac Rabinovitch, Jacob Royal, Mark Hoeber, Scott Hommel, Sharon Zakhour, Tom Risser. 2007. JAVA 6 Výukový kurz. COMPUTER PRESS. Sun.The Java Tutorials. accessed August 24, 2014,http://download.oracle.com/javase/tutorial/. Eckel, Bruce. 2006. Thinking in Java. 4th ed. Prentice Hall, February 20. Barnes, David J., and Michael Kolling. 2008.Objects First With Java: A Practical Introduction Using BlueJ. 4th ed. Prentice Hall. Pecinovský, Rudolf. 2005. JAVA 5.0 Novinky jazyka a upgrade aplikací. Brno: Computer Press. http://knihy.pecinovsky.cz/java5novinky.
Motto: Write once, run anywhere Sun Microsystem
Demonstration of JAVA applications Desktop applications - http://wordrider.net/freerapid/download.html 3d application - http://www.bytonic.de/downloads/jake2_jogl1 1.jnlp Web application- https://www.edison.cz Mobile applications Others
JAVA is (not only) language Other languages based on JAVA platform exist: Scala JavaFX and others
Required/useful application Installed Java Development Kit (JDK) Eclipse professional IDE BlueJ educational IDE
Features of JAVA technology Multiplatform and portable Object Oriented It has simple language core is API Robust, Dynamic and Secure Multithreaded Support for distributed application
History 1992 - Oak language by Green Project 1995 Oak renamed to JAVA 1996 JDK 1.0 1997 JKD 1.1 - over 220,000 downloads occur in just three weeks 1999 Java 2 platform released 2004 jdk 1.5 (Java 2 Platform, Standard Edition 5) 2006 - jdk 1.6 2011 jdk 1.7 2014 jdk 1.8
Multiplatform and portable Java source code is compiled into machine code - Java Bytecode (.class files) format, that is platform independent. This machine code is executed on Java Virtual Machine (JVM) JVM is a machine that is implemented as software running in various operating systems. JVM specification provides concrete definitions for implementation of instruction set, register set, class file format, stack...
Keywords abstract const finally int public this boolean Continue float interface return throw break default for long short throws byte do goto native static transient case double if new strictfp try catch else implements package super void char extends import private switch volatile class final instanceof protected synchronized while
Aspects of Java Programming Object construction Method calling Data types Modification of an object state Class definition Java Flow Control Packages Exceptions and Exception Handling
Object, Type, and Class An Object is an distinguishable entity that has: Identity: a uniqueness which distinguishes it from all other objects Behavior: services it provides to another objects State: value of attributes held by an object A class is an abstraction of objects with similar implementation Class is definition of set of similar objects Every object is an instance of one class
Object responds to a message call
Return value and data types
Data type Primitive types only values: int is in [-2147483648, 2147483647 ] boolean is in {false, true} double is in [4.9*10-324, 1.7976931348623157*10 308 ] Object types reference to instance of class: type from JAVA (more than 18000) e.g. String defined by user e.g. Rectangle
Reference vs. instance Another instance is created only by operation new. Reference to the same instance is passed during assignment.
Peaceful destruction of an object When an object is no longer being used, it could release its memory space. Java virtual machine knows that it is not used whether no reference to it exists.
Garbage collector our friend The collection and freeing of memory is the responsibility of a thread of code called automatic garbage collector (GC). GC starts: low memory explicit start System.gc() The garbage collector keeps track of all memory allocated with the new key keyword and also tracks who has access to that memory. When the access count reaches zero, the memory can be collected and freed.
A Creation of customized object We could call constructors and method with
Parameterized methods
Overloading Definition of another constructor or method with same name is called overloading Overload method must differs with number or type of parameters Specific constructor or method is selected base on passed arguments
Class definition
Name of a class identifier It follows conventions Name should be noun, in mixed case with the first letter of each internal word capitalized.
Methods define behavior of objects Type of return value. void means that there is returned no value. parameters public class Tree { // //definition of method public void setposition(int x, int y) { trunk.setposition(x, y); crown.setposition(x, y); //...body of method } Method that returns value should contain return statement in its body. This statement ends method execution and result of followed expression is return value. public int getheight() { return crown.getheight() + trunk.getheight(); } // }
Attributes State of an object is based on value of its
Attributes defines possible states of objects Modifier of accessibility private accessibility is recommended public class Tree { // //definition of attributes private Rectangle trunk ; private Ellipse crown ; private int num = 7, count; // } Name of attribute. Init value of attribute. Type of attribute.
Accessor and mutator Accessor and mutator form one property. Using properties has benefits public class Rectangle { private int x; } public int getx() { return x; } Attributes are defined as private. It is accessible only by current class. public void setx(int x) { if (x < 0 x + šířka > PLÁTNO.getŠířka()) throw new IllegalArgumentException("X :" + x + " is out of range."); this.x = x; } Class defines method for reading accessors and writing mutators.
Class methods Named also as static. Class method is called directly on class.
Class attributes Named also as static. Class attribute is stored directly in class Their value is shared among of instances of class Modified usually by class method invocation.
A definition of class methods and attributes Class method and attribute is defined with modifier static before or after modifier of accessibility. public class Tree { //... //class attribute static private int step = 25; Access to class attribute step should be qualified with name of class due the name collision with name of parameter. //class method static public void setstep(int step) { Tree.step = step; } //... }
Local variables Defined inside body of a method. Visible after declaration and only inside given method. Destroyed when the method is leaved. Defined only type no other modifier. public Tree(int x, int y, int height, int width) { //local variable definition It have to be initialized int h3 = height / 3; before first using. } new Ellipse(x, y, width, h3*2, MyColor.GREEN); new Rectangle(x + 9*width/20, y + h3*2, width/10, h3, MyColor.BROWN);
Constants Preferred to using magic values. Class and instance attribute, method parameter and local variable defined with modifier final.
Constants literals Constant described by its value. //boolean literals boolean t = true, f = false; int decimal = 123, //decimal format of literal octal = 0173, //octal format of literal binary = 0b1111011, //binary format of literal hexa = 0x7b; //hexadecimal format of literal double d_1 = 12.3, //decimal format d_2 = 12., //decimal format d_3 =.12, //decimal format sl_1 =.314e+1, //semilogarithmic format sl_2 =.314e1, //semilogarithmic format sl_3 = 31.4e-1, //semilogarithmic format sl_4 = 3.14E0; //semilogarithmic format String str_1 = "Hello world!", //simple string str_2 = "Hello world!" + //string split on more lines "I am your friend." + "and my name is \"Joe Smith\"",//escape sequence \" represents " str_3 = "C:\\windows\\format.exe", //escape sequence \\ represents \ str_4 = "user joe\npass 123456\n"; //escape sequence \n represents new line Object obj = null; //represents variable without reference
Comments and documentation //comment on one line /** * Comment included in automatically generated HTML documentation (known as * JAVADOC). * {@code Tree} is demonstration class. Only method {@link Tree.moveDown(int)} is *defined. * @author koz01 * @version 0.1 * */ public class Tree { /** * Demonstration method * @param step description of arguments * @return description of return value * @throws IllegalArgumentException */ public boolean movedown(int step) throws IllegalArgumentException{ /* * Comment on one or more lines * There wil be placed some genuine implementation. */ return false; }
Class organization convention constant class attributes, other class attributes, constant instance attributes, class properties, other class methods, constructors, abstract methods, instance properties, other instance methods, private class methods, private instance methods, inner classes.
Binary operators Arithmetic operation + - * - for int and double operands. Division / and division residuum % - different for int and double. String concatenation +.
Unary operators
Assignment operators Simple assignment = For arithmetic operators exist composite assignment +=, -=, *=, /=, %=
Operator of increment and decrement Two different forms - prefix and postfix. increment ++ decrement --