Basics of Java development ~ How to write programs (variables and types) ~

Basics of Java development ~ How to write programs (variables and types) ~

Learn the basics of writing java programs through programming that deals with numbers. To learn programming, it is important to understand the concept and learn the minimum rules. Learn the rules as if you were learning a foreign language word. The concept is mathematical or philosophical, so if you don't understand it, remember what is said to be the best.


Introduction

First, let's create the template used in this chapter. HelloWorld Let's create a class named package name lessons class name L1 in the project created by HelloWorld by referring to the procedure for creating a project. Please describe the following program in the file. Click the Run button to run the program that adds 1 to 10.


From now on, I will explain how to write java through this program.

L1.java


package lessons;
public class L1 {
	public static void main(String[] args) {
		int startNum=1;
		int endNum=10;
		int resultNum=0;
		int count=0;
		while(startNum<=endNum) {
			resultNum=startNum+resultNum;
			startNum=++startNum;
			++count;
		}
		startNum=startNum-count;

		System.out.println(startNum+"From"+endNum+"The result of adding up to"+resultNum);
	}
}

variable

A program is something that a repeating computer does. For example, suppose you have a program that adds from 1 to 100. To be honest, if you can only do this, you can't use anything. But wouldn't it be a little more convenient if there were various versatility, such as being able to add from 3 to 103 with the same program, and being able to add from 15 to 30? In order to have versatility, it is not good to write an addition program from 1 to 100 like 1 + 2 + 3 + .... If you write like ʻa + (a + 1) + ((a + 1) + 1) + ... , you can add from 3 to 103 just by changing a to 3. A value that cannot be determined as a cut in order to reuse the <b> program in this way is called a variable </ b>. In the example program, the first number is defined as startNum, the number of additions is defined as ʻendNum, and the result is defined as resultNum.


Java type and variable definitions

This is an example of the formula ʻa + (a + 1) + ((a + 1) + 1) + ... `, but if you are a human, you can see that the value that goes into a is a number. But computers don't know that. Therefore, it is necessary to tell in advance that the value in a is a number. The spell used to teach you is the type.

Variables are defined in the form type variable name. In the example formula above, we define the variable a in the form ʻint a`. The types that are often used are as follows.

Mold What to put
int Numbers
long Large numbers
double Numbers including decimals
boolean Boolean (true false)
String letter

Use = to set a value for a variable.

//Set a value in the variable startNum that handles numbers
int startNum=1;
//Set the value to the variable message that handles characters
String message=startNum+"From"+endNum+"The result of adding up to"+resultNum;

In java programs, enclose strings in "" (double quotation marks).


operator

You don't have to remember anything, but the symbols used in mathematics such as +,-, /, * are called operators. I also use + to easily connect strings other than numbers.

//Variables and character strings are connected and processed into a single character string.
String message=startNum+"From"+endNum+"The result of adding up to"+resultNum;

++ count I don't think I've seen this in mathematics, but think of it as having the same meaning as count = count + 1.


Supplement

I will supplement what I did not touch because I did not know where to explain.


Hands-on

  1. Let's use the sample program and rewrite it to add from 10 to 100.
  2. Change the output message to ○○ is the answer.

Recommended Posts

Basics of Java development ~ How to write programs (variables and types) ~
Java Development Basics ~ How to Write Programs * Exercise 1 ~
[Java] Types of comments and how to write them
Basics of Java development ~ How to write a program (flow and conditional branching) ~
[Java] How to output and write files!
[Java] Variables and types
JDBC promises and examples of how to write
How to write Scala from the perspective of Java
How to write java comments
I summarized the types and basics of Java exceptions
How to use Java variables
A memo about the types of Java O/R mappers and how to select them
I tried to summarize the basics of kotlin and java
Comparison of how to write Callback function (Java, JavaScript, Ruby)
How to write and notes when migrating from VB to JAVA
Studying Java # 6 (How to write blocks)
Java Primer Series (Variables and Types)
[Processing × Java] How to use variables
How to write Java variable declaration
How to name variables in Java
[Introduction to Java] Variables and types (variable declaration, initialization, data type)
Collection of programming selection tasks to make and remember (Java basics)
How to write offline real-time Java implementation example of F01 problem
Java starting from beginner, variables and types
How to name variables 7 selections of discomfort
[java] Summary of how to handle char
Summary of how to write annotation arguments
[Introduction to Java] How to write a Java program
[Java] [Maven3] Summary of how to use Maven3
[Introduction to Java] Variable declarations and types
How to write and explain Dockerfile, docker-compose
Basics of java basics ② ~ if statement and switch statement ~
Java beginner escape boot camp Part 1 Java class structure and how to write
[Java] Note how to use RecyclerView and implementation of animated swipe processing.
[Java improvement case] How to reach the limit of self-study and beyond
[Java] How to use FileReader class and BufferedReader class
[java] Summary of how to handle character strings
Java Development Basics ~ Development Environment Settings and Project Creation ~
[Java] How to get and output standard input
[Java] Summary of how to abbreviate lambda expressions
Basics of threads and Callable in Java [Beginner]
How to get and study java SE8 Gold
[Java] Memo on how to write the source
[Java] How to get the authority of the folder
How to write Java String # getBytes in Kotlin?
[Introduction to Java] Basics of java arithmetic (for beginners)
How to access Java Private methods and fields
[Java] How to use Calendar class and Date class
Differences between Java, C # and JavaScript (how to determine the degree of obesity)
[Java] How to get the URL of the transition source
[Java] How to use compareTo method of Date class
How to write offline real time Implementation example by ruby and C99 of F04
How to write Rails
java: How to write a generic type list [Note]
[Java] How to get the maximum value of HashMap
Java memory management and how to read GC Viewer
[Java] Refer to and set private variables with reflection
Java review ① (development steps, basic grammar, variables, data types)
How to write dockerfile
As of April 2018 How to get Java 8 on Mac
How to write docker-compose