I've advanced through his tutorials with no problems with his manner of naming things, I actually appreciate the sense of humor Bucky has, without it, the tutorials would probably be more tedious. Something I advice doing, is not naming your classes the same as Bucky, that way you avoid copying his code right as it is and actually have to change a thing or two, believe me, it's a much better practice.
"uhh, let's put, the 'structor....make sure you sell it wrong.." haha, your commentary is awesome. These are my new favorite tuts, thanks for sharing your knowledge!
Today I felt stupid because I couldn't understand some of the tutorials. But then I realized that those are 5-7 minutes tutorials. We just can't understand everything in a few minutes guys. We have to practice them or just accept it and move on. Bucky says in some of the tutorials "I hope you understand a little bit of this tutorial". He means that we should understand the basis, not everything. So keep learning and don't give up !!!
When you called it toString, there was a green little triangle that popped up. I clicked on it and some java stuff appeared, so now I know it is important to call the method toString. You are real smart in superb and computer programming. P.S. I think your awesome bucky
bucky, thank you so much! in your first like a 30 tutorials i didn't get many sense of each you did but how longer i follow your tutorials how more i going to understand what you are doing. you may should make my dream come true making money of creating games! bucky, you are a king!!!!!!!
Im so happy the more tutorials the more i understand i didnt even have to watch the whole tutorial cuz i already knew wat i had to do and i have no programming experience
That's the point of it. When you put "this" in the parameters, java doesn't know what variables to use, so the toString command is designed to be what "this" will use when it doesn't have any variables to use.
Apples, tuna, and potpie. No wonder I get so hungry when I am learning programing with Bucky, but it is so delicious. Thanks for tutorials, they are a real help!
@CTOhan You probably already figured this out, but it's because of the way toString() works. If you try to refer to an object as a string java has a built in toString() method that returns a string representation of the object. If you don't make your own toString() method it's just an address that doesn't make sense. Since he referred to the object in the constructor it just looked for the toString() method, which he wrote to return the date.
thank you so much!! you are a much better teacher than my actual teacher. its not really her fault, the administration just threw her in over her head and i have an assignment due and your video showed me how to do it. THANK YOU =) i am now a subscriber to you.
One thing that might help the viewers understand this video is that if you don't define a toString method in your class, Java goes to the toString method of the Object class(parent class).
A pot pie is a term for a type of baked savory pie with a bottom and top completely encased by flaky crusts and baked inside a pie tin to support its shape. The pot pie does differ from the Australian meat pie and many British regional variants on pie recipes, which may have a top of flaky pastry, but whose body is usually made from heavier, more mechanically stable shortcrust, hot water crust or similar pastries
toString() is called anytime you want to get a string representation of an object.. it is built automatically, if you dont build one such method, so he overwrites (overrides) this method to return more appropriate format
use print f where he does as printf stands for print formatted which you can order the information. i'm not very good with it myself but my understanding is that it is used to get it in the format you want, like %s stands for "insert string here" if you will, and then you place the string after the actual message to print and it fills the blank.
%d acts as a place holder for a number. The first %d corresponds to the variable month, the second corresponds to the variable day, and the final %d corresponds to the variable year.
I love these tutorials though it gets boring after a long time, but the funny comments after each vid keeps me motivated along with my hope of being able to make MC mods or even my own mini games/apps! Hopefully I'll learn C/C++ later from ur other tuts and maybe even Objective-C so I can make apps! :D
@syltman %s is the string variable, go to 5:40 and watch from there, he's stepping through the instructions and he tells you what it's doing at the %s part.
I did some testing. If you change the name of the method toStirng, to something else, whatever, you will not have the same result. Instead it will give you the address of the object. So to achieve this effect, it is a must to use toString as method name.
I don't like how "this" is so implicit, if that's the proper word. Why not do something like this: ... for this is %s ", dateFormatter()); } public String dateFormatter(){ return String.format(... That way if there are multiple toString methods, you won't have discrepancy over which to use...
I had fun with this. I enhanced the date so days couldn't go over 31 and under 0, same with months except the limit was 12 of course. And in case of a negative year value, it would display "BC" without the minus sign.. the Math.abs() code was finally useful :D
toString sets the format for the string. "this" called the "formatter", if you will, toString, which then structured the string output as defined in toString. "this" called potpie's toString because "this" only calls methods within it's own class
So what I leaned from this is that the tostring is used when we use 'this' inside the constructor and the purpose of tostring is to substitute the string defined inside it in place of 'this'
4:37 potpie whatEver = new potpie(9,22,2010) Heh, no, but, great tutorials! I'm wanted to work with Java game programming, and these are great "memory refreshers" and "more basics learners". Thanks!
Just think of "this" as being short for "thisClassObject" When you put this in the printf you're telling the function to use thisClassObject's toString method.
Good question - it's because of the to_String that he built. Whenever you tell Java to just print an object (like in System.out), Java will use the to_String function, which he built explicitly. When he wrote the code System.out.println(this), keep in mind that 'this' is an object. So that's what's being printed.
It's printing out the code this means to use the int int the methos specifically cus there could be another int with the same name and the percent sighn means that it's asking for what string to print out every thing else is just regular words in quotes.
RokasDesigns he tried to print an object using "System.out.printf". The object reference is "this" for the created object potpie. Whenever you try to print an object, java will automatically look for the method "toString". In this case, he overrides the method "toString" to let java know that this is what he want to come out when you try to print the object(potpie).
Yeah this can be confusing. But the 'this' keyword always refers to the current object. I don't believe how he used it here is the most common use case which adds to the confusion. When used here in place of a string it seems Java makes an assumption (a big one IMO, considering Java is pretty strict on being explicit anywhere else). Then he is "overriding" the 'toString' method. He doesn't say this but I believe it's important to mention to someone just learning the language. Hope this helps.
How i understood it, the this statement in the print line because it has the %s that means it needs to use a string so it looks through the class for a string and it finds the toString so it uses it.
I don't understand one thing... at 1:25 Why do you use additional variables like month, day, year? Couldn't you use "return String.format("%d/%d/%d", m, d, y)" instead? ....to use the method's arguments directly? :/
I believe the variables int m, d, y only work in the potpie constructor and not anywhere else. However, I could be wrong since I'm a noob at programming.
GotterVibez Because Bucky is using object-oriented principles - as one should in Java - without explaining that he does and what these are. These are the dangers of teaching programming "as-is". The constructor is supposed to set the member variables with the values passed into the parameters. He actually explained this wrong a couple videos ago when he was first introducing "this". Just imagine the print-out would be somewhere outside of the constructor. You would use the member variables. The print-out should not happen inside the constructor. If it does, yes, you can use either the parameters or the actual member variables.
GotterVibez I figured you solved this by now. It's for readers, in general. =) That explanation a couple videos earlier was so wrong, it caused a ripple effect of confusion throughout the following videos.
"Should" and "have to" are two different things. While I and probably most other people name classes with capitalized nouns, I am sure that I've compiled classes with all lower case names to.
What I don't get is why do we always return "strings" and not "ints" while we're dealing with numbers - such as 4,5,6. Is it because we hand write the numbers as arguments? Could someone explain me why?
OMG DUDE, THANKS A LOT, I was really confused on this stupid part, but thanks for clearing it up, only if I discovered this in the begining of the semester!!! :@
so this time the "this" keyword calls the string method. In earlier tutorials, the "this" keyword called a constructor. Are there any other functions of this keyword as well?
When you entered the parameters for the public potpie method, why cant you just insert them str8 away as the previously initialized month, day, year variables?
I think so, it relates to the objects for that class so there is no need for more than one toString method, that one method will be used by all the objects created for that class. I still don't have a clue what it does though...
Want to be more efficient? After you have ur attributes n shit ready go to Right click - > Source - > generate Tostring(); You will find that you can also generate getters and setters for attributes there also and even constructors!
Hey guys, I have a question. In the 'Time Class' tutorial #36, when we created a method the syntax was public void setTime (int h, int m, int s){ } and in this tutorial for an another method the syntax was public potPie (int m, int d, int y){ }. So, my question is why we did not use void in here?
probably waaaaaaaaaaaay too late but in case someone needs answer for this. public potPie (int m, int d, int y){ } is a constructor and constructor does not have return type that is why we don't put void or others in there. on the other hand, public void setTime (int h, int m, int s){ } is a method, and methods have a return type which in this case is void
@SPARTANRusty Yeah if you remember way back in tutorial 2 or 3 we made a project called bucky. Each class since then was right click on or in bucky, then new/ class. Hope that helps. I think they have to be in the same project to interact with each other, so tho we didn't see him make the new one that's where it must logically be.
In your case the printf method will only reply to first parameter which is the month and the value 4. So you will see The constructor for this is 4. When you use ''this'' instead like in the video it overrided the constructor values with the parameters u initialized it with. So for example u make a 2nd String method that formats the values different way, for example: public String toRegular() { return String.format("%02d/%02d/%02d", month, day, year); } then your System.out.printf("The constructor for this is %s ", this); will refer to your toString() method, that is what the "this" basically does here.
yes your method is also correct(like in C).but video just shows another method to do the same so it is like more solutions to same question. Here the main point of video is to explain that when we use "this" in that printf statement it is going to look for toString method (or function in C) by default
no bro you are wrong the printf("%d/%d/%d",month,day,year); works absolutely fine . the point of the video is just to show the use toString. + im a C guy so i know printf works fine
The %s means it NEEDS a string. That string is going to be 'this'. Whenever you use 'this' and require a String, java will automatically look for the reserved 'toString' method in the class and use it. In this case, 'this' refers to the constructor (as it is used inside the constructor), so the constructor object is converted to a string by the toString method. Well, at least I think...I'm only studying Java for 3 days!
Where can i learn important methods and classes of java? I do not want to keep referring a book to find whether already existing methods and classes are already available for that function. I know C++ and know most of the functions by memory.
so, in our "System.out.printf", as we used "this" and the "%s" so we know we were looking for a string, right? That is why we used the "toString" method (wich is a specific method from java). So if instead of a string we were looking for an int "%d" could we use a method called "toInt"?
I do not believe so. You'd need to create the method toInt(), but then you can't ask for the object in integer form by just calling 'this', as toInt is not a special method, whereas toString is built into Java. To call the object as an integer, instead of typing 'this' you would type this.toInt(), which takes the object, and runs the method toInt, which returns an integer, to then be used by the %d parameter. Otherwise 'this' is interpreted as a strong and %d will get lost looking for an integer. Hope that makes sense.
Luís Nascimento He does explain that, but later on in the video he simplifies it so much that it sounds wrong again: The printf is calling the method "toString()", which is inherited from the Object class, on its parameter(s). Since he passes an object reference, the toString would not be a string that "makes sense" to humans. Therefore he is overriding (which the green triangle in Eclipse is telling you) the definition of toString with something reasonable. There is no "toInt()" Object method.
Bonus tip: Instead of private int month; private int day; private int year; - you can just type private int month,day,year; I believe he said this a long time ago.
I followed your instructions and it all works, but I was wondering why do we have to create private ints and then their correspondent ints later which are public? I have played around with the code and if I get rid of 'm,d,y' and make 'month, day, year' public it shows as 0/0/0 so obviously you cant cheat the code, but I dont really understand why.
Hey Bucky, not sure what I am doing wrong here but am getting "The constructor for this is 0/0/0" when I run the program. Normally I get the same output as you, and if I don't I usually find the issue. This one has me stumped though! My classes do say package toString as my first line of code, something that I wasn't getting on windows 10, but recently switched to Linux Mint. Is that beginning line impacting my code, or am I overlooking something obvious? Thanks for being an excellent instructor!
I figured it out! lol. My dum dum self did m=month d=day y=year, instead of month=m ect. Anyway about it though, thank you for uploading these great tutorials!
This guy is always high af lmao
I wish I had teachers like you at my university. You're engaging and humorous!
rofl "6 seems like a good year, long time ago" i laughed so hard i woke up my family
I've advanced through his tutorials with no problems with his manner of naming things, I actually appreciate the sense of humor Bucky has, without it, the tutorials would probably be more tedious.
Something I advice doing, is not naming your classes the same as Bucky, that way you avoid copying his code right as it is and actually have to change a thing or two, believe me, it's a much better practice.
"make sure you spell it wrong" 2:20
lmao
First time?
"uhh, let's put, the 'structor....make sure you sell it wrong.." haha, your commentary is awesome. These are my new favorite tuts, thanks for sharing your knowledge!
Thanks bucky. I like that you dont really spend spend time on theory, rather all application and how this stuff is actually used.
Today I felt stupid because I couldn't understand some of the tutorials. But then I realized that those are 5-7 minutes tutorials. We just can't understand everything in a few minutes guys.
We have to practice them or just accept it and move on. Bucky says in some of the tutorials "I hope you understand a little bit of this tutorial". He means that we should understand the basis, not everything.
So keep learning and don't give up !!!
When you called it toString, there was a green little triangle that popped up. I clicked on it and some java stuff appeared, so now I know it is important to call the method toString. You are real smart in superb and computer programming.
P.S. I think your awesome bucky
I watched all the previous videos at 2x and now I switched to 1x and he now sounds high af. Loved that.
bucky, thank you so much!
in your first like a 30 tutorials i didn't get many sense of each you did but how longer i follow your tutorials how more i going to understand what you are doing.
you may should make my dream come true making money of creating games!
bucky, you are a king!!!!!!!
Im so happy the more tutorials the more i understand i didnt even have to watch the whole tutorial cuz i already knew wat i had to do and i have no programming experience
Shtring
+Daski69 shtfuckyou
+Daski69 Why are you doing this to my brain?
+Daski69 WHAT HAVE YOU DONE TO ME
+alexlp2k10 MY LEARNING EXPERIENCE IS BROKEN SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING SHTRING
市釧路 SHut the fuck up biatch
I really love this guy's JAVA tutorials, keep up the good work!!
That's the point of it. When you put "this" in the parameters, java doesn't know what variables to use, so the toString command is designed to be what "this" will use when it doesn't have any variables to use.
Apples, tuna, and potpie. No wonder I get so hungry when I am learning programing with Bucky, but it is so delicious. Thanks for tutorials, they are a real help!
@CTOhan You probably already figured this out, but it's because of the way toString() works. If you try to refer to an object as a string java has a built in toString() method that returns a string representation of the object. If you don't make your own toString() method it's just an address that doesn't make sense. Since he referred to the object in the constructor it just looked for the toString() method, which he wrote to return the date.
thank you so much!! you are a much better teacher than my actual teacher. its not really her fault, the administration just threw her in over her head and i have an assignment due and your video showed me how to do it. THANK YOU =) i am now a subscriber to you.
One thing that might help the viewers understand this video is that if you don't define a toString method in your class, Java goes to the toString method of the Object class(parent class).
"6 seems like a good year" lolol
@haudace don't get confused lol, "toString" is just a common method used to return the instance variables of an object in string form
A pot pie is a term for a type of baked savory pie with a bottom and top completely encased by flaky crusts and baked inside a pie tin to support its shape.
The pot pie does differ from the Australian meat pie and many British regional variants on pie recipes, which may have a top of flaky pastry, but whose body is usually made from heavier, more mechanically stable shortcrust, hot water crust or similar pastries
toString() is called anytime you want to get a string representation of an object.. it is built automatically, if you dont build one such method, so he overwrites (overrides) this method to return more appropriate format
use print f where he does as printf stands for print formatted which you can order the information. i'm not very good with it myself but my understanding is that it is used to get it in the format you want, like %s stands for "insert string here" if you will, and then you place the string after the actual message to print and it fills the blank.
%d acts as a place holder for a number. The first %d corresponds to the variable month, the second corresponds to the variable day, and the final %d corresponds to the variable year.
Thank you so much Bucky! You have been helping in through my java course all term. Go you! :)
"6 seems like a good year, long time ago" first time I laughed watching these
Wow! Hungry guy!
From the first Java tutorial, you've mentioned:
Apples
Tuna
Bass
Pot pies
Eames Van trinh He made two classes, one tuna, one bass. I forgot which episode it was.
Eames Van trinh Want to follow me? I'll follow you.
Eames Van trinh I followed you. Now follow me.
Eames Van trinh Thanks.
Eames Van trinh Wanna join plus.google.com/u/1/communities/117396812586339466106
Strange that I just ate a potpie before I watched this...
Your are helping me with your amazing videos
"Go ahead and percent... Go ahead and percent? Wtf does that mean?" Hahaha love your work bucky!
Lmao make sure you spell contrtuctor wrong @ 2:21
I love these tutorials though it gets boring after a long time, but the funny comments after each vid keeps me motivated along with my hope of being able to make MC mods or even my own mini games/apps! Hopefully I'll learn C/C++ later from ur other tuts and maybe even Objective-C so I can make apps! :D
@syltman
%s is the string variable, go to 5:40 and watch from there, he's stepping through the instructions and he tells you what it's doing at the %s part.
Java is the beast and the beauty in the same time!
It's been a long time my friend :) im teaching by a 10 years old video and it really helps me a lot.
Love your tutorials! Don't worry about the haters below. I'm not seeing them doing any tutorials. Keep it up & thanks!
I did some testing. If you change the name of the method toStirng, to something else, whatever, you will not have the same result. Instead it will give you the address of the object. So to achieve this effect, it is a must to use toString as method name.
I don't like how "this" is so implicit, if that's the proper word. Why not do something like this:
... for this is %s
", dateFormatter());
}
public String dateFormatter(){
return String.format(...
That way if there are multiple toString methods, you won't have discrepancy over which to use...
You can initialize the variables like this too:
private int month,day,year;
..which is even more comfortable.
I had fun with this. I enhanced the date so days couldn't go over 31 and under 0, same with months except the limit was 12 of course. And in case of a negative year value, it would display "BC" without the minus sign.. the Math.abs() code was finally useful :D
toString sets the format for the string. "this" called the "formatter", if you will, toString, which then structured the string output as defined in toString. "this" called potpie's toString because "this" only calls methods within it's own class
You're way better than my ICS teacher
So what I leaned from this is that the tostring is used when we use 'this' inside the constructor and the purpose of tostring is to substitute the string defined inside it in place of 'this'
If you think I am wrong somewhere then please correct my understanding 😃😃
4:37 potpie whatEver = new potpie(9,22,2010)
Heh, no, but, great tutorials! I'm wanted to work with Java game programming, and these are great "memory refreshers" and "more basics learners".
Thanks!
%s is for string, %d is for digits, and
means to skip to a new line
Just think of "this" as being short for "thisClassObject" When you put this in the printf you're telling the function to use thisClassObject's toString method.
Good question - it's because of the to_String that he built. Whenever you tell Java to just print an object (like in System.out), Java will use the to_String function, which he built explicitly. When he wrote the code System.out.println(this), keep in mind that 'this' is an object. So that's what's being printed.
%s is for string, %d is for numbers (decimal)
It's printing out the code this means to use the int int the methos specifically cus there could be another int with the same name and the percent sighn means that it's asking for what string to print out every thing else is just regular words in quotes.
how does java know 'toString' is the class for 'this'
how does ti print that?
is it random? feels like java is guessing most of my stuffs
did you figure it out? and does 'this' in this case only respond to toString?
RokasDesigns he tried to print an object using "System.out.printf". The object reference is "this" for the created object potpie. Whenever you try to print an object, java will automatically look for the method "toString". In this case, he overrides the method "toString" to let java know that this is what he want to come out when you try to print the object(potpie).
""this()" will invoke current class constructor.
Yeah this can be confusing. But the 'this' keyword always refers to the current object. I don't believe how he used it here is the most common use case which adds to the confusion. When used here in place of a string it seems Java makes an assumption (a big one IMO, considering Java is pretty strict on being explicit anywhere else). Then he is "overriding" the 'toString' method. He doesn't say this but I believe it's important to mention to someone just learning the language. Hope this helps.
How i understood it, the this statement in the print line because it has the %s
that means it needs to use a string so it looks through the class for a string and it finds the toString so it uses it.
Dude.... potpie.... This is unprecedented...
does 'this' would look to a mathod called toInt or toDouble if I use '("%i %f",this, this);' instead ??
7:15
What if Der is more then one string method? Which method it will choose?🤔
AHAHA this was a funny episode! can't wait till the next epic adventure into the depths of toStrings! :)
I don't understand one thing... at 1:25 Why do you use additional variables like month, day, year? Couldn't you use "return String.format("%d/%d/%d", m, d, y)" instead? ....to use the method's arguments directly? :/
I believe the variables int m, d, y only work in the potpie constructor and not anywhere else. However, I could be wrong since I'm a noob at programming.
GotterVibez Because Bucky is using object-oriented principles - as one should in Java - without explaining that he does and what these are. These are the dangers of teaching programming "as-is".
The constructor is supposed to set the member variables with the values passed into the parameters. He actually explained this wrong a couple videos ago when he was first introducing "this".
Just imagine the print-out would be somewhere outside of the constructor. You would use the member variables.
The print-out should not happen inside the constructor. If it does, yes, you can use either the parameters or the actual member variables.
Brax1982 Thanks, :) I was a newbie when I posted the question :) Now things are much clearer, but thank you for replying :D
GotterVibez I figured you solved this by now. It's for readers, in general. =) That explanation a couple videos earlier was so wrong, it caused a ripple effect of confusion throughout the following videos.
HaHa everyones workin there way through all these episode like crazy to get to that gui episode :P
I CANT WAIT
"Should" and "have to" are two different things.
While I and probably most other people name classes with capitalized nouns, I am sure that I've compiled classes with all lower case names to.
What I don't get is why do we always return "strings" and not "ints" while we're dealing with numbers - such as 4,5,6. Is it because we hand write the numbers as arguments? Could someone explain me why?
OMG DUDE, THANKS A LOT, I was really confused on this stupid part, but thanks for clearing it up, only if I discovered this in the begining of the semester!!! :@
40 second tutorial
may I ask what font you used? Because I dont like the font im using right now
so this time the "this" keyword calls the string method. In earlier tutorials, the "this" keyword called a constructor.
Are there any other functions of this keyword as well?
thank you! it is great!! so clear and funny too :) Bucky do you have any tutorial on hashmap or treemap as well??
Can you show us how to apply Scanner (in this or similar problem) in order to input the parameters form the keyboard.
i can used it when i start school\
by November^.^
keep it up
When you entered the parameters for the public potpie method, why cant you just insert them str8 away as the previously initialized month, day, year variables?
I think so, it relates to the objects for that class so there is no need for more than one toString method, that one method will be used by all the objects created for that class. I still don't have a clue what it does though...
Want to be more efficient? After you have ur attributes n shit ready go to Right click - > Source - > generate Tostring(); You will find that you can also generate getters and setters for attributes there also and even constructors!
Lmao, I had to spit out my coffee. This guy was not sober, but it was a good guide even if he was high as fuck!
what a great explanation. thank you a lot
when running the program, whenever it gets to the point in the program where the object is made, the code in the constructor is automatically run
this is the only way of getting the control out of the constructor for a while and after that it gets fully executed!
Hey guys, I have a question. In the 'Time Class' tutorial #36, when we created a method the syntax was
public void setTime (int h, int m, int s){ }
and in this tutorial for an another method the syntax was
public potPie (int m, int d, int y){ }.
So, my question is why we did not use void in here?
probably waaaaaaaaaaaay too late but in case someone needs answer for this.
public potPie (int m, int d, int y){ } is a constructor and constructor does not have return type that is why we don't put void or others in there. on the other hand, public void setTime (int h, int m, int s){ } is a method, and methods have a return type which in this case is void
@SPARTANRusty Yeah if you remember way back in tutorial 2 or 3 we made a project called bucky. Each class since then was right click on or in bucky, then new/ class. Hope that helps. I think they have to be in the same project to interact with each other, so tho we didn't see him make the new one that's where it must logically be.
We can type syso then press CTRL+Space => System.out.println :)
thenewboston somehow when i use printf and format it keeps putting that lightbulb x thing .What should i do?
you rule in java ,you are the best :D
Why do we use toString while we can directly print the output as system.out.printf("the constructor for this is %d/%d/%d", month, day, year)
In your case the printf method will only reply to first parameter which is the month and the value 4. So you will see The constructor for this is 4.
When you use ''this'' instead like in the video it overrided the constructor values with the parameters u initialized it with.
So for example u make a 2nd String method that formats the values different way, for example:
public String toRegular() {
return String.format("%02d/%02d/%02d", month, day, year);
}
then your System.out.printf("The constructor for this is %s
", this); will refer to your toString() method, that is what the "this" basically does here.
yes your method is also correct(like in C).but video just shows another method to do the same so it is like more solutions to same question. Here the main point of video is to explain that when we use "this" in that printf statement it is going to look for toString method (or function in C) by default
no bro you are wrong the printf("%d/%d/%d",month,day,year); works absolutely fine . the point of the video is just to show the use toString. + im a C guy so i know printf works fine
thanks dude
apples, tuna... now potpie, what kind of food obsessions do you have Bucky?
Are there other videos about this. Im having a hard time grasping this with his explanation.
The %s means it NEEDS a string. That string is going to be 'this'. Whenever you use 'this' and require a String, java will automatically look for the reserved 'toString' method in the class and use it. In this case, 'this' refers to the constructor (as it is used inside the constructor), so the constructor object is converted to a string by the toString method. Well, at least I think...I'm only studying Java for 3 days!
Tony Mazon thank u a lot , had a hard time understanding toString
can variables in the constructor be the same name as the ones in the class?
@X3N4T3 Most probably not to confuse newbies.
This was great, exactly what I was looking for. Still lost but less than 10 min ago :)
Hey, isn't there a built-in method of the String class named toString()? Why isn't that covered here???
Where can i learn important methods and classes of java? I do not want to keep referring a book to find whether already existing methods and classes are already available for that function. I know C++ and know most of the functions by memory.
so, in our "System.out.printf", as we used "this" and the "%s" so we know we were looking for a string, right? That is why we used the "toString" method (wich is a specific method from java). So if instead of a string we were looking for an int "%d" could we use a method called "toInt"?
I do not believe so. You'd need to create the method toInt(), but then you can't ask for the object in integer form by just calling 'this', as toInt is not a special method, whereas toString is built into Java.
To call the object as an integer, instead of typing 'this' you would type this.toInt(), which takes the object, and runs the method toInt, which returns an integer, to then be used by the %d parameter. Otherwise 'this' is interpreted as a strong and %d will get lost looking for an integer.
Hope that makes sense.
*****
Thanks!! I got it
Luís Nascimento He does explain that, but later on in the video he simplifies it so much that it sounds wrong again: The printf is calling the method "toString()", which is inherited from the Object class, on its parameter(s). Since he passes an object reference, the toString would not be a string that "makes sense" to humans. Therefore he is overriding (which the green triangle in Eclipse is telling you) the definition of toString with something reasonable. There is no "toInt()" Object method.
@thenewbostan what if there are two methods which return the string
How do you write a to string method to output an array? Like for example given this:
int[]marks 80, 59, 99, 21);
Bonus tip:
Instead of
private int month;
private int day;
private int year;
- you can just type
private int month,day,year;
I believe he said this a long time ago.
can i use use more then one toSting in class ? how would i print for more than one constructor ??
W00T! Can't wait!
whats the point of creating int m,d,y when we can directly assign the value to day month and year?
I have the same exact question
What if you just want to output /n or %s? Its really strange why you include those inside of the parenthesis in System.out.print("")
what if i have multiple string printing in my code
do i need multiple toString methods?
I followed your instructions and it all works, but I was wondering why do we have to create private ints and then their correspondent ints later which are public? I have played around with the code and if I get rid of 'm,d,y' and make 'month, day, year' public it shows as 0/0/0 so obviously you cant cheat the code, but I dont really understand why.
Hey Bucky, not sure what I am doing wrong here but am getting "The constructor for this is 0/0/0" when I run the program. Normally I get the same output as you, and if I don't I usually find the issue. This one has me stumped though! My classes do say package toString as my first line of code, something that I wasn't getting on windows 10, but recently switched to Linux Mint. Is that beginning line impacting my code, or am I overlooking something obvious? Thanks for being an excellent instructor!
I figured it out! lol. My dum dum self did m=month d=day y=year, instead of month=m ect. Anyway about it though, thank you for uploading these great tutorials!
Thx for sharing. I have a question. Why the method toString can be called automatically? Why not need to call it like Object.method() ? Thank you!
I was following you up to the mid 30's I am so lost