Java中的Integer sum()方法

爪哇。整型。sum()是java中的一个内置方法,它返回其参数之和。该方法根据+运算符将两个整数相加。

null

语法:

public static int sum(int a, int b)

参数: 该方法接受两个相互添加的参数: A. :第一个整数值。 B :第二个整数值。

返回值: 该方法返回其参数之和。 例外情况: 该方法抛出了一个 算术异常 当结果溢出整数时。

例子

Input: a = 170, b = 455
Output: 625

Input: a = 45, b = 45
Output: 90

下面的程序演示了Java。整型。sum()方法: 项目1: 对于一个正数。

// Java program to illustrate the
// Java.lang.Integer.sum() method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
int a = 62 ;
int b = 18 ;
// It will return the sum of two arguments.
System.out.println( "The sum is =" + Integer.sum(a, b));
}
}


输出:

The sum is =80

项目2: 下面的程序说明了例外情况。

// Java program to illustrate the
// Java.lang.Integer.sum() method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
// When very large integer is taken
int a = 92374612162 ;
int b = 181 ;
// It will return the sum of two arguments.
System.out.println( "The sum is =" + Integer.sum(a, b));
}
}


输出:

prog.java:8: error: integer number too large: 92374612162
       int a = 92374612162;
               ^
1 error

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享