Dot method in java

Maratha Programmer

A blog about Java programming. Also we try to cover Java, Android,C, C++, php, MySQL, SQL Server, JSP, Servlet, etc. We try to improve logic & programming skill.

Saturday, 15 March 2014

3 dot (…) method or Varargs in Java

To see explanation please click here .

These functionality enables to write methods/functions which takes variable length of arguments. For example the popular print() method in Java. We can call print() method with multiple arguments. Some time we have a scenario that one method can take variable number of argument and now with varargs from language makes it much easier. In this Java tutorial we will see How variable arguments makes it easy to write convenient method which can accept any number of arguments, perfect candidates are sum() and average() kind of method

Syntax:-

return_type MyMethod(type. Variable_name)

Notice the dots . in above code. That marks the last argument of the method as variable argument. Also the vararg must be the last argument in the method.

It means that zero or more String objects (or an array of them) may be passed as the parameter(s) for that function.

Читайте также:  Html to xml golang

Important Note 1:

The parameter(s) passed in this way is always an array — even if there’s just one. Make sure you treat it that way in the method body.

Important Note 2:

The parameter that gets the . must be the last in the method signature. So, myMethod(int i, String. strings) is okay, but myMethod(String. strings, int i) is not okay.

Program:-

In above code, the add() method is taking variable arguments and is being called from main method with number of arguments. In its every call we use different arguments. This can be done with the help of method overloading up to some extent. But 3 dot (…) method is best solution for it. By using this type of method you can implement addition, subtraction, multiplication, division,etc .

To see the explanation of above program please see video . It helps you to understand the concept easily.

Источник

Java Dot Operator

In this tutorial, we will learn about Java methods, how to define methods, and how to use Methods In Java programs with the help of examples. It denotes the separation of class from a package, separation of method from the class, and separation of a variable from a reference variable.

Java Dot Operator

In Java, operators are special symbols (such as +, -, *, /, &, ||, &&, ^, !, =, etc.) that perform specific operations on one, two, or more than two operands, and then return a result. Except for these operators, Java provides some other operators such as instanceOf operator, dot operator , scope resolution operator, etc. In this section, we will discuss only the dot operator in Java, its uses with example.

It is just a syntactic element. It denotes the separation of class from a package, separation of method from the class, and separation of a variable from a reference variable. It is also known as separator or period or member operator.

  • It is used to separate a variable and method from a reference variable.
  • It is also used to access classes and sub-packages from a package.
  • It is also used to access the member of a package or a class.

In simple words, we can say that the dot operator is actually access provider of objects and classes. For example:

objectReference.methodName(argumentList); objReference.count; //access instance variable count
Example:

DotOperatorExample1.java

public class DotOperatorExample1 < void display() < double d = 67.54; //casting double type to integer int i = (int)d; System.out.println(i); >public static void main(String args[]) < DotOperatorExample1 doe = new DotOperatorExample1(); //method calling doe.display(); >>

DotOperatorExample2.java

public class DotOperatorExample2 < public static void main(String args[]) < int a = 6754, b = 8765; DotOperatorExample2 doe = new DotOperatorExample2(); //function calling int c = doe.findMinimum(a, b); System.out.println("Minimum Value = " + c); >//returns the minimum of two numbers public static int findMinimum(int x, int y) < int minimum; if (x >y) minimum = y; else minimum = x; return minimum; > >

Java Dot Operator, Java Dot Operator with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.

Java Methods

In this tutorial, we will learn about Java methods, how to define methods, and how to use Methods In Java programs with the help of examples.

Java Methods

A method is a block of code that performs a specific task.

Suppose you need to create a program to create a circle and color it. You can create two methods to solve this problem:

Dividing a complex problem into smaller chunks makes your program easy to understand and reusable.

In Java, there are two types of methods:

  • User-defined Methods : We can create our own method based on our requirements.
  • Standard Library Methods : These are built-in methods in Java that are available to use.

Let’s first learn about user-defined methods.

Declaring a Java Method

The syntax to declare a method is:

    returnType — It specifies what type of value a Method Returns For example if a method has an int Return Type then it returns an integer value.

In the above example, the name of the method is adddNumbers() . And, the return type is int . We will learn more about return types later in this tutorial.

This is the simple syntax of declaring a method. However, the complete syntax of declaring a method is

modifier static returnType nameOfMethod (parameter1, parameter2, . ) < // method body >
  • modifier — It defines access types whether the method is public, private, and so on. To learn more, visit Java Access Specifier.
  • static — If we use the static keyword, it can be accessed without creating objects.

  • parameter1/parameter2 — These are values passed to a method. We can pass any number of arguments to a method.

Calling a Method in Java

In the above example, we have declared a method named addNumbers() . Now, to use the method, we need to call it.

Here’s is how we can call the addNumbers() method.

// calls the method addNumbers();

Call a method in Java using the name the method followed by a parenthesis

Example 1: Java Methods

class Main < // create a method public int addNumbers(int a, int b) < int sum = a + b; // return value return sum; >public static void main(String[] args) < int num1 = 25; int num2 = 15; // create an object of Main Main obj = new Main(); // calling method int result = obj.addNumbers(num1, num2); System.out.println("Sum is: " + result); >>

In the above example, we have created a method named addNumbers() . The method takes two parameters a and b . Notice the line,

int result = obj.addNumbers(num1, num2);

Here, we have called the method by passing two arguments num1 and num2 . Since the method is returning some value, we have stored the value in the result variable.

Note : The method is not static. Hence, we are calling the method using the object of the class.

Java Method Return Type

A Java Method may or may not return a value to the function call. We use the return statement to return any value. For example,

Here, we are returning the variable sum . Since the return type of the function is int . The sum variable should be of int type. Otherwise, it will generate an error.

Example 2: Method Return Type
class Main < // create a method public static int square(int num) < // return statement return num * num; >public static void main(String[] args) < int result; // call the method // store returned value to result result = square(10); System.out.println("Squared value of 10 is: " + result); >>
Squared value of 10 is: 100

In the above program, we have created a method named square() . The method takes a number as its parameter and returns the square of the number.

Here, we have mentioned the return type of the method as int . Hence, the method should always return an integer value.

Java method returns a value to the method call

Note : If the method does not return any value, we use the void keyword as the return type of the method. For example,

Method Parameters in Java

A method parameter is a value accepted by the method. As mentioned earlier, a method can also have any number of parameters. For example,

// method with two parameters int addNumbers(int a, int b) < // code >// method with no parameter int addNumbers()< // code >

If a method is created with parameters, we need to pass the corresponding values while calling the method. For example,

// calling the method with two parameters addNumbers(25, 15); // calling the method with no parameters addNumbers()
Example 3: Method Parameters
class Main < // method with no parameter public void display1() < System.out.println("Method without parameter"); >// method with single parameter public void display2(int a) < System.out.println("Method with a single parameter: " + a); >public static void main(String[] args) < // create an object of Main Main obj = new Main(); // calling method with no parameter obj.display1(); // calling method with the single parameter obj.display2(24); >>
Method without parameter Method with a single parameter: 24

Here, the parameter of the method is int . Hence, if we pass any other data type instead of int , the compiler will throw an error. It is because Java is a strongly typed language.

Note : The argument 24 passed to the display2() method during the method call is called the actual argument.

The parameter num accepted by the method definition is known as a formal argument. We need to specify the type of formal arguments. And, the type of actual arguments and formal arguments should always match.

Standard Library Methods

The standard Library Methods are built-in methods in Java that are readily available for use. These standard libraries come along with the Java Class Library (JCL) in a Java archive (*.jar) file with JVM and JRE.

  • print() is a method of java.io.PrintSteam . The print(«. «) method prints the string inside quotation marks.
  • sqrt() is a method of Math class. It returns the square root of a number.
Example 4: Java Standard Library Method

To learn more about standard library methods, visit Java Library Methods.

What are the advantages of using methods?

1. The main advantage is code reusability . We can write a method once, and use it multiple times. We do not have to rewrite the entire code each time. Think of it as, «write once, reuse multiple times».

Example 5: Java Method for Code Reusability
public class Main < // method defined private static int getSquare(int x)< return x * x; >public static void main(String[] args) < for (int i = 1; i > >
Square of 1 is: 1 Square of 2 is: 4 Square of 3 is: 9 Square of 4 is: 16 Square of 5 is: 25

In the above program, we have created the method named getSquare() to calculate the square of a number. Here, the method is used to calculate the square of numbers less than 6 .

Hence, the same method is used again and again.

2. Methods make code more readable and easier to debug. Here, the getSquare() method keeps the code to compute the square in a block. Hence, makes it more readable.

The Dot (.) Operator in Java, Java. Java Operator. Created: November-22, 2021. In Java language, the dot operator (.) symbolizes the element or operator that works over the syntax. It is often known as a separator, dot, and period. Simply the dot operator acts as an access provider for objects and classes. The usage of the above operator is as …

The Dot (.) Operator in Java

In Java language, the dot operator ( . ) symbolizes the element or operator that works over the syntax. It is often known as a separator, dot, and period. Simply the dot operator acts as an access provider for objects and classes. The usage of the above operator is as below.

  1. It separates a function and variable from an instance variable.
  2. It allows to access sub-packages and classes from a package.
  3. It leads to access the member of a class or a package.
public class DotOperator < void show() < int i = 67; System.out.println("In show method: "+ i); >static boolean isGreater(int a, int b) < return a >b; > public static void main(String args[]) < DotOperator doe = new DotOperator(); doe.show(); System.out.println("Is 5>4: " + DotOperator.isGreater(5, 4)); > > 

In the above code block, the use of the instance method and static method gets showcased. The code block has a public DotOperator class that has two member methods. The internal working of the member method show is to display a local instance variable using print stream.

The class holds another static isGreater() method that takes two parameters. The result of the operation is a boolean value if the two inputs are greater or lesser than each other.

Lastly, the class holds the main method, which tracks the actual logic to perform usage of dot operator. In the main function, an instance of parent class that is DotOperator gets created.

The instance variable now used to access the class’s member function show. The method calls the show function and displays the value that gets initialized and instantiated in the class.

Similarly, the class name DotOperator gets directly allowed to access the static method of the DotOperator class. The function also returns true or false based on the first value being more than the second one.

The boolean output gets returned and printed in the main method of the class.

Below is the output of the above code block.

In show method: 67 Is 5>4: true 

Источник

Оцените статью