[JAVA] Basic operators and operations

Operators of four arithmetic operations

Addition Subtraction (Hikizan) Multiplication Division (division)

In addition to this, there is a remainder division (modulo operation) that finds the remainder after division. The four arithmetic operations are used in exactly the same way as mathematical formulas. The only difference is that the multiplication (x) uses (*) and the division uses (/) instead of (÷).

result1 = x + y;    //Addition
result2 = x - y;    //Subtraction
result3 = x * y;    //Multiplication
result4 = x / y;    //division
result5 = x % y;    //Modulo arithmetic

スクリーンショット (34).png

The above calculation is also possible with integer type such as int and floating point type.

String concatenation

The "+" symbol can be used not only for addition but also for concatenating character strings. If you select "abc" + "de", the output result will be "abcde".

String str = "Hello" + "Qiita" + "Mr.";
System.out.println(str);

You can concatenate variables. You can concatenate string literals and variables with "+".

String name = "Qiita";
System.out.println("Good evening" + name + "Mr.");

Also, the most important function of the string concatenation operator "+" is to concatenate strings with any type of data. When concatenating with this function, any type of data will be converted to "String type" and concatenated.

Create a project to check the operation so far.

eclipse is not used this time

Operator/
    ├──Operator.java

Operator.java


public class Operator{

	public static void main(String[] args){
		
		int x = 6;
		int y = 3;

		int result1 = 0;
		int result2 = 0;
		int result3 = 0;
		int result4 = 0;

		//Operators of four arithmetic operations
		result1 = x + y;	//Addition
		result2 = x - y;	//Subtraction
		result3 = x * y;	//Multiplication
		result4 = x / y;	//division

		System.out.println( result1 + "=" + x + "+"+ y);
		System.out.println( result2 + "=" + x + "-"+ y);
		System.out.println( result3 + "=" + x + "*"+ y);
		System.out.println( result4 + "=" + x + "/"+ y);

		//String concatenation
		String str = "Hello" + "Qiita" + "Mr.";
		System.out.println(str);

		//String literals and variables"+"Connect using
		String name = "Qiita";
		System.out.println("Good evening" + name + "Mr.");
	}
}

スクリーンショット (36).png

Recommended Posts

Basic operators and operations
Comparison operators and conditionals
Basic data type and reference type
JavaScript overview and basic grammar
Ruby methods and classes (basic)
11 Corresponds to comparison and logical operators
[Java] Basic types and instruction notes
Java for beginners, expressions and operators 1
Basic data types and reference types (Java)
Java for beginners, expressions and operators 2
Relationship between database and model (basic)
Reading and writing Java basic files
[Ruby] If and else problems-with operators-
[Ruby] Boolean values ​​and logical operators
TCP: Basic Principles and Application Architecture
Java basic data types and reference types
Ruby's && and || are not conditional operators
String operations this and that-Java edition-
With ruby ● × Game and Othello (basic review)
Differences between preface and postfix of operators
Basic usage of enums and code examples
Memorandum (Ruby: Basic Grammar: Classes and Instances)
[Note] Cooperation between Java and DB (basic)
Basic CRUD comparison between Mybatis and Hibernate