Scanner class Methods in Java

Java- Other Operators(String Concatenation(+) Operator,new Operator, Dot(.) Operator, instanceof Operator)

 We have already discussed various operators of Java. Their are some other operators in Java that are  frequently used and play vital role in an object oriented language.Lets learn them quickly!

1.String Concatenation Operator(+)

This operator is used very often. We use it in almost every print statement. The operator which groups two or more string together to make one large string is known as String Concatenation Operator. By concatenation we mean to add or group strings.
Syntax:     "string1" +"string2";

Example: Lets say, we have two different strings "Iron" and "Man". We want to print both strings together on screen.Write the code as :-   "Iron " +"Man"; This line will concatenate two strings but to print them on screen  use the print statement as follows,

System.out.println("Iron " +"Man");//space after Iron print a space between Iron Man output string.
//Output: Iron Man

Note: String concatenation operator (+) is used to print the value of variable. It is an important operator. It helps in printing more than one variable on screen.We can print variable of any data type using String Concatenation Operator.It is different than addition operator , to print addition of two variable use the expression within parentheses like, "Sum is :"+(2+5);

Example:

Example of string concatenation Operator java

Output: 
String Concatenation Operator Java Example

2. new Operator

As we know java is an object oriented language.The new operator is used to create objects.Lets first understand ,what is an object. In general , an object is an entity which can be seen and touched like, laptop , mobile ,chair ,table , book everything near you which is non- living is an object. SO , that's a general definition of object.

In java, everything is an object , the GUI(Graphical User Interface), wizard,buttons where we click are objects. We create classes in java but to access the members of these classes ,we need to create objects which can call the desired member of the a class for us.

The new operator is used to create objects of any class. new means to create a new object which will access the contents of  particular class.We use this operator to call the members of a class from another class. So, it is used for communication between classes.

Syntax: className objectName=new className();

Example: In this example, we will create two classes, one named "Vehicle" and other as  "ClassNewOperator". This Vehicle class will have a String variable "name" which will print the name of vehicle. We will create an object "obj" of vehicle class in "ClassNewOperator" class to access the "name" variable of vehicle class and print it on screen. The vehicle class will not have main() method and other class will have it.
In Java, we can create two classes in one file , but only one class can have main() method. The class which have main() method is the main class.In here, the "ClassNewOperator" is the main class.The output is shown on right side in red box.

3. Dot(.) Operator

The dot(.) operator is an object reference operator. It is a decimal point know as Dot operator in java. It is used to access the members of an class using the object. We can not refer or access a member without using dot operator. In case of static members, we can omit it as static members are accessible from anywhere.

Syntax:    objectName . memberName;

Example: We will use the same example that we used in new Operator. We created an object obj. Now,print string variable using  dot(.) operator.
Here, "obj" is accessing the value of "name" using "." operator which is followed by the string "name" variable.

4. instanceof Operator

The instanceof operator is an object reference operator. It is used to check if the variable is actually the object of a class. It gives output in binary value.It prints true if object belongs to the class and false if not. 

Syntax:    variableName instanceof  className;

Example: We will use the same example that we used in new Operator. We created an object obj, now lets check if the "obj"  is actually an object of class vehicle.
In the example, instanceof checks if the obj is the object of Vehicle and prints true as result.

This was all about the operators in java,These operators are most commonly used and very important to learn.So, please read them carefully.

Experiment: Try to create another class and create more members with different datatypes and access them.

    Keep Learning! Happy Coding!

Comments