2. CONSTRUCTOR.
import java.util.*;
public class Hi {
// here value is copied in localVariable 'a'.
public static void incrementPrimitiveDT(int a) {
a++;
}
// here reference is copied, means 'input' is also pointing to 'arr'.
public static void incrementReferenceDT(int input[]) {
for(int i=0;i<input.length;i++) {
input[i]++;
}
}
public static void main(String[] args) {
int i=10;
incrementPrimitiveDT(i); // passByValue
System.out.println(i); // 10
int arr[]= {1,2,3,4,5};
incrementReferenceDT(arr); // passByReference
System.out.println(Arrays.toString(arr)); // [2, 3, 4, 5, 6]
}
}
- Object creation and memory allocation in Integer class.
public static void main(String[] args) {
// case 1:
Integer i= new Integer(1);
Integer j=new Integer(1);
System.out.println(i==j); // false, coz == check reference address of i and j, and here 'i' is pointing to 1 (i---> 1) and 'j' is pointing to different 1 (j---> 1)
System.out.println(i.equals(j)); // true
// case 2:
Integer a=new Integer(2);
Integer b=2;
System.out.println(a==b); // false
System.out.println(a.equals(b)); // true
// case 3:
Integer x=new Integer(3);
Integer y=x;
System.out.println(x==y); // true
System.out.println(x.equals(y)); // true
// case 4
Integer p=128;
Integer q=128;
System.out.println(p==q); // false
System.out.println(p.equals(q)); // true
// case 5 -128 to 127 is in cached memory so they point to the same object without creating its instances.
Integer s=50;
Integer t=50;
System.out.println(s==t); // true
System.out.println(s.equals(t)); // true
}
- When Divedend < Divisor then answer is dividend.
- When Divedend > Divisor then answer lie in between [0 to divisor-1].
- To Inject B in A we ADD(+) B*INF in A and store it in A, where INF is any number greater than A and B.
- to extract old number i.e., A from modified A we do (A % INF).
- to extract new number(Injected number) i.e., B from modified A we do (A / INF).
int a=5;
int b=4;// number to be injected
int INF=9999;
a=a+(b*INF);
System.out.println(a%INF); // 5
System.out.println(a/INF); //4
Note: When there is no reference variable pointing to the actual object then it is deleted automatically when garbage collection hits.
public class Demo{
public static void main(String[] args) {
Human obj=new Human();
Human son=obj; // this is his mom who calls obj as hisSon.
Human bro=obj; // this is his sister who calls obj as hisBro.
son.hairCut="done"; // here his mom told him to get a haircut.
// here his sister is also able to see his haircut.
System.out.println(bro.hairCut); // done
}
}
class Human{
String name="Vikash";
String hairCut="notDone";
}