CSE 14 Lecture Notes - Lecture 17: Reference Type, Primitive Data Type
CMPS12A Lecture 17: Understanding Memory and Wrapper Classes
In coding, the value that we put in a variable will be stored in memory where the variable itself
points to the value. We know that we can override an inherited method so it can be a new
value.
Example:
“tig = hello;
= goode;
The aiale of “tig tpe as fist iitiated to hello ut the got oeidde goode.
In this way, we cannot narrow access it; so this is called override. There is also overload where
you create a new function with the same name but must use a different function heading; this
can have any access level.
Example:
“tig = e “tighello;
“tig = e “tighello;
If we were to use the .equals() method and compare these two variables, so:
boolean z = x.equals(y);
System.out.println(z);
//We would get true sie oth ad hae the “tig hello alue ad .euals ol
compares the value itself. There is another way that you may have thought of already to also
compare these variables and that is the == method. So:
boolean a = x == y;
System.out.println(a);
//Ee if oth aiales otai the sae “tig hello, this ould etu false. Wh is that?
Let’s t to udestad agai hat happes he eo is alloated ito aiales. Daing it
out would be of more useful but first lets make a code.
find more resources at oneclass.com
find more resources at oneclass.com
Example:
class ComparingTest {
public static void main(String [] args) {
“tig = e “tighello;
“tig = e “tighello;
boolean z1 = x.equals(y);
boolean z2 = x == y;
“tig a = e “tiggoode;
String b = a;
boolean c1 = a.equals(b);
boolean c2 = a == b;
“ste.out.pitl is: + ;
“ste.out.pitl is: + ;
“ste.out.pitl.euals is: + z;
“ste.out.pitl == is: + z;
“ste.out.pitla is: + a;
“ste.out.pitl is: + ;
“ste.out.pitla.euals is: + ;
“ste.out.pitla == is: + ;
}
}
Lets look at this example very carefully and see what is going on. We have variables x and y of
“tig tpe that ae akig a e “tig ith alues of hello and we have two variables z1
and z2 of Boolean type that will return true or false depending on we have them point towards
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
Cmps12a lecture 17: understanding memory and wrapper classes. In coding, the value that we put in a variable will be stored in memory where the variable itself points to the value. We know that we can override an inherited method so it can be a new value. The (cid:448)a(cid:396)ia(cid:271)le (cid:454) of t(cid:396)i(cid:374)g t(cid:455)pe (cid:449)as fi(cid:396)st i(cid:374)itiated to (cid:862)hello(cid:863) (cid:271)ut the(cid:374) got o(cid:448)e(cid:396)(cid:396)idde(cid:374) (cid:271)(cid:455) (cid:862)good(cid:271)(cid:455)e(cid:863). In this way, we cannot narrow access it; so this is called override. There is also overload where you create a new function with the same name but must use a different function heading; this can have any access level. If we were to use the . equals() method and compare these two variables, so: boolean z = x. equals(y); //we would get true si(cid:374)(cid:272)e (cid:271)oth (cid:454) a(cid:374)d (cid:455) ha(cid:448)e the t(cid:396)i(cid:374)g (cid:862)hello(cid:863) (cid:448)alue a(cid:374)d . e(cid:395)uals(cid:894)(cid:895) o(cid:374)l(cid:455) compares the value itself.