第3週目:演算子
6091 ワード
n/a.ターゲット
Javaが提供する様々な演算子を学習します.
勉強する算術演算子 単項演算子 ビット演算子 関係演算子 論理演算子 instanceof assignment(=) operator 矢印(->)演算子 3演算子 演算子優先度 Java 13. switch演算子 算術演算子
Javaが提供する様々な演算子を学習します.
勉強する
算術演算子
ソース:https://www.programiz.com/java-programming/operators
数学計算に使用される演算子と被演算子の演算方向は左から右です.class Main {
public static void main(String[] args) {
// declare variables
int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
System.out.println("a % b = " + (a % b));
}
}
Outputa + b = 17
a - b = 7
a * b = 60
a / b = 2
a % b = 2
assignment(=) operator
ソース:https://www.programiz.com/java-programming/operators
変数に値を割り当てる演算子を指定します.class Main {
public static void main(String[] args) {
// create variables
int a = 4;
int var;
// assign value using =
var = a;
System.out.println("var using =: " + var);
// assign value using =+
var += a;
System.out.println("var using +=: " + var);
// assign value using =*
var *= a;
System.out.println("var using *=: " + var);
}
}
Outputvar using =: 4
var using +=: 8
var using *=: 32
関係演算子
ソース:https://www.programiz.com/java-programming/operators
関係演算子は、2つの被演算子間の関係を決定するために使用され、結果が真の場合は偽の真を返します.class Main {
public static void main(String[] args) {
// create variables
int a = 7, b = 11;
// value of a and b
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
// > operator
System.out.println(a > b); // false
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false
// <= operator
System.out.println(a <= b); // true
}
}
論理演算子
ソース:https://www.programiz.com/java-programming/operators
論理演算子は、式の真または偽を決定するために使用されます.class Main {
public static void main(String[] args) {
// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false
// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false
// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}
ソース:https://www.programiz.com/java-programming/operators
上記のように括弧内の式を先に判断してから演算してもよい.
単項演算子
ソース:https://www.programiz.com/java-programming/operators
単項演算子は1つの被演算子のみを使用します.
class Main {
public static void main(String[] args) { // declare variables
int a = 12, b = 12;
int result1, result2;
// original value
System.out.println("Value of a: " + a);
// increment operator
result1 = ++a;
System.out.println("After increment: " + result1);
System.out.println("Value of b: " + b);
// decrement operator
result2 = --b;
System.out.println("After decrement: " + result2);
}
}
OutputValue of a: 12
After increment: 13
Value of b: 12
After decrement: 11
ビット演算子
ソース:https://www.programiz.com/java-programming/operators
ビット演算子は、ビットの演算に使用されます.
instanceof
A(オブジェクト)instanceof B(クラス)
オブジェクトの実際の参照のタイプを検索し、変換できるかどうかを計算します.演算結果としてTrueとFalseを返します.
関係を継承するときに使用する必要があります.
親子関係なら真
自分の関係なら、それは本当です.
親子関係の場合はFalseclass Main {
public static void main(String[] args) {
String str = "Programiz";
boolean result;
// checks if str is an instance of
// the String class
result = str instanceof String;
System.out.println("Is str an object of String? " + result);
}
}
OutputIs str an object of String? true
矢印(->)演算子
さんこうえんざんし
演算子の優先度
Java 13. スイッチ演算子
Reference
この問題について(第3週目:演算子), 我々は、より多くの情報をここで見つけました
https://velog.io/@dwroyal02/3주차-연산자
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
class Main {
public static void main(String[] args) {
// declare variables
int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
System.out.println("a % b = " + (a % b));
}
}
a + b = 17
a - b = 7
a * b = 60
a / b = 2
a % b = 2
ソース:https://www.programiz.com/java-programming/operators
変数に値を割り当てる演算子を指定します.
class Main {
public static void main(String[] args) {
// create variables
int a = 4;
int var;
// assign value using =
var = a;
System.out.println("var using =: " + var);
// assign value using =+
var += a;
System.out.println("var using +=: " + var);
// assign value using =*
var *= a;
System.out.println("var using *=: " + var);
}
}
Outputvar using =: 4
var using +=: 8
var using *=: 32
関係演算子
ソース:https://www.programiz.com/java-programming/operators
関係演算子は、2つの被演算子間の関係を決定するために使用され、結果が真の場合は偽の真を返します.class Main {
public static void main(String[] args) {
// create variables
int a = 7, b = 11;
// value of a and b
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
// > operator
System.out.println(a > b); // false
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false
// <= operator
System.out.println(a <= b); // true
}
}
論理演算子
ソース:https://www.programiz.com/java-programming/operators
論理演算子は、式の真または偽を決定するために使用されます.class Main {
public static void main(String[] args) {
// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false
// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false
// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}
ソース:https://www.programiz.com/java-programming/operators
上記のように括弧内の式を先に判断してから演算してもよい.
単項演算子
ソース:https://www.programiz.com/java-programming/operators
単項演算子は1つの被演算子のみを使用します.
class Main {
public static void main(String[] args) { // declare variables
int a = 12, b = 12;
int result1, result2;
// original value
System.out.println("Value of a: " + a);
// increment operator
result1 = ++a;
System.out.println("After increment: " + result1);
System.out.println("Value of b: " + b);
// decrement operator
result2 = --b;
System.out.println("After decrement: " + result2);
}
}
OutputValue of a: 12
After increment: 13
Value of b: 12
After decrement: 11
ビット演算子
ソース:https://www.programiz.com/java-programming/operators
ビット演算子は、ビットの演算に使用されます.
instanceof
A(オブジェクト)instanceof B(クラス)
オブジェクトの実際の参照のタイプを検索し、変換できるかどうかを計算します.演算結果としてTrueとFalseを返します.
関係を継承するときに使用する必要があります.
親子関係なら真
自分の関係なら、それは本当です.
親子関係の場合はFalseclass Main {
public static void main(String[] args) {
String str = "Programiz";
boolean result;
// checks if str is an instance of
// the String class
result = str instanceof String;
System.out.println("Is str an object of String? " + result);
}
}
OutputIs str an object of String? true
矢印(->)演算子
さんこうえんざんし
演算子の優先度
Java 13. スイッチ演算子
Reference
この問題について(第3週目:演算子), 我々は、より多くの情報をここで見つけました
https://velog.io/@dwroyal02/3주차-연산자
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
class Main {
public static void main(String[] args) {
// create variables
int a = 7, b = 11;
// value of a and b
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
// > operator
System.out.println(a > b); // false
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false
// <= operator
System.out.println(a <= b); // true
}
}
ソース:https://www.programiz.com/java-programming/operators
論理演算子は、式の真または偽を決定するために使用されます.
class Main {
public static void main(String[] args) {
// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false
// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false
// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}
ソース:https://www.programiz.com/java-programming/operators上記のように括弧内の式を先に判断してから演算してもよい.
単項演算子
ソース:https://www.programiz.com/java-programming/operators
単項演算子は1つの被演算子のみを使用します.
class Main {
public static void main(String[] args) { // declare variables
int a = 12, b = 12;
int result1, result2;
// original value
System.out.println("Value of a: " + a);
// increment operator
result1 = ++a;
System.out.println("After increment: " + result1);
System.out.println("Value of b: " + b);
// decrement operator
result2 = --b;
System.out.println("After decrement: " + result2);
}
}
OutputValue of a: 12
After increment: 13
Value of b: 12
After decrement: 11
ビット演算子
ソース:https://www.programiz.com/java-programming/operators
ビット演算子は、ビットの演算に使用されます.
instanceof
A(オブジェクト)instanceof B(クラス)
オブジェクトの実際の参照のタイプを検索し、変換できるかどうかを計算します.演算結果としてTrueとFalseを返します.
関係を継承するときに使用する必要があります.
親子関係なら真
自分の関係なら、それは本当です.
親子関係の場合はFalseclass Main {
public static void main(String[] args) {
String str = "Programiz";
boolean result;
// checks if str is an instance of
// the String class
result = str instanceof String;
System.out.println("Is str an object of String? " + result);
}
}
OutputIs str an object of String? true
矢印(->)演算子
さんこうえんざんし
演算子の優先度
Java 13. スイッチ演算子
Reference
この問題について(第3週目:演算子), 我々は、より多くの情報をここで見つけました
https://velog.io/@dwroyal02/3주차-연산자
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
// declare variables
int a = 12, b = 12;
int result1, result2;
// original value
System.out.println("Value of a: " + a);
// increment operator
result1 = ++a;
System.out.println("After increment: " + result1);
System.out.println("Value of b: " + b);
// decrement operator
result2 = --b;
System.out.println("After decrement: " + result2);
}
}
Value of a: 12
After increment: 13
Value of b: 12
After decrement: 11
ソース:https://www.programiz.com/java-programming/operators
ビット演算子は、ビットの演算に使用されます.
instanceof
A(オブジェクト)instanceof B(クラス)
オブジェクトの実際の参照のタイプを検索し、変換できるかどうかを計算します.演算結果としてTrueとFalseを返します.
関係を継承するときに使用する必要があります.
親子関係なら真
自分の関係なら、それは本当です.
親子関係の場合はFalseclass Main {
public static void main(String[] args) {
String str = "Programiz";
boolean result;
// checks if str is an instance of
// the String class
result = str instanceof String;
System.out.println("Is str an object of String? " + result);
}
}
OutputIs str an object of String? true
矢印(->)演算子
さんこうえんざんし
演算子の優先度
Java 13. スイッチ演算子
Reference
この問題について(第3週目:演算子), 我々は、より多くの情報をここで見つけました
https://velog.io/@dwroyal02/3주차-연산자
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
class Main {
public static void main(String[] args) {
String str = "Programiz";
boolean result;
// checks if str is an instance of
// the String class
result = str instanceof String;
System.out.println("Is str an object of String? " + result);
}
}
Is str an object of String? true
さんこうえんざんし
演算子の優先度
Java 13. スイッチ演算子
Reference
この問題について(第3週目:演算子), 我々は、より多くの情報をここで見つけました
https://velog.io/@dwroyal02/3주차-연산자
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
Java 13. スイッチ演算子
Reference
この問題について(第3週目:演算子), 我々は、より多くの情報をここで見つけました
https://velog.io/@dwroyal02/3주차-연산자
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
Reference
この問題について(第3週目:演算子), 我々は、より多くの情報をここで見つけました https://velog.io/@dwroyal02/3주차-연산자テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol