November 26 2014

Protected Access Specifier In Java

Access Specifiers tell how to access the members of the class. There are four access specifiers for class members.

  1. private
  2. default
  3. protected
  4. public

Protected Access Specifier In Java

Protected members of a class can be accessible across the package and to the sub classes outside the package. It is called default + children. 

Example : In the below class, variable name and getMessage method are specified as protected. All the classes in package1 can access the class members. Continue reading

November 8 2014

Java Boolean Operators

Java Boolean Operators

Boolean Operators are operators which act on boolean variables and returns the boolean result.Following are the boolean operators in java

Boolean and operator ( & )

Boolean & operator returns true only if both the variables are true otherwise returns false

Boolean or operator ( | )

Boolean | operator return true if any of the variable is true

Boolean not operator ( ! )

Boolean ! Return true if the value of the variable is false and false if value of the variable is true.

Continue reading

November 6 2014

Use of final keyword in java

Use of final keyword in java

final keyword  can be used in three ways

1.To create a constant

If a variable is declared as final ,value of the variable cannot be changed. Value must be assigned to the final variable during the declaration.


package com.answersz.corejava;

public class FinalVariableExample {

final double PI=3.1415;

public void changeFinalVariable(){
PI=2.14; // You will get the following compilation error
// The final field FinalVariableExample.PI cannot be assigned
}
}

Continue reading

November 3 2014

Unary Operator Java

Unary Operator Java (-)

Unary Operator is used to negate  a value. Negation is converting negative number to positive number and positive number to negative number.

 Syntax

-variable or – value

Program 

In the following example value of a is 10 and -a is assigned to b. Value of b becomes -10. Value of c is -20 and -c is assigned to d so value of d become +20; Continue reading

November 3 2014

Java Ternary Operator

Java Ternary Operator  /  Java Conditional Operator

The ternary operator is also called conditional operator. It is called ternary as it deals with three variables.

Syntax

variable = (expression1)? expression2 : expression3 ;

expression1 is evaluated first . If expression1 value is true expression2 value will be assigned to variable .If expression1 value is false expression3 value will be assigned to variable.

In the following example (age>=18) is the condition if the expression is true value “Allowed”is assigned to voting else “Not Allowed” will be assigned to voting Continue reading