Escolar Documentos
Profissional Documentos
Cultura Documentos
Programming
JSE &
Lect. Catalin Boja, Ph.D.
JCA
IT&C Security Master
catalin.boja@ie.ase.ro
www.ism.ase.ro
Course organization
• Activities: Course 50% + Laboratory 50%
• Language: English
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
Compile – javac.exe
=>
Bytecode
.class file
Execute – java.exe
=>
Executed by JVM
http://www.itcsolutions.eu/2010/11/29/tutorial-java-1-prerequisites/
www.ism.ase.ro
www.ism.ase.ro http://www.itcsolutions.eu/2010/12/15/tutorial-java-2-basic-concepts/
2009-2010 © ism.ase.ro Catalin Boja
Java fundamentals
• one line comments defined by //
• multiple lines comments defined between /* and */
• the end delimiter for a instruction is ; (semicolon);
• commented instructions are ignored;
• instructions can be associated in blocks of code that
are defined between { and };
• Java language is case sensitive, vb as variable is
different than Vb or VB;
• EVERYTHING is defined inside a class
• You CAN NOT define GLOBAL variables or methods
outside a class (like you did in C/C++);
www.ism.ase.ro
X
Student a1(2345, "Maria");
Student a2(231, "Ana");
a2 = a1;
pa1->print(); a1.print();
pa2->print(); a2.print();
if(a1 == a2) cout << "\n\t equal VALUES"; if (a1 == a2) System.out.println("\t equal
if(pa1 == pa2) cout<< "\n\t equal REFERENCES");
POINTERS"; else System.out.println("\n NOT equal
www.ism.ase.ro else cout<< "\n\t NOT equal REFERENCES"); }
POINTERS" }
© 2010 Catalin Boja 14
}
Java fundamentals
C++ vs. Java
•objects managed by value and • objects are managed only by
reference; references
• pointers from C++ defined with *
• a class may contain dynamic
can be used only in native code – JNI;
attributes managed by • memory clean-up is done by the
pointers; JVM garbage collector;
• the destructor is used to • a destructor like method (finalize)
release memory space and to used to clean up other resources
• you CAN’T overload operators;
avoid memory leaks;
• operator = does ALLWAYS shallow
• you must define copy copy ;
constructor and overload = • copy constructor needed to make
operator to prevent default deep copy;
shallow-copy • strings managed by String (object
used like a vaue-type);
• strings managed by char *
www.ism.ase.ro
www.ism.ase.ro
For more information check How to define primitive data types variables post
2009-2010 © ism.ase.ro Catalin Boja
Java fundamentals
Default values in Java (NOT for local variables;
ONLY for instance variables):
Type Default value
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char ‘\u0000’
boolean false
www.ism.ase.ro reference null
2009-2010 © ism.ase.ro Catalin Boja
Java fundamentals
Converting a value type into a reference type and backwards is
done by boxing / unboxing
public class BoxUnbox
{
static void main(String[] args)
STACK HEAP
{
int i = 123; 4 bytes: 123
Integer iObject; reference null
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
Pers p1
Pers p1
Pers p1
www.ism.ase.ro
Pers p1
2009-2010 © ism.ase.ro Catalin Boja 24
Java fundamentals
Arrays
• How to add a new element to a Java array
In this post we see what is the solution to the problem of
adding a new element to an existing array.
• How to copy values of an array into another array
In this post are described methods used to copy one array
values into another array.
• Matrixes and Multidimensional Arrays
In this post we will see what are and how to define
multidimensional arrays. The most common
multidimensional array is the matrix – a two dimension
array.
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
Shallow copy
reference
a1 = a2
reference X bytes : values
www.ism.ase.ro
www.ism.ase.ro
base constructor
class Base{
int attribute1;
int attribute2;
}; inheritance
Subclass
class Subclass: Base{
int new_attribute;
};
www.ism.ase.ro Base constructor
2009-2010 © ism.ase.ro Catalin Boja
Java fundamentals
Inheritance
UPCASTING – it is allowed to manage a subclass
reference using a base reference
class Base{
Base
…
};
class Subclass : Base{ X
…
}; Subclass
www.ism.ase.ro
www.ism.ase.ro
class Base{
int Method1(int a){…}
};
class Subclass: Base{ call to the base class
int attribute_new;
int Method1(int a){…}
int Method2(int a){ super.Method1(a);}
www.ism.ase.ro
};
2009-2010 © ism.ase.ro Catalin Boja
Java fundamentals
Inheritance
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
Overriding Object
methods = overloading
them
www.ism.ase.ro
Inheritance vs Coposition
the subclass is a
special form of the
class Vehicle{ base class;
…
};
Inheritance vs Coposition
the class has an
instance variable of
class Engine{ other class type ;
…
};
class Auto {
www.ism.ase.ro
Engine engine;
}; 2009-2010 © ism.ase.ro Catalin Boja
Java fundamentals
Inheritance
• operator instanceof is used to test whether an
object is an instance of some class;
• conversion from a base class reference to a
subclass one is done with the cast operator:
www.ism.ase.ro
www.ism.ase.ro
interface IOperations {
void Operation1( );
void Operation2( ); override interface method
};
class Base implements IOperations {
public void Operation1() {…}
public void Operation2() {…}
}
www.ism.ase.ro
www.ism.ase.ro
[Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides - Design Patterns
Elements of Reusable Object-Oriented Software, Addison-Wesley Pub Co; 1st
edition (January 15, 1995)]
www.ism.ase.ro
www.ism.ase.ro
Function CALL
CALLBACK function 1
www.ism.ase.ro function 2
RESULT
94
© 2010 Catalin Boja
Java fundamentals
Callback & Events
- An event is a message sent by an object to announce something (GUI user interaction -
mouse click, button click or program logic – application routines)
handle event
raise event
- In a event handle model, the object that manages the event (and raises it) doesn’t know
who what method will receive and handle it; that’s why it is needed something that will
connect the source and the destination (in Java this is ????)
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
<<interface>>
Collection<E>
<<interface>> <<interface>>
List<E> Set<E>
www.ism.ase.ro
<<interface>>
Map<K,V>
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
Java fundamentals
Files
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro
<<interface>> <<interface>>
Serializable DataOutput
www.ism.ase.ro
www.ism.ase.ro
Object Graphs
www.ism.ase.ro
Java fundamentals
Serialization
• you can override the mechanism:
private void writeObject(ObjectOutputStream
os){
os.defaultWriteObject();
//other data
}
www.ism.ase.ro
www.ism.ase.ro
Executing threads
www.ism.ase.ro
www.ism.ase.ro
www.ism.ase.ro