int, double, float, String, boolean, literals
Object representations of data.
But for now, here's what I know:
- Method Area:
- Stores methods in categories:
- Static fields
- Constants
- Constructors
- Methods
- Heap Area:
- Objects and arrays are created here.
- JVM Stack Area:
- A frame is added whenever a method is called.
- When methods in the stack are fully executed, the frame is removed (via Garbage Collection - System.gc).
== and != compare values.int a = 1;
int b = 1;
System.out.println(a == b); // true
String a = new String("a");
String b = new String("a");
System.out.println(a == b); // false
a == b is false because String a and String b are different objects with different memory addresses.String a = new String("a");
String b = a;
System.out.println(a == b); // true
a == b is true because b now references the same object as a.NULL, meaning they are not referencing any object.null reference.int[] arr = null;
arr[0] = 10; // NullPointerException
String str = null;
System.out.println(str.length()); // NullPointerException
Both examples cause a NullPointerException because arr and str are not pointing to any object.
new KeywordUsed to create a new object for a reference type variable.
String a = new String("a");
In this case, the value "a" is passed to the constructor of String.
.equals() for String Comparison.equals() evaluates the value of the string object, not the memory address like ==.
boolean ret = str1.equals(str2);
int a = 1;
int b = a;
a += 2;
System.out.println(a); // 3
System.out.println(b); // 1
int b = a is executed, b gets a copy of a's value. Therefore, when a is modified, it doesn't affect b.Pretty much in most langauges, Strings are immutable!
I think that's why things like this happen:
String a = new String("a");
String b = a;
a += "bcd";
System.out.println(a); // "abcd"
System.out.println(b); // "a"
What is going on here?
a is concatenated with "bcd", Java creates a new String object. a and b are now referencing different objects.