What is the value of this? somewhere I read that in C# (this==null) is possible. But what about in Java? That, will the following fragment ever return true
?
if(this!=null)
{
return false;
}
else
{
return true;
}
What is the value of this? somewhere I read that in C# (this==null) is possible. But what about in Java? That, will the following fragment ever return true
?
if(this!=null)
{
return false;
}
else
{
return true;
}
if(this!=null)
The above always evaluates to true
, meaning that the first branch of your if
will always get executed, and the function always returns false
.
"this" can never be null in Java
.....?
if(this!=null)
{
return false;
}
Within an instance method or a constructor, this is a reference to the current object .So it never be null
.
"this" keyword refers to the "that" object, which you are referring to..
class Sample
{
int age;
Sample(int age)
{
this.age = age; // this.age -> the variable a in the that current instance
}
public void display()
{
System.out.println(age); //age here is actually this.age
}
}
public class XYZ
{
public static void main(String[] args)
{
Sample a,b;
a.display();
b.display();
}
}
Just think logically - that's like saying If I don't exist...
.
The thing that currently has control of the code has to exist, otherwise the code wouldn't be running in the first place.