java programming Operators tutorial

In the previous lecture, we'll learn the java program structures and data types. in this lesson, you'll learn how to perform calculations on data with the use of operators. Check out the program below which use the "+" operator to calculate 40+60.

public class Main{
public static void main(String[] args){
System.out.println(40+60) // using + or addition operator
System.out.println(150+300)// second addition statement
System.out.println(150-300)// Subtraction operator
System.out.println(150*300)// Multiplication operator
System.out.println(150/300)// division operator
System.out.println(150%300)// Remainder operator
System.out.println(235.40/50.23) // floating point numbers
System.out.println("result:" +23);// concatination added
System.out.println("Pi:"+3.1415); // concatination added
}
}

The program sum-up the two integers and return the result of 40+60 = 100
Now make the program calculate and print 150+ 300 just for your activity enhancement, otherwise, you can look the above as an example. Now let's learn the other arithmetic operators: The addition(+), Subtraction(-), the multiplication(*), the division(/), and the remainder(%) operator. if you try to run your compiler the print result is rounded because the operands are integers, the result is also an integer. to get a floating-point result use floating-point operands.
print 235.40/50.23 if the operands are floating-point numbers the result will also be a floating-point number. try to print it on your IDE or see the codes above. Now the result you probably get will be a floating number, check out also the program above after the floating number declaration which uses the "String" concatenation operator which is also "+" in java. if one operand of the "+" operator is a string the result will be the concatenation of the string representations of both operands. see the code above as an example. The output is a string combined from "result:" and 23. Now use the string concatenation operator to print (Pi=3.1415) where 3.1415 is given as a number.




Post a Comment

0 Comments