Secure Applications Programming Java
Secure Applications Programming Java
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
www.ism.ase.ro
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;
www.ism.ase.ro
www.ism.ase.ro
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 20
}
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 - variables
• variable name must begin with a letter,
underscore symbol (_) or dollar sign ($);
• variable names can not begin with a digit;
• after first character, you can use digits in the
variable name;
• variable name can not be a word reserved for
Java language, a keyword;
• several variables can be defined
simultaneously;
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
String is Immutable
www.ism.ase.ro
www.ism.ase.ro
String Constant Pool
2009-2010 © ism.ase.ro Catalin Boja
Java fundamentals – String and
Immutable
Method Description
charAt() returns the char at a given index; index takes values from 0 to length()-1;
concat() appends a String to the end of another; the same as +
equals() compare at case level 2 String values
length() return the number of chars; IT IS NOT the length attribute of an array. IT IS A
METHOD
replace() replace occurences of a char with a given one
substring() returns a substring
toLowerCase() converts all chars to lowercase
toString() returns the value of the String object
toUpperCase() converts all chars to uppercase
trim() remove whitespace from the end
www.ism.ase.ro
These method affects the value of the calling object, StringBuilder or StringBuffer.
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
for( intialization; ; )
for( ; condition; iteration )
for( ; ; iteration)
for( ; ; ) // endless loop
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 66
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
import packageName.className;
• if you want to use all classes from another package you define the
import like this:
import packageName.*;
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
class Person{
class Person{ public void Eat(){…}
public string SaySomething(){…} public void Eat(Food food){…}
} }
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 153
© 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
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
www.ism.ase.ro
www.ism.ase.ro
Copyright © 2011 Qualitance QBS s.r.l. &
Catalin Boja
Java advanced - Generics
public class Box {
VS.
public class GenericBox<T> {
T value; //generic type
public void setValue(T value) { this.value = value;}
public T getValue() {return value;}
}
www.ism.ase.ro A generic class – generics aproach
Copyright © 2011 Qualitance QBS s.r.l. &
Catalin Boja
Java advanced - Generics
• Wildcard syntax allows a generic method to
accept subtypes (or supertypes) of the
declared type;
• The ? wildcard is called the unbounded
wildcard and denotes that any type
instantiation is acceptable;
List<?> anyList = new ArrayList<Date>( );
anyList = new ArrayList<String>( );
www.ism.ase.ro
www.ism.ase.ro
Copyright © 2011 Qualitance QBS s.r.l. &
Catalin Boja
Java advanced - Generics
Generic classes:
• Represent template classes (like in C++) –
descriptions of classes with parameters;
• Can be adapted to real types (Java types + user
defined);
• Creating instances, the JVM generates real
classes;
• A generic class requires one or more type
parameters
www.ism.ase.ro
Java advanced - Generics
public class TestGenerics<T> {
T instanceVariable;
T[] array
TestGenerics(T input){ type variable
instanceVariable = input;
}
T getInstance(){
return instanceVariable;
}
www.ism.ase.ro
Copyright © 2011 Qualitance QBS s.r.l. &
166
Catalin Boja
Java fundamentals
Collections
• to use own objects in collections you need to
override Object methods:
– boolean equals (Object obj)
– int hashCode()
• to sort object you need to implement
Comparable (int compareTo(Object)) or
Comparator(int compare(Object one, Object
two)) interface
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
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
www.ism.ase.ro
2 – spends x
3 – check the balance
4 – spends y
Thread Thread
5 – check the balance Bank account
“Wife” “Husband”
with a balance
6 – check the balance
8 – spends y
Locked by
3 – check the balance
“Husband”
Thread Thread
4 – spends y
“Wife” “Husband”
5 – check the balance
6 – check the balance
Locked by
“Wife”
X
6 – spends x waits the release of resource
8 – spends y
www.ism.ase.ro
Family bank account
2009-2010 © ism.ase.ro Catalin Boja
Java fundamentals
Threads
Concurrency
• make syncronized methods or blocks that
access common resources;
• each object has one lock (managed by the
JVM);
• declare common variables as volatile –
modifications are discarded into memory
• use wait(), notify() and notifyAll() inherited
from Object;
www.ism.ase.ro
while (!condition) {
this.wait();
}
www.ism.ase.ro
[3]
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
n – modulus
e – public exponent
d – private exponent
size of n determines how many bits the RSA key
p and q need to have a bit length half that of the key size.
[4]
www.ism.ase.ro