Curs 02
Curs 02
MODELUL DE MEMORIE.
Alexandru Olteanu
{[email protected]}
int x; // tip de date primitiv
MyClass myobject ; // tip de date structurat , clasa
void print() {
cout<<"("<<this.x<<","<<this.y<<")"<<endl;
}
}
void print() {
System.out.println("("+this.x+","+this.y+")")
}
}
In Java:
@Getter
@Setter
public class University {
String name;
}
Project Lombok
CONSTRUCTORI
Default constructor
Parameterized constructor
Copy constructor
Destructor
Default constructor (no-arg constructor): provides
the default values to the object like 0, null etc.
depending on the type
public class Magazin {
private String brand ;
public Magazin () {
brand = "de inchiriat ";
}
}
...
Magazin item = new Magazin () ;
Parameterized constructor: provide different
values to the distinct objects
public class Magazin {
private String brand ;
public Magazin ( String brand ) {
this.brand = brand ;
}
}
...
Magazin item = new Magazin (" IKEA ") ;
A class can have any number of constructors that
differ in parameter lists. The compiler
differentiates these constructors by taking into
account the number of parameters in the list and
their type
public class Magazin {
private String brand ;
private Integer suprafata ;
public Magazin ( Integer suprafata ) {
this.suprafata = suprafata ;
this.brand = "de inchiriat ";
}
public Magazin ( Integer suprafata , String brand ) {
this.suprafata = suprafata ;
this.brand = brand ;
}
}
In Java, there is no Copy Constructor per-se, but
there are many ways to copy the values of one
object into another:
By constructor
By assigning the values of one object into
another
By clone() method of Object class
public class Training {
private int pushups ;
private int crunches ;
public Training ( Training otherTraining ) {
this . pushups = otherTraining . pushups ;
this . crunches = otherTraining . crunches ;
}
}
In C++, destructorul se apeleaza la iesirea din scope
a variabilei:
class Catalog {
private :
int * note ;
public :
Catalog (int count = 20) {
note = new int [ count ];
}
~Catalog () {
delete [] note ;
}
};
int main () {
Catalog * grupa321CD = new Catalog (30) ; // -> constructor
...
delete grupa321CD ; // -> destructor
}
In C++, destructorul se apeleaza la iesirea din scope
a variabilei:
class Catalog {
private :
int * note ;
public :
Catalog (int count = 20) {
note = new int [ count ];
}
~Catalog () {
delete [] note ;
}
};
int main () {
Catalog * grupa321CD = new Catalog (30) ; // -> constructor
...
//delete grupa321CD ;
} // -> destructor
In Java exista functia finalize, dar cu adevarat nu
stim cand este apelata pentru ca de gestiunea
memoriei se ocupa Garbage Collector
Java Garbage Collection Basics (Oracle)
MODELUL DE
MEMORIE
Pointeri vs Referinte in C/C++
int x = 13 , y = 14;
int *px = &x, &rx = x;
int *px , ℞ // eroare : referintele sunt initializate la crea
px = &y; rx = y; // nu se modifica referinta , ci variabila refe
px ++; rx++; // nu se modifica referinta , ci variabila referita
Pointeri vs Referinte in C/C++
void swap (int first, int second) {
int temp = first; first = second; second = temp;
}
source: jenkov.com
În Java, variabile din tipuri de date primitive pot fi
locale unei metode sau membrii unui obiect
public class Album {
public static void main ( String [] args ) {
int price ;
}
}