#someone give me example of how `return this` work behind the scene...
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
When you use return this in a Java method, you are essentially returning a reference to the current object. This can be useful for method chaining, where multiple methods are called in succession on the same object.
Behind the scenes, when return this is used, the current object is simply returned as-is. This means that no new object is created or copied - it is simply a reference to the existing object.
For example:
public class Example {
private int value;
public Example setValue(int value) {
this.value = value;
return this;
}
public void printValue() {
System.out.println("Value: " + value);
}
public static void main(String[] args) {
Example ex = new Example();
ex.setValue(10).printValue();
}
}
In this example, the setValue method returns this, allowing us to call printValue directly on the same object in a single line. This can make code more concise and readable.
Overall, return this simply returns a reference to the current object without creating a new instance.
so let say
class Car {
int speed;
public Car(int speed) {
this.speed = speed;
}
public Car fasterCar(Car otherCar) {
if (this.speed >= otherCar.speed) {
return this;
} else {
return otherCar;
}
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car(55);
Car car2 = new Car(45);
Car fastest = car1.fasterCar(car2);
System.out.println("The speed of the fastest car is " + fastest.speed);
}
}
Detected code, here are some useful tools:
System out
[Nothing]
if fastest returns this it will now points Car?
For any non-static method in class T where the method signature is T m(T other) you can think of it as a static method T m(T this, T other). That is, this is an implicit argument to the method and the value of that argument comes the reference used to call the non-static method. And returning this is therefore no different than returning other, just a return of one of two possible values.
So it's the same as
static highestSpeed(int thisOne, int otherOne) {
if (thisOne >= otherOne) {
return thisOne;
} else {
return otherOne;
}
}
...
int v1 = 55;
int v2 = 45;
int fastest = highestSpeed(v1, v2);
That is car1.fasterCar(car2) can be thought of like a static method call fasterCar(car1, car2) where car1 is 'this'.
so this just simplify the instance?
like instead manually return an class, u use this
wdym "manually returning a class"
like
class Car {
int speed;
public Car(int speed) {
this.speed = speed;
}
public Car fasterCar(Car otherCar) {
if (this.speed >= otherCar.speed) {
return this;
} else {
return otherCar;
}
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car(55);
Car car2 = new Car(45);
Car fastest = car1.fasterCar(car2);
System.out.println("The speed of the fastest car is " + fastest.speed);
}
}
is equivalent of
class Car {
int speed;
public Car(int speed) {
this.speed = speed;
}
public Car fasterCar(Car otherCar) {
if (this.speed >= otherCar.speed) {
return Car;
} else {
return otherCar;
}
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car(55);
Car car2 = new Car(45);
Car fastest = car1.fasterCar(car2);
System.out.println("The speed of the fastest car is " + fastest.speed);
}
}
Detected code, here are some useful tools:
its not
hm?
the second one isnt even valid
you dont return the class, you return an instance of the class
this is being the current instance from the methods context
otherCar is a reference and the method fasterCar declares that it returns a reference of type Car. this is a reference to the current instance (which is a Car)
so the fastest will become
Car fastest = Car();
(sry if i'm getting it wrong, i just don't understand this...)
The test was wrong. You should be taking a course.
This isn't the place to learn java one minor concept at a time. That expression is not valid and your intent could be read at least two ways. so I don't know what you think that expression means.
Car is a class. In Car fastest, fastest is a reference of type Car. Car() indicates a constructor but you can only call those with the new operator.
I highly recommend you look at a course like the free Helsinki Java MOOC. If you do, please do come back here for any clarifications and support, but the course will do a better job of introducing the concepts in order, in a way you can apply them.
MOOC is a completely free introductory Java course created by the University of Helsinki, it is a great way to learn Java from the ground up.
- The MOOC teaches a broad introduction to programming in Java in two parts - one at beginner, and another at intermediate level.
The end of the course is marked by creating your own Asteroids game clone! - The MOOC allows using features up to Java 11 - you can install Temurin OpenJDK 11 from the Adoptium project.
- To submit exercises for evaluation, you need to configure an Editor/IDE (Integrated Development Environment) with the TMC Plugin.
The course instructions will suggest to use TMCBeans/NetBeans or VS Code for the course, but you can also use IntelliJ.
- TMCBeans/NetBeans is the easiest to configure - but has the most dated user experience
- VS Code is very popular as an editor, but it is quite new for Java Development. Some extra configuration is needed.
- IntelliJ arguably has the best user experience and is most widely used Java IDE by professionals.
IntelliJ requires installing a version no newer than2023.1- because the IntelliJ TMC Plugin doesn't work with newer installs.
The IntelliJ Community version is free and all you need to install the TMC plugin.
This server currently recommends IntelliJ.
To use IntelliJ with the MOOC, simply install the TMC plugin by opening IntelliJ -> File -> Settings -> Plugins and searching for TMC. You will then be able to use IntelliJ to complete MOOC.
Visit MOOC here: https://java-programming.mooc.fi/
(the course is available in both English and Finnish)
About the course - Java Programming
i'm taking
but didn't understand shit
A course specifically on Java? I'm a little perplexed by this statement
so i already skipped 4 java course when i was doing pretest
It sounds like that 'pretest' was a poor measure of whether or not you knew Java.
Did the course that you did take, cover the basics? Or did it start off assuming you already knew them?
i went back
to the start
"Or did it start off assuming you already knew them?"
You could at least skim read the material of the Java MOOC and see at which point you've missed something - hopefully they'll fill in the blanks
So let say.
car1.fastestCar(car2);
is equivalent of
Car::fastestCar(&car1, car2);
Rust associated functions can be called in either form (though the function may borrow or take ownership).
thats not rust
that equivalent in C++
So many languages, many overlapping syntaxes.
so basically
it goes ```java
public Car fasterCar(Car otherCar) {
but Java write it as
```java
public static Car fasterCar(Car const this, Car otherCar) {
passing the member object for the class
Technically not, because method resolution works differently for instance methods.
But effectively that's what's how it functions, from the perspective of the method author. this acts like an implicit final parameter in an instance method.
o
The declaration type of this is the class in which the method appears and the value of this is the value of the reference used to invoke the method.