We covered the basics in part 1 now lets see some other stuff that might confuse you.
You should focus on references because it is an essential part of Java and will greatly help if you try learning another object oriented language
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
public class App { public static void main(String[] args) { Person person1 = new Person("metin"); // person1 is a reference/pointer to an object of Person named metin Person person2 = new Person("metin2"); // person2 is a reference/pointer to an object of Person named metin2 person2 = person1; // now person2 is referencing/pointing person1 // what that means is person2 and person1 are pointing to the same object from Person // basically they are the same System.out.println(person1.getName()); // result is : metin System.out.println(person2.getName()); // result is : metin // since they point to the same object, which is a person with name metin // if you change the name with setName of person1 or person2 // next time you print out the name it will be the same again person2.setName("metin3"); System.out.println(person1.getName()); // result is : metin3 System.out.println(person2.getName()); // result is : metin3 // so all we did was change the object's name that person2 and person1 pointed // in others words both their names changed, because they point to same object // what happened to the object you created with name metin2? // you know the one person2 was pointing at the beginning // well that one is going to be cleaned/deleted from memory and from our application // because there is no pointer pointing to it } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.ArrayList; public class App { public static void main(String[] args) { ArrayList<Person> list = new ArrayList<Person>(); // initialize the list // looks similar like Person object when initializing doesent it? except for pointy brackets // Person in the pointy brackets simply means this list will work with Person objects Person person1 = new Person("metin"); Person person2 = new Person("metin2"); Person person3 = new Person("metin3"); list.add(person1); list.add(person2); list.add(person3); // now there are 3 objects inside the list list.remove(0); // removed the first object in the list -> person1 list.remove(person2); // removed person2 from the list // as seen here you can either remove an object with its index in the list(starting from 0) // or the object itself } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import java.util.ArrayList; public class App { public static void main(String[] args) { ArrayList<Person> list = new ArrayList<Person>(); Person person1 = new Person("metin"); Person person2 = new Person("metin2"); list.add(person1); list.add(person2); // now the fun begins Person person3; person3 = list.get(0); // person3 is pointing to person1, which means person3 is actually person1 person3 = list.get(1); // person3 is pointing to person2, which means person3 is actually person2 person2 = list.get(0); // person2 is pointing to person1, which means person2 is actually person1 person1 = list.get(1); // person1 is pointing to person2, which means person1 is actually person2 System.out.println(person1.getName()); // result : metin2 System.out.println(person2.getName()); // result : metin System.out.println(person3.getName()); // result : metin2 } } |
Q : References/Pointers are hard to learn, do I really need to learn it?
A : I just cant stress enough how important it is for java so definitely YES, trust me it will be really rewarding in the future.
Q : Are there other containers/data structures like ArrayList?
A : Yes, but they are almost similar to ArrayList.
Q : Can my Person pointer like person1 point only Person objects?
A : Yes. Unless there is Inheritance involved, but leave Inheritance for later.
Q : When should I learn Inheritance, Generics, Comparable and other stuff?
A : You have to be really comfortable with plain java objects like in this tutorial. So complete this tutorial series and do lots of exercises. Example create your own class(es) and write similar code like in this part.