What Is the Meaning of "This" in Java

Meaning of :: in Java syntax

This is method reference. Added in Java 8.

TreeSet::new refers to the default constructor of TreeSet.

In general A::B refers to method B in class A.

What is the meaning of & in java

It the bitwise "and" operator. Link: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Edit more specific explanation: It is used to combine two numbers in which a position in the binary representation is 1, if it is 1 in both numbers so:

0101 & 0000 = 0000
0101 & 1111 = 0101
0101 & 0100 = 0100
...and so on...

Meaning of .this and .class in java

Home.this

Home.this refers to the current instance of the Home class.

The formal term for this expression appears to be the Qualified this, as referenced in Section 15.8.4 of the Java Language Specification.

In a simple class, saying Home.this and this will be equivalent. This expression is only used in cases where there is an inner class, and one needs to refer to the enclosing class.

For example:

class Hello {
class World {
public void doSomething() {
Hello.this.doAnotherThing();
// Here, "this" alone would refer to the instance of
// the World class, so one needs to specify that the
// instance of the Hello class is what is being
// referred to.
}
}

public void doAnotherThing() {
}
}

Home.class

Home.class will return the representation of the Home class as a Class object.

The formal term for this expression is the class literal, as referenced in Section 15.8.2 of the Java Language Specification.

In most cases, this expression is used when one is using reflection, and needs a way to refer to the class itself rather than an instance of the class.

What does the '{' symbol (curly-brace) indicate in Java?

The question from your instructor seems not so great because there isn't a single unified meaning of the { symbol in Java.

In the context of a statement, the { symbol is used to denote the start of a block statement. This accounts for all the uses of { with if statements, while loops, for loops, do ... while loops, switch statements, etc., which technically only apply to a single statement but are often used with block statements:

if (x == 0) {
statementOne();
statementTwo();
}

In the context of a method or type (class/interface/enum/annotation), the { symbol is used to denote the beginning of the body of a class or a method:

public class NewClass {
...

public void foo() {
...
}
}

It can also be used inside a class to declare an initializer or static initializer block:

class MyClass() {
static int x;
static {
x = somethingHorrible();
}
};

In the context of an array literal, the { symbol is used to denote the beginning of the list of elements used inside that literal:

int[] arr = new int[] {1, 3, 7};

Each of these uses of the open brace symbol is different from all the others. In fact, the language would work just fine if we used a different symbol for each of these different contexts.

I think that the best answer to your question is that { is used in contexts where some group of things will be treated as a unit, whether it's a block statement (many statements treated as a single one), a class (many methods and fields treated as a single object), a method (many statements treated as a single unified piece of code), an initializer (many things that need to be done at once), etc.

In the majority of these contexts, as the comments have pointed out, the brace introduces a new scope. Statement blocks, class bodies, initializer bodies, and function bodies all introduce a new scope, and that is definitely something important to keep in mind. (Array initialization doesn't do this, though.)

What does the arrow operator, '-', do in Java?

That's part of the syntax of the new lambda expressions, to be introduced in Java 8. There are a couple of online tutorials to get the hang of it, here's a link to one. Basically, the -> separates the parameters (left-side) from the implementation (right side).

The general syntax for using lambda expressions is

(Parameters) -> { Body } where the -> separates parameters and lambda expression body.

The parameters are enclosed in parentheses which is the same way as for methods and the lambda expression body is a block of code enclosed in braces.



Related Topics



Leave a reply



Submit