CSE 14 Lecture Notes - Lecture 17: Reference Type, Primitive Data Type

36 views6 pages
1 Jun 2018
School
Course
Professor
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:
“tig  = hello;
 = goode;
The aiale  of “tig tpe as fist iitiated to hello ut the got oeidde  goode.
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:
“tig  = e “tighello;
“tig  = e “tighello;
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 sie oth  ad  hae the “tig hello alue ad .euals ol
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);
//Ee if oth aiales otai the sae “tig hello, this ould etu false. Wh is that?
Let’s t to udestad agai hat happes he eo is alloated ito aiales. Daing 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
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in
Example:
class ComparingTest {
public static void main(String [] args) {
“tig  = e “tighello;
“tig  = e “tighello;
boolean z1 = x.equals(y);
boolean z2 = x == y;
“tig a = e “tiggoode;
String b = a;
boolean c1 = a.equals(b);
boolean c2 = a == b;
“ste.out.pitl is:  + ;
“ste.out.pitl is:  + ;
“ste.out.pitl.euals is:  + z;
“ste.out.pitl ==  is:  + z;
“ste.out.pitla is:  + a;
“ste.out.pitl is:  + ;
“ste.out.pitla.euals is:  + ;
“ste.out.pitla ==  is:  + ;
}
}
Lets look at this example very carefully and see what is going on. We have variables x and y of
“tig tpe that ae akig a e “tig 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
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents