Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Practice InterviewNew Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

Core Java Interview Questions and Answers

Ques 286. What is this() keyword in core java and when should we use this() in core java?

Ans.

The this keyword is primarily used in three situations. The first and most common is in setter methods to disambiguate variable references. The second is when there is a need to pass the current class instance as an argument to a method of another object. The third is as a way to call alternate constructors from within a constructor.

Example of Case 1: Using this to disambiguate variable references. In Java setter methods, we commonly pass in an argument with the same name as the private member variable we are attempting to set. We then assign the argument x to this.x.
public class Foo {
    private String name;
    // ...
    public void setName(String name) {
        // This makes it clear that you are assigning 
        // the value of the parameter "name" to the 
        // instance variable "name".
        this.name = name;
    }
    // ...
}
Example of Case 2: Using this we can pass current class object through method argument to another object.
class Foo{
	void callMethod(){
		Toy toy = new Toy();
		toy.insertValue(this); //Here this means Foo current object
	}
}
Example of Case 3: Using this to call alternate constructors. In the comments, trinithis correctly pointed out another common use of this. When you have multiple constructors for a single class, you can use this(arg0, arg1, ...) to call another constructor of your choosing, provided you do so in the first line of your constructor.
class Foo {
    public Foo() {
        this("Some default value for bar");
        // Additional code here will be executed 
        // after the other constructor is done.
    }
    public Foo(String bar) {
        // Do something with bar
    }
    // ...
}

Is it helpful? Add Comment View Comments
Ques 287. What is java.lang package in Core java?
Ans.
For writing any java program, the most commonly required classes & interfaces are defined in a separate package called java.lang package. This package is by default available for every java program, no import statement is required to include this package.
Few Examples:
java.lang.Object
java.lang.Class
java.lang.String
Is it helpful? Add Comment View Comments
Ques 288. What is java.util package in Core Java?
Ans. Java.util package contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes.
E.g.
java.util.List
java.util.Set
java.util.Date

Is it helpful? Add Comment View Comments
Ques 289. What is java.io package in Core Java?
Ans. Java.io package provides classes for system input and output through data streams, serialization and the file system.
E.g
java.io.BufferedReader
java.io.FileReader
Is it helpful? Add Comment View Comments
Ques 290. What is java.math package in Core Java?
Ans. Java.math package provides classes for performing arbitrary-precision integer arithmetic (BigInteger) and arbitrary-precision decimal arithmetic (BigDecimal).
E.g
java.math.BigDecimal
java.math.BigInteger
Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2019 WithoutBook