How to add two numbers in Java
Posted On
Posted By admin
This blog posts shows how to add two numbers in Java:
public class Demo { public static void main(String args[]){ int a = 10; int b = 20; int result = add(a,b); System.out.println("Result is "+result); } private static int add(int a,int b){ int result = a+b; return result; } }
Here, we are using the method add to add two numbers. It accepts 2 integer values, adds them and returns the result. You can change the data type/ return value to anything else like long,short, float, double, etc.
If you run the code, the following output will get printed to the console:
Result is 30
If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my Java course here
Also, if you'd like to test your Java knowledge, do check out my practice tests course here
Also, if you'd like to test your Java knowledge, do check out my practice tests course here
If you like this post, please do let me know via the comments box below. You can also connect with me via my Facebook Page or subscribe to my Youtube channel!