Java for beginners, expressions and operators 2

Introduction

This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ** Wrong ** are the main things. This is for the purpose of posting the part that was actually mistaken during coding and posting it for self-reflection. In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.

environment

Language: Java11, JDK13.0.2 Operating environment: Windows 10

operator

Last time, I dealt with + used for addition and concatenation, and = used for substitution. There are many Java operators, but this time we will deal with typical ones. Listed below.

** 1. Multiplication (*), division (/), remainder (%) ** ** 2. Equivalent (==) and unequal (! =) ** ** 3. Increment (++) and decrement (-), and their pre-postfix **

Multiplication / division / remainder

In Java (at least Python, as I know), use * instead of x for multiplication. Use / instead of ÷ for division, and% for the remainder (which gives the number of remainders).

Division by zero is not allowed in division by zero (division by zero). It does not appear as an error when compiling, but an exception is thrown at the time of execution and the process stops. Some people in Qiita have an article about division by zero on this issue, so I'll link to it (though it's an article over 3 years ago).

"Divid by 0" @yamayasan

Here's an example of what I actually made a mistake.

ZeroDivisionError.java


int dayInMonth = 0;
...
//I forgot to enter a value for dayInMonth in the process so far.
averageFeedCatFood = 
totalFeedCatFood / dayInMonth;

To calculate the average amount of food given to a cat in a month, simply divide the total amount of food by the number of days in a month. It's bad to set the initial value to 0, but don't forget to enter the value in dayInMonth in the middle of the process.

Equivalent and unequal

I wrote last time that = is not equivalent and is used as an assignment. Use == to represent the original equivalence, and! = To represent the inequality (no spaces between = and!). Equivalence and inequality examine the relationship between operands 2 and are also called ** relational operators **. It is often used when equivalence / inequality is used as a condition of an if statement, and it is judged by the truth value of the expression.

ex) if( int lastDayInFeburary == 29){ totalPayment = totalPayment + dailyPayment; } else{ totalPayment = totalPayment; } //2月に29日がある場合、合計支払いに一日分の支払いを追加する処理。

I made a mistake below.

WrongRerational.java


boolean judge = True;
int judgementDay = 30;

if(judgementDay != 30){
    judge = False;
}
else{
    judge = True;
}
System.out.println("Today's announcement" + judge + "So it's not Judgment Day.")
//I simply misunderstood which judge was
//I have made the sentence a helpful sentence.

When it comes to more difficult conditional expressions, if you rely on if, you will be in trouble because the results will be different from what you expected.

Increment decrement

Increment (++) increases the value of the variable by 1 and assigns it, and decrement (-) decreases the value of the variable by 1 and assigns it. This is how to write it. ex) int day++; // This is the same process as int day = day + 1 ;. // int day + = 1 is the same ** (assignment operator) **

int couponTickets--; // This is the same as int couponTickets = couponTickets -1; // couponTickets-= 1 is the same ** (assignment operator) **

Also, this increment / decrement is ** processed differently depending on whether it is placed before or after the variable **.

For example, find out which comment is wrong. Note that it is too long (post the entire class process).

piggyBank.java


class piggyBank
{
    public static void main(String[] args)
    {
        int piggyBank = 99;
        System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
        //99

        System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
        //99

        piggyBank = piggyBank++;
        System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
        //99

        piggyBank = piggyBank++;
        System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
        //99

        piggyBank = ++piggyBank;
        System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
        //100

        piggyBank = ++piggyBank;
        System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
        //101

        piggyBank = piggyBank--;
        System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
        //100?

        piggyBank = piggyBank--;
        System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
        //100

        piggyBank = --piggyBank;
        System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
        //99

        piggyBank = --piggyBank;
        System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
        //99

        System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
        //
    }
}

In the case of postfix, substitute and then add +1 to the value. In the case of prefix, substitute after adding +1 to the value. Review required here.

At the end

I don't think we can understand the equivalence and inequality yet. Review at the time of binary operation. There are many mistakes in increment decrement. You will have to make it yourself and practice.

reference

I haven't written it before, so I'll give you my reference book. Add to past articles. I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.

Easy Java 7th Edition

Recommended Posts

Java for beginners, expressions and operators 2
Learn for the first time java # 3 expressions and operators
Classes and instances Java for beginners
[For beginners] Difference between Java and Kotlin
Kantai Collection Java # 1 Classes and Objects [For Beginners]
[For beginners] About lambda expressions and Stream API
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
Java for beginners, data hiding
Java application for beginners: stream
Java while and for statements
List of frequently used Java instructions (for beginners and beginners)
[For beginners] Explanation of classes, instances, and statics in Java
AWS SDK for Java 1.11.x and 2.x
[For Java beginners] About exception handling
Nowadays Java lambda expressions and Stream API
Learn Java with "So What" [For beginners]
[Java] for Each and sorted in Lambda
Java starting from beginners, logical operators / conditional operators
[Java] Proxy for logging SQL and SQL results
[Java] Calculation mechanism, operators and type conversion
For JAVA learning (2018-03-16-01)
2017 IDE for Java
Java and JavaScript
XXE and Java
Java for statement
Review notes for Java 1.7 and later file copies
A collection of simple questions for Java beginners
[Introduction to Java] Basics of java arithmetic (for beginners)
Let's use Java New FileIO! (Introduction, for beginners)
[Java] for statement, while statement
Getters and setters (Java)
About products using crud processing and devise (for beginners)
[Java] Thread and Runnable
Sample (Java) for OAuth 2.0 authentication and access token acquisition
[For beginners] DI ~ The basics of DI and DI in Spring ~
[Java] Package for management
[Java] Sort the list using streams and lambda expressions
Java true and false
[Java] for statement / extended for statement
[Java] String comparison and && and ||
VSCode Java Debugger for Java Build failed Causes and countermeasures
Understand Java 8 lambda expressions
Comparison operators and conditionals
Countermeasures for Java OutOfMemoryError
[For beginners] Minimum sample to display RecyclerView in Java
Java development for beginners to start from 1-Vol.1-eclipse setup
NLP for Java (NLP4J) (2)
Java --Serialization and Deserialization
[Java] Arguments and parameters
(Memo) Java for statement
NLP for Java (NLP4J) (1)
Introduction to Java for beginners Basic knowledge of Java language ①
Array variables and associative arrays compiled by beginners for beginners
This and that for editing ini in Java. : inieditor-java
About Java lambda expressions
timedatectl and Java TimeZone
[Java] Branch and repeat
Prepare the environment for java11 and javaFx with Ubuntu 18.4
Explain Java 8 lambda expressions
colorize and regular expressions