Great video and great explanation:D. Indeed very talented lecturer!! Only the last part of the video is wrong: String s1 = "Hello"; String s2 = "Hello"; System.out.println(s1==s2); This is actually true, because when you don't use the new operator the String value is placed in the String pool. As a result, s1 and s2 are pointing to the same memory address. I think the Professor forgot the new operator and meant the following: String a = new String("Hello"); String b = new String("Hello"); System.out.println(a==b);
Björn Schuller You are right. There is an optimization for String class. (When you get a new instance they are not same even though their computed hashcodes are the same. ) What about concatenating strings ? It is said that when you concat strings as follows it works as if you 'new' Strings and uses memory inefficiently. So using StringBuilder(StringBuffer for threadsafe) is recommended. Java, --returns NOT Equals !!! . Unless you won't write s1.Equals(s1,s2+"lo") String s1, s2; s1 = "Hello"; s2 = "Hell" ; if(s1==(s2+"o")) { System.out.println("Equals!"); }else { System.out.println(s1+" != "+s2+"o"); // OoooPs! } C# -- They are all equals string s1, s2; s1 = "Hello"; s2 = "Hel" ; if (s1 == s2 + "lo") { Debug.WriteLine("they are equal"); } if (s1.Equals(s2 + "lo")) { Debug.WriteLine("they are equal"); } In C# there are object.Equals and object.ReferenceEquals. Their values are equals which can be verified by object.Equal(o1,o2) and for references object.ReferenceEquals(o1,o2). So, == double equal operator works differently among programming languages. To check if values are the same in Java you should use yourObject.Equals(otherOne);.... but == checks references. What's more in .Net you can 'new' some structs such as DateTime and structs are value types. Not object types. Thus, it doesn't mean that object types are the only types you can 'new' it. You can overload operators in .Net but not in Java. According to my job I use both of the languages and at first glance there are lots of similar things but there are various differences underlying .
Topics: Memory, Different Sections of Memory for Different Types of Variables, Memory Allocation Mechanics, The Pointer Viewpoint, The Binky Pointer Fun Video (see.stanford.edu/Course/CS106A)
Amezing explanation on JMM. s1==s2 will be true because String litral pool does not create same hello more than once so both s1 and s2 will hold same memory location and result will be true.
hey, there is a mistake. Why word = 4 bytes? in 3:02. In data structure, 1 WORD = 2 BYTES=16BITS, 1 DWORD = 4 BYTES= 32 BITS, 1 QWORD = 8BYTES. Who can explain that?
Great lecture but last part about string is wrong. String literals are stored in the string pool where identical strings are referred to the same memory location. So (s1 == s2) produce true, unless if new keyword is used to create the string.
Now I know how to recognize former CS1006A guy among my Java-oriented students! First. they know that a computer has a stack; And they try to do calculations in _hexadecimal_ ;-)
They are using Java SE 6 (2006). Most of the code is still relevant. Java updates: Java SE 7 (2011), Java SE 8 (2014), Java SE 11 (2018), Java 17 (2021). If you are using Stanford’s Karel JAR you will need Java SE 6 for its applet support.
Point object is in the heap. When we refer to an object (as opposed to assigning a variable to a primitive type), the local variable takes the value of address of the object in the heap. But firstly we allocate memory and then call the constructor method to initialise instance variables. So constructor, and move are in the stack. Does that clarify things?
@j0natan This may be and old question but... Do you mean 'why do you have to declare a Point as a "New Point()" because you said it was of type Point: "Point myPoint"? If so, it is because: When you say "Point myPoint" it is making a variable of type point. The object is, at this moment NULL. When you say "= new Point(x, y)", it then assigns the address to this 'new' Point object, and changes the NULL variable. Does that make sense? :P
very good lecture, one thing I don't understand why do you have to declare the pointers as different Objects cant it just be a declaration like Pointer pointer = new Object(); Pointer is just and example
does anybody know what happened to the course website. I've spent the last 30 minutes searching my computer and the internet for it. Hopefully the page hasn't been taken down. Please let me know. I'm only half way through the course and plan on finishing.
'this.' contains the address of where the current object lives on the heap. It is created and destroyed on the stack along with the local variables of the method. 'this.' refers to the current object
Tnaca7 Probably 4 months too late, but just in case some one else reading these. You can get them here stanford.edu/class/archive/cs/cs106a/cs106a.1142/lectures/
+beingme2345 Actually, the lowest value you can represent with 8 bits is 00000000 and the highest is 11111111 (255 in decimal) so the range of 1 byte is [0 to 255] which is 256 integers.
I need a drink. I don't drink but I think I need a drink. Actually that second example cleared it up for me the first example had my head spinning. The Heap and Stack pictures help a lot it's nice to have a mental picture of what is going on. I've read about pointers before and I thought I understood but those examples never mentioned Heap and Stack so this clears it up quite a bit. Actually no I lied the "this" still confuses me.
this explains why everyone cannot be a teacher.. and its an art ... amazing job ... i want to sit with clean slate ;-)
Very talented lecturer! His students are very lucky. :-)
Stanford, we want more courses taught by Professor Mehran please!
I agree. The world needs more Mehran!
yeah
he makes "heap" and "stack" go being happy couple in my head now. Best CS professor ever!!!
I am here in 2020 and this lecture still excites me. Amazing professor.
Great video and great explanation:D. Indeed very talented lecturer!! Only the last part of the video is wrong:
String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1==s2);
This is actually true, because when you don't use the new operator the String value is placed in the String pool. As a result, s1 and s2 are pointing to the same memory address. I think the Professor forgot the new operator and meant the following:
String a = new String("Hello");
String b = new String("Hello");
System.out.println(a==b);
yes, you're right.
Björn Schuller You are right. There is an optimization for String class. (When you get a new instance they are not same even though their computed hashcodes are the same. )
What about concatenating strings ?
It is said that when you concat strings as follows it works as if you 'new' Strings and uses memory inefficiently. So using StringBuilder(StringBuffer for threadsafe) is recommended.
Java, --returns NOT Equals !!! . Unless you won't write s1.Equals(s1,s2+"lo")
String s1, s2;
s1 = "Hello";
s2 = "Hell" ;
if(s1==(s2+"o"))
{
System.out.println("Equals!");
}else
{
System.out.println(s1+" != "+s2+"o"); // OoooPs!
}
C# -- They are all equals
string s1, s2;
s1 = "Hello";
s2 = "Hel" ;
if (s1 == s2 + "lo")
{
Debug.WriteLine("they are equal");
}
if (s1.Equals(s2 + "lo"))
{
Debug.WriteLine("they are equal");
}
In C# there are object.Equals and object.ReferenceEquals. Their values are equals which can be verified by object.Equal(o1,o2) and for references object.ReferenceEquals(o1,o2).
So, == double equal operator works differently among programming languages. To check if values are the same in Java you should use yourObject.Equals(otherOne);.... but == checks references.
What's more in .Net you can 'new' some structs such as DateTime and structs are value types. Not object types. Thus, it doesn't mean that object types are the only types you can 'new' it. You can overload operators in .Net but not in Java.
According to my job I use both of the languages and at first glance there are lots of similar things but there are various differences underlying .
Meybe for old jvm versions. Or he forget new String("Hello");
Things are starting to get interesting.
Topics: Memory, Different Sections of Memory for Different Types of Variables, Memory Allocation Mechanics, The Pointer Viewpoint, The Binky Pointer Fun Video
(see.stanford.edu/Course/CS106A)
That yoda comment was gold.. funny guy.
Amezing explanation on JMM.
s1==s2 will be true because String litral pool does not create same hello more than once so both s1 and s2 will hold same memory location and result will be true.
good times..
world class education. a million times better than my uni lecturers
Great Explanation..!!!
Now I have a better understanding about Heap And Stack
Mehran marathon! Good times!
what an energy this teacher has!!! :D
Very informative! I hadn't known this or even really thought about it before watching this lecture. Thanks Stanford!
hey, there is a mistake. Why word = 4 bytes? in 3:02. In data structure, 1 WORD = 2 BYTES=16BITS, 1 DWORD = 4 BYTES= 32 BITS, 1 QWORD = 8BYTES. Who can explain that?
Great lecture but last part about string is wrong. String literals are stored in the string pool where identical strings are referred to the same memory location. So (s1 == s2) produce true, unless if new keyword is used to create the string.
+seenimurugan shanmugam you are absolutely right buddy
I wanted to see the video at the end :(
Day 4
- Chapter 5
- Lectures 8 - 14
Days 5 & 6
- Read
- Do some assignments
Now I know how to recognize former CS1006A guy among my Java-oriented students!
First. they know that a computer has a stack;
And they try to do calculations in _hexadecimal_ ;-)
This is an essential part to programming.
at last a lecture where we can take back something home. wish i had studied at stanford !
i got more clear after this lecture. It's so great
how can we compare int a=5; int b=5; if(a==b)? It checks value, not address
Mehran's eraser marks @13:22 looking like some abstract art
Greatest of all time
Damn, I was asked heap, stack in some interviews. Wish I could have stuck at it through.
The lectures are from 2008. Does anybody know if it’s still relevant with today’s java?
They are using Java SE 6 (2006). Most of the code is still relevant. Java updates: Java SE 7 (2011), Java SE 8 (2014), Java SE 11 (2018), Java 17 (2021). If you are using Stanford’s Karel JAR you will need Java SE 6 for its applet support.
@@dmiserak thank you so much for your comprehensive reply 👍
@@dmiserak can we still use JApplet now? How about the GObject or GRect? And what about the java.acm?
Mehran u r just amazing :) thanks man ...
This is the funky thing!
There are a couple of youtube video download addons in Chrome if you want to try those. Just download them and play them in VLC.
Awesome. Simply awesome.
Does anybody know the video he was going to show to the class, i MUST know.
Probably some HR thing all students had to watch nothing to do with the class.
I wish I had a teacher like him in my undergrad : (
why is Point (2,3) in the heap. but move(int dx, int dy) in the stack. I thought a constructor is method?
Point object is in the heap. When we refer to an object (as opposed to assigning a variable to a primitive type), the local variable takes the value of address of the object in the heap.
But firstly we allocate memory and then call the constructor method to initialise instance variables. So constructor, and move are in the stack.
Does that clarify things?
@j0natan This may be and old question but...
Do you mean 'why do you have to declare a Point as a "New Point()" because you said it was of type Point: "Point myPoint"?
If so, it is because: When you say "Point myPoint" it is making a variable of type point. The object is, at this moment NULL. When you say "= new Point(x, y)", it then assigns the address to this 'new' Point object, and changes the NULL variable.
Does that make sense? :P
Perfect! Perfect! Perfect!
lol
1:05 "I just bought a computer with 2 GB of RAM"
Oh back in 2008
very clear. when I learned the "==" Vs equals(), I were confused.
very good lecture, one thing I don't understand why do you have to declare the pointers as different Objects
cant it just be a declaration like Pointer pointer = new Object();
Pointer is just and example
Objects having own copy of instance variables what cause instance variables are not thread safe
Great teacher.
Monitoring the program syntax and source codes.
does anybody know what happened to the course website. I've spent the last 30 minutes searching my computer and the internet for it. Hopefully the page hasn't been taken down. Please let me know. I'm only half way through the course and plan on finishing.
elcat9091 I think I saw a message somewhere that they had taken it down to revamp it and would put it up again in August.
where is the video ??Stanford
Where is "this" in the stack when calling constructor? I mean "this" is available to be used in constructors, it sure has to be somewhere
'this.' contains the address of where the current object lives on the heap. It is created and destroyed on the stack along with the local variables of the method. 'this.' refers to the current object
I wonder what the video Mehran played at the end is about.
"Professor" has been misspelled in the description.
Are the lecture slides available anywhere?
Tnaca7 Probably 4 months too late, but just in case some one else reading these. You can get them here
stanford.edu/class/archive/cs/cs106a/cs106a.1142/lectures/
"i've got 2 gigs of RAM in my computer" @1:05 LOL hhhhh that was a thing back in 2008
Doesn't he mean 256 here? 2:04
No, he doesn't. One byte can represent a number from 0 (inclusive) to 255, in other words it can assume one of 256 values.
Oh right, we include 0.
+beingme2345 Actually, the lowest value you can represent with 8 bits is 00000000 and the highest is 11111111 (255 in decimal) so the range of 1 byte is [0 to 255] which is 256 integers.
ALL the confusion about ram resolved here
p1 puplic how it be local?
Sure is
no microphone no candy !!!!
Oh we're the little children, we store integers. rofl this guy rocks!
@00:26 Bless you! (lol).
I need a drink. I don't drink but I think I need a drink. Actually that second example cleared it up for me the first example had my head spinning. The Heap and Stack pictures help a lot it's nice to have a mental picture of what is going on. I've read about pointers before and I thought I understood but those examples never mentioned Heap and Stack so this clears it up quite a bit. Actually no I lied the "this" still confuses me.
nice chalk
The video at the end: ua-cam.com/video/vm5MNP7pn5g/v-deo.html
17:44
Professor Sahami, you kinda lost me the first half but the last half more than made up for it.
@ snaps with a twist ;-)
The video cut off at the end is at /watch?v=6pmWojisM_E
34:47. Boink!
But you need to know it ^^
ua-cam.com/video/vm5MNP7pn5g/v-deo.html Video at the end of the lecture
Yottabyte=one septillion bytes or 1trillion terabytes. mhhh maybe in 2075?
was hoping you'd list the topics like you do in most videos..anyway..your comments are very helpful
garbage collection shirt gnomes are awesome
Thanks I didn't want to go either xD
34:47 laawl!!
2 gigs of ram is now 8 gigs of ram haha
you must unlearn what you have learned...
really fake and gay even fake and gayer then i originally thought, also can i get a place in stanford? :*