+ 1

What is this() in java how does it work and how to use it inside a constructor in a print statement

if you have seen the new boston youtube channel 42 video tutotorial you know what i am talking about . in that video he uses printf("%s",this) which apparently points towards toString and constructor how does this work

30th Jun 2018, 6:46 PM
Atharva
Atharva - avatar
4 Respostas
+ 6
Allows you to put string values inside a string without having to use + String name = "Atharva"; System.out.printf("hi %s",name);
30th Jun 2018, 11:35 PM
D_Stark
D_Stark - avatar
+ 6
Atharva its to convert the whole input stream to string which is an alternative way to string concatenation
1st Jul 2018, 9:00 AM
D_Stark
D_Stark - avatar
+ 2
@D_Stark my doubt is if i use this instead of the place where you used name inside a constructor then how does it or why would it call the toString function
1st Jul 2018, 5:20 AM
Atharva
Atharva - avatar
+ 1
this is a keyword that you can use to refer to the object, as simple as that. When you do printf("%s", this); the printf function will run the toString() since this = the object
30th Jun 2018, 9:21 PM
TurtleShell
TurtleShell - avatar