Q1. What does % mean in java?
Asnwer: The % operator is one of the Arithmetic operator in java. Its called as Modulo or Remainder operator.. This operator divides one operand by another and returns remainder of that.
A operator which returns remainder of division is called reminder operator. %
is the keyword for this operator in Java.
Example:
public Class JavaReminderOperator {
public static void main(String s[]) {
int a = 5;
int b = 2;
System.out.println("a%b=" + a%b);
}
}
Output of the above example is 1.
The remainder, what is left from b dividing the highest possible whole number less than or equal to a resulting in a whole number.
In our example: 5%2, highest number resulting in an integer is 4, therefore ,
5%2 = 5 - (2*2) = 5 - 4 = 1
Q2. What does += mean in java?
**Asnwer: ** The += operator is called Addition Assignment operator. This adds value of the right operand to a value of variable in the left and assigns the result to the left variable. This operator adds and then assign the value to variable so its called as Addition Assignment operator.
This is short form of x = x + y. we can write this statement as x += y. This is used to simplify the statement.
Example:
public class JavaOPsTest {
public static void main(String s[]) {
int a = 2;
int b = 1;
a += b;
System.out.println("a+=b = " + a);
}
}
Above example output will be 3 here. Internally it will add right side value b=1 to the left variable value a=2 and add 2+1 and then assign to left variable a=3.
Interesting point to note here, we cant use different data type (int and float) in x = x + y. If we attempt, Java will throw compile time exception. Please see the below code to understand this concept –
public class JavaOPsTest {
public static void main(String s[]) {
int a = 2;
float b = 1.1f;
a = a + b; // throws compile time error
System.out.println(a);
}
}
Same code will work if we use short form a += b. it will implicitly cast to left side operand type and give results.
public class JavaOPsTest {
public static void main(String s[]) {
int a = 2;
float b = 1.1f;
a += b;
System.out.println(a);
}
}
This will cast b to 1 and then add to a.
String Concatenation
The += operator also works for string concatenation.
String a = "Java ";
a+="Example";
System.out.println(a);
This will output Java Example
Q3. Explain super keyword in Java. What does it do?
The super keyword used to refer to parent class.
Using super
, we can reference the attributes, methods and even constructors of the parent class.
Another key concept to know is that using super
, we can differentiate between the attributes, methods and constructors of the subclass and parent class in case of name collisions.
Example:
class Parent {
int data = 2;
public Parent() {
System.out.println("Called Parent Constructor")
}
void display() {
System.out.println("Display Parent data in Parent Method= " + data)
}
}
class Child extens Parent {
int data = 10;
void display() {
super(); // calling parent constructor using super()
System.out.println("Display Parent data = " + super.data); // calling parent variable using super keyword
super.display(); // calling parent method using super keyword
}
}
Output –
Called Parent Constructor
Display Parent data = 2
Display Parent data = 2
As you can see, in the example above, we are able to reference the attributes in the parent class using super
Q4. What does void
mean in Java?
The void
keyword exists in many programming languages. Specifically in Java, void
is used as part of method signature to indicate that the method does not return any value.
If a method is declared with void return type, it indicates to JVM that this method is not going to return any value after completing its execution.
Example:
public class JavaOPsTest {
public static void main(String s[]) {
new JavaOPsTest().getData();
System.out.println("After void method called");
}
public void getData() {
int a = 2;
int b = 15;
System.out.println("Void does not return any value. Sum = " + (a+b));
}
}
In the above example, getData
method return type is defined as void, hence it will process task and then wont return any value back to calling point new JavaOPsTest().getData();
.
Output –
Void does not return any value. Sum = 17
After void method called
Q5. What does this
mean in java?
The this keyword is used to refer members of the current object in scope. Members include fields and methods.
This
keyword is also used to differentiate locally scoped variables and instance variables in case of name collisions.
Locally scoped variables have higher precedence and by just using the variable name, we can reference the locally scoped variables. In such situations, if we need to access instance variable with the same name, we can do so using this.instanceVariableName
.
Example –
public class JavaOPsTest {
int memberData = 5;
public static void main(String s[]) {
new JavaOPsTest().getData(10);
}
public void getData(int memberData) {
System.out.println("Instance variable value =" + this.memberData);
}
}
Output –
Instance variable value =5
As you can see, in the above example, we are using this
to reference the instance level memberData
as opposed to the locally scoped memberData
that is passed as a parameter to the method.
Here is another example where this
is used to access overloaded constructor from a no-arg constructor –
public class JavaOPsTest {
public JavaOPsTest() {
this("Calling one argument contructor using this");
}
public JavaOPsTest (String data) {
System.out.println(data);
}
public static void main(String s[]) {
new JavaOPsTest();
}
}
Q6 What does null
mean in java?
null is not a keyword but a literal.
null
is a literal just like Boolean values true
and false
. null
can be assigned to any variable.
If a variable has null value, it essentially means that the variable is not referencing any object.
If we try to reference any property or methods of such a null
variable, JVM throws an unchecked java.lang.NullPointerException
Example –
String s = null;
System.out.println(s.length()); /* throws NullPointerException since there's really no String to invoke the length() on*/
Two other important points about null
in Java –
null
is Case sensitive. (All iterals in Java are case sensitive for that matter) i.e. We can’t writeNULL
instead ofnull
String s = NULL; // Incorrect. Won't compile
String s = null; // Correct
- Unless explicitly initialized, all object variables have the default value of
null
Example –
public class JavaOPsTest {
private static Object object;
public static void main(String s[]) {
System.out.println("object value by default = " + object);
}
}
Output of the above example will be object value by default = null
Q7. What does *= mean in java?
The *=
operator is called the Multiplication Assignment operator. This multiplies the value of the right operand to a value of variable in the left and assigns the result to the left variable. This operator multiplies and then assign the value to variable so it’s called as Multiplication Assignment operator.
*=
operator is a short form. using this operator, we can write x = x * y
as x *= y
.
Example:
public class JavaOPsTest {
public static void main(String s[]) {
int a = 2;
int b = 5;
a *= b;
System.out.println(a); // Prints 10
}
}
Interestingly, *=
supports implicit type casting. Therefore, given x (int) and y (float), x = x*y
fails to compile due to differing data types of x
and y
but x *= y
works. It implicitly casts the left hand operand before performing the multiplication.
public class JavaOPsTest {
public static void main(String s[]) {
int a = 2;
float b = 1.1f;
a = a * b; // fails to compile due to differing data types of x and y
a *= b; // compiles and computes as expected
System.out.println(a);
}
}
Q8. What does == mean in java?
Operator == is the Equality and Relational Operators. It is used to determine if one operand is equal to another or not. It returns Boolean value after the comparison. This operator mostly using in looping and if statements.
It is important to understand the nuanced behavior of ==
when comparing primitives versus objects.
If the operands are primitives, ==
compares the values. On the other hand, if the operands are objects, ==
compares the references to check whether they point to the same memory location. If they do, it returns true
or false
otherwise.
== Syntax:
LHS value == RHS value
Example1: Comparing primitives using ==
public class JavaOperatorExam {
public static void main(String s[]) {
int a = 2;
int b = 2;
int c = 3;
System.out.println("a==b is " + (a==b)); // prints "a==b is true"
System.out.println("a==c is " + (a==c)); // prints "a==c is false"
}
}
Example2: Comparing objects using ==
public class JavaOperatorExam {
public static void main(String s[]) {
String a = "First";
String b = a;
String c = new String ("First");
System.out.println("a==b is " + (a==b)); // prints "a==b is true"
System.out.println("a==c is " + (a==c)); // prints "a==c is false"
}
}
Q9. What does ?: mean in Java?
Operator ? is a conditional operator which when combined with :
can be used as if-else statement.
?:
is also called as ternary operator since it operates on three operands.
?:
ternary operator syntax is as follows: operand1 ? operand2 : operand3
If operand1
one is true
, then this operation evaluates to operand2
. Otherwise, this operation evaluates to operand3
.
operand1
must be or must evaluate to a boolean value.
Example:
public class JavaOperatorExam {
public static void main(String s[]) {
int a = 2;
int b = 4;
int c = 0;
if(a==b) {
c = a;
}else {
c = b;
}
}
}
Above example can be simplified as below using ?:
as follows:
public class JavaOperatorExam {
public static void main(String s[]) {
int a = 2;
int b = 2;
int c = (a==b)?4:3;
System.out.println(c); //prints 4
}
}
Q10. What does return
do in Java?
The return keyword in Java is used to indicate to JVM that it must exit from the current scope (typically a method) and return back to the calling point.
In case of methods with non-void return type, return
must be followed by the value that the method wants to return.
Any statements in the finally block are the only statements executed before the control is passed back to the calling point.
Example1: Use of return
in a method returning a value
public class JavaOperatorExam {
public static void main(String s[]) {
int c = new JavaOperatorExam().process();
System.out.println("Method with return = " + c);
}
public int process() {
int a = 2;
int b = 4;
int c = (a==b)?a:b;
return c;
}
}
Example2: Use of return
in a method that does not return a value
public class JavaOperatorExam {
public static void main(String s[]) {
new JavaOperatorExam().process();
}
public void process() {
int a = 2;
int b = 4;
int c = (a==b)?a:b;
if (c==4) { // control exits if this condition is true
System.out.println("Calling return statement to exit from here");
return; //don't continue processing this method anymore...return control to calling point
}else {
System.out.println("Else wont execute");
}
System.out.println("This line wont execute");
}
}
Example3: Behavior of return
in presence of finally
block
public class JavaOperatorExam {
public static void main(String s[]) {
new JavaOperatorExam().process();
}
public void process() {
try {
int a = 2;
int b = 4;
int c = (a == b) ? a : b;
if (c == 4) { // control exits if this condition is true
System.out.println("Calling return statement to exit from here");
return; //don't continue processing this method anymore...return control to calling point
} else {
System.out.println("Else wont execute");
}
System.out.println("This line wont execute");
}
finally{
System.out.println("This line will always execute, no matter what");
}
}
}