[Introduction to Java] Basics of java arithmetic (for beginners)

I'm a fledgling programmer for the first year. This is a collection of personal notes that have been organized and published for review purposes. Please note that there is a high possibility that you are wrong.

Attitude

――If you do it with the image of mathematics that you learned at school, you will be surprised at the difference

x = x + 1;

And

count += 2;

Or, as a programmer, it feels like "Oh, yes", but for those who are just starting programming, it can be "! ??".

――Since it may differ from the meaning of general mathematical operators in this way, it is basically necessary to think of it as a different meaning.

On top of that, you should find a common place.

Expression components

  1. Operator: "+" "-" "=", etc.
  2. Operand: Other than the operator

Literal

--The value described in the source code, which is an element in itself --The difference from operands is that literals have the described value itself as an element, but operands do not necessarily have the described value as the element itself.

int a , b;
a = 10;          //10 is an integer literal
b = a + 27;     //a,b,27 is an operand, but literals are only 27

boolean flag;
flag = true;     //true is a boolean literal

Evaluation

--The operator performs calculations using the surrounding operands and changes to values with the operands.

Example


int numA = 1;
int numB = 2;
int ans = numA + numB;
//operator+Changes to a value of 3 with numA and numB.

if(numA < numB){
    System.out.println("numA is less than numB");
}
//operator>Changes to true with numA and numB

System.out.println(numA == numB);
//When you actually output characters, the result will be false

operator

--The site on the right is easy to understand (round throw). Reference) Java Road: Operator

――Roughly summarize in a table

type operator Meaning (roughly) Remarks
Arithmetic operator + Add
- Pull
* Hang
/ Divide
% Ask for the remainder
Assignment operator = Substitute the right side for the left side
+= Substitute the value obtained by adding the right side to the left side
-= Substitute the value obtained by dividing the right side from the left side
*= Substitute the value obtained by multiplying the left side by the right side
/= Substitute the value obtained by dividing the right side from the left side
%= Divide the left side Substitute the remainder of the right side
Increment operator ++ Increase the value by 1 a++When++The meaning is different with a
† Note 1
Decrement operator -- Decrease the value by 1 a--When--The meaning is different with a
† Note 1
Comparison operator == equal † Note 2
!= Not equal
> large
>= that's all
< small
<= Less than
Logical operator & And
&& And Perform short-circuit evaluation
† Note 3
|
|| Or Perform short-circuit evaluation
† Note 3
! denial

† Note 1: For a ++, assign a value and then add +1. ++ a is +1 and then assigned.

Example


int a = 0;
int b = 0;
System.out.println(a++);    //The result is 0
System.out.println(++b);    //The result is 1

System.out.println(a);    //The result is 1
System.out.println(b);    //The result is 1

But in fact, most of the time I see ```i ++; `` `alone, and I haven't seen or written the code (for now) if I can't distinguish it. I think you should put it in the corner of your head.

† Note 2: Be careful when comparing objects. Reference) Comparison of objects † Note 3: What is short-circuit evaluation?

int num = 5;
if(num > 2 || num > 7){
    //Process A
} else {
    //Process B
}

If there is a process like this, num> 7 is not evaluated because it is confirmed that it will be true when the evaluation of num> 2 is completed. When the evaluation is confirmed in this way, not performing unnecessary evaluation after that is called ___ short-circuit evaluation ___.

Ternary operator

--Use as below

How to write


[conditions] ? [Return value if TRUE] : [Return value for FALSE] ;

――Be careful because it is said that readability is poor if you use it easily. Until you get used to it, you should remember it exclusively when reading

int n = a > 0 ? 1 : 0 ;
//The meaning is the same as below

int n;
if(a > 0){
    n = 1;
} else {
    n = 0;
}

reference)

-Is the ternary operator evil?

Recommended Posts

[Introduction to Java] Basics of java arithmetic (for beginners)
[For beginners] Introduction to Java Basic knowledge of Java language ③ Array, selection structure, iteration structure
[Java] Introduction to Java
Introduction to java
Output of the book "Introduction to Java"
A collection of simple questions for Java beginners
Introduction to java command
An introduction to Groovy for tedious Java engineers
Let's use Java New FileIO! (Introduction, for beginners)
[For beginners] DI ~ The basics of DI and DI in Spring ~
Initialization of for Try to make Java problem TypeScript 5-4
[For beginners] Minimum sample to display RecyclerView in Java
Java development for beginners to start from 1-Vol.1-eclipse setup
List of frequently used Java instructions (for beginners and beginners)
How to execute WebCamCapture sample of NyARToolkit for Java
[Rails] Introduction of devise Basics
[Java] Beginner's understanding of Servlet-②
Basics of character operation (java)
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
[Java] Beginner's understanding of Servlet-①
[For super beginners] DBUnit super introduction
[For super beginners] Ant super introduction
Summary of Java language basics
[Java] Summary of for statements
Java for beginners, data hiding
[Java] Introduction to Stream API
[For super beginners] Maven super introduction
Java application for beginners: stream
[Introduction to rock-paper-scissors games] Java
[For beginners] Explanation of classes, instances, and statics in Java
I tried to summarize the basics of kotlin and java
For Java beginners: List, Map, Iterator / Array ... How to convert?
Reintroduction to Java for Humanities 0: Understanding the Act of Programming
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
[Rails] Introduction of Rubocop by beginners
[Introduction to Java] About lambda expressions
[Introduction to Java] About Stream API
Introduction to Functional Programming (Java, Javascript)
[Java] Input to stdin of Process
[For super beginners] Mirage SQL super introduction
From introduction to use of ActiveHash
Initial introduction to Mac (Java engineer)
Introduction to Programming for College Students: Introduction
From introduction to usage of byebug
Generics of Kotlin for Java developers
Java, arrays to start with beginners
Classes and instances Java for beginners
Basics of Java development ~ How to write programs (variables and types) ~
Collection of programming selection tasks to make and remember (Java basics)
How to check for the contents of a java fixed-length string
Implementation of clone method for Java Record
[Java] Flow from introduction of STS to confirmation of dynamic page on localhost (2/3)
Introduction to Programming for College Students: Variables
Memorandum of new graduate SES [Java basics]
Learn Java with "So What" [For beginners]
[Java] Flow from introduction of STS to confirmation of dynamic page on localhost (1/3)
About the procedure for java to work
Introduction to algorithms with java --Search (depth-first search)
Practice of Java programming basics-I want to display triangles with for statements ①
[Introduction to Java] How to write a Java program