Objects
An object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a referenceInterface
An Interface is a contract in the form of collection of method and constant declarations. When a class implements an interface, it promises to implement all of the methods declared in that interface.Instance Members
Each object created will have its own copies of the fields defined in its class called instance variables which represent an object’s state. The methods of an object define its behaviour called instance methods. Instance variables and instance methods, which belong to objects, are collectively called instance members. The dot ‘.’ notation with a object reference is used to access Instance Members.Static Members
Static members are those that belong to a class as a whole and not to a particular instance (object). A static variable is initialized when the class is loaded. Similarly, a class can have static methods. Static variables and static methods are collectively known as static members, and are declared with a keyword static. Static members in the class can be accessed either by using the class name or by using the object reference, but instance members can only be accessed via object references.Below is a program showing the various parts of the basic language syntax that were discussed above.
/** Comment
* Displays "Hello World!"to the standard output.
*/
public class HelloWorld {
String output = "";
static HelloWorld helloObj; //Line 1
public HelloWorld(){
output = "Hello World";
}
public String printMessage(){
return output;
}
public static void main (String args[]) {
helloObj = new HelloWorld(); //Line 2
System.out.println(helloObj.printMessage());
}
}
* Displays "Hello World!"to the standard output.
*/
public class HelloWorld {
String output = "";
static HelloWorld helloObj; //Line 1
public HelloWorld(){
output = "Hello World";
}
public String printMessage(){
return output;
}
public static void main (String args[]) {
helloObj = new HelloWorld(); //Line 2
System.out.println(helloObj.printMessage());
}
}
Class Name: HelloWorld
Object Reference: helloObj (in Line 1)
Object Created: helloObj (In Line 2)
Member Function: printMessage
Field: output (String)
Static Member: helloObj
Instance Member : output (String)
Java Operators
They are used to manipulate primitive data types. Java operators can be classified as unary, binary, or ternary—meaning taking one, two, or three arguments, respectively. A unary operator may appear before (prefix) its argument or after (postfix) its argument. A binary or ternary operator appears between its arguments.Operators in java fall into 8 different categories:
Assignment Operators =
Arithmetic Operators - + * / % ++ --
Relational Operators > < >= <= == !=
Logical Operators && || & | ! ^
Bit wise Operator & | ^ >> >>>
Compound Assignment Operators += -= *= /= %=
<<= >>= >>>= Conditional Operator ?:
Arithmetic Operators - + * / % ++ --
Relational Operators > < >= <= == !=
Logical Operators && || & | ! ^
Bit wise Operator & | ^ >> >>>
Compound Assignment Operators += -= *= /= %=
<<= >>= >>>= Conditional Operator ?:
Java has eight different operator types: assignment, arithmetic, relational, logical, bitwise, compound assignment, conditional, and type.
Assignment operators
The java assignment operator statement has the following syntax:
If the value already exists in the variable it is overwritten by the assignment operator (=).
Arithmetic operators
Java provides eight Arithmetic operators. They are for addition, subtraction, multiplication, division, modulo (or remainder), increment (or add 1), decrement (or subtract 1), and negation. An example program is shown below that demonstrates the different arithmetic operators in java.
The binary operator + is overloaded in the sense that the operation performed is determined by the type of the operands. When one of the operands is a String object, the other operand is implicitly converted to its string representation and string concatenation is performed.
String message = 100 + “Messages”; //”100 Messages”
Relational operators
Relational operators in Java are used to compare 2 or more objects. Java provides six relational operators:
greater than (>), less than (<), greater than or equal (>=), less than or equal (<=), equal (==), and not equal (!=).
All relational operators are binary operators, and their operands are numeric expressions.
Binary numeric promotion is applied to the operands of these operators. The evaluation results in a boolean value. Relational operators have precedence lower than arithmetic operators, but higher than that of the assignment operators.
Logical operators
Logical operators return a true or false value based on the state of the Variables. There are six logical, or boolean, operators. They are AND, conditional AND, OR, conditional OR, exclusive OR, and NOT. Each argument to a logical operator must be a boolean data type, and the result is always a boolean data type.
Given that x and y represent boolean expressions, the boolean logical operators are defined in the Table below.
|
x true true false false |
y true false true false |
!x false false true true |
x &y x &&y true false false false |
x | y x || y true true true false |
x ^ y false true true false |