-
Variables : ManipulatingJava 2022. 2. 1. 17:08
자바에서 변수를 이용하는 기본적인 방법들을 정리하였습니다.
< Introduction >
In this post, learn how to manipulate variables of different data types.
To manipulate the value of variables, we can use expressions, arithmetic operators, and more in order to change our variables’ values.Example)
// declare initial number double number = 20000.99; // declare initial otherNumber double otherNumber = 1000.00; // store result of calculation in our original variable number = number + otherNumber;
: In the final line of the code above, used the expression number + otherNumber to determine the new value of the balance variable. When an expression is executed, it produces a single value.
- The data type of an expression is determined by the resulting value.
For example, an expression that uses two int values will evaluate to an int value. If an expression contains a double value, then the resulting value will also be type double.
< Arithmatic Operator >
To execute basic math operation.
- + : add value
- - : subtract value
- ++ : increase the value by 1
- -- : decrease the value by 1
- * : multiply value
- / : divide value
- % : give a remainder after two numbers are divided.
< Compound Assignment Operators >
To simplify arithmetic operation and reassign.
- Addition +=
- Subtraction -=
- Multiplication *=
- Division /=
- Modulo %=
Example)
int number = 1; // long number = number + 1; // short number += 1;
< Order of Operations >
To dictate the order in which an expression is evaluated.
The order(precedence) :
- Parentheses ( )
- Exponents
- Modulo %/ Multiplication */ Division /
- Addition +/ Subtraction -
- Operators that share the same precedence are evaluated from Left-to-Right within the expression.
< Greater Than and Less Than >
To compare the two numbers using relational operators.
It returns boolean(true/false)- Less than : <
- Greater than : >
- Less than or equal to : <=
- Greater than or equal to : >=
- Is equal to : ==
- Is not equal to : !=
< .equals( ) >
To compare objects, we use .equals( ) instead of ==
It also returns booleanExample)
String person1 = "Lee"; String person2 = "John"; System.out.println(person1.equals(person2)); // false
< String Concatenation >
+ operator work on String too!
Example)
int animals = 12; String species = "zebra"; String zooDescription = "Our zoo has " + animals + " " + species + "s!"; System.out.println(zooDescription); // Our zoo has 12 zebras!
< final Keyword >
To declare a variable with a value that cannot be manipulated(changed), we need to use the final keyword.
When we declare a variable using final, the value cannot be changed; any attempts at doing so will cause an error.Example)
final double pi = 3.14; pi = 3.11; // error: cannot assign a value to final variable pi
'Java' 카테고리의 다른 글
Class : Method (0) 2022.02.04 Class : Introduction (0) 2022.02.03 Variables (0) 2022.01.31 IDE (0) 2022.01.30 Hello World (0) 2022.01.30 - The data type of an expression is determined by the resulting value.