Java starting from beginner, variables and types

Postscript: 2020/3/4 Added reference books

Introduction

This article is a memorandum. Although it is a reference book level content, the code posted in this article is ** Wrong things ** are the center. 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

What is a variable?

A variable is a mechanism for storing necessary values using the memory of a computer. It can be likened to a ** "box for storage" **.

Mandatory rules for handling variables ** 1, decide the [name] of the variable ** ** 2, after specifying the [type] of the variable ** ** 3, use after [declaring] the variable **

The combination of letters and numbers that can be used to name variables is called an ** Identifier **. However, there are some names that cannot be identifiers due to Java rules.

1. Identifier rules

Below are the wrong examples and why.

Wrong_Identifier.java


1forAll //Don't start with numbers.
class  //So-called "reserved words" that java has reserved for other purposes
all-for^one //Special characters cannot be used.

Here are some examples of names that can be identifiers.

one4all // It's OK because the head is an alphabet. classOfOurs // It's OK if it becomes a word other than a reserved word without inserting a space. all_for_one // Those connected by an underscore are OK.

2. Type rules

A "type" is also called a "data type", and a named variable must have a predetermined value that can be stored in it. The basic Java types will be described later. Here, the contents when the substitution is made by mistake are described.

Wrong_DataType.java


char myCatName = "Tet" //This is a string, char is a single character or only a number
byte characterLevel = 128 //byte is-Can store from 128 to 127

The type determines what can be stored, and at the same time, the type determines how much computer memory is used. In the above example, char is 2 bytes and byte is 1 byte. The largest is 8 bytes of double.

3. Declaration of variables

In Java, variables cannot be used unless they are declared. The following is written in ** Initialization **, which stores the value in the variable at the same time as the variable is declared.

mistakeOfDeclaration.java


int moneyInMyPocket = 200;
int fleshJuice = 120;
moneyInMyHand = 80; //The type of the moneyInMyHand variable is not declared

About the last sentence int moneyInMyHand =moneyInMyPocket - fleshJuice The correct answer is to declare it properly as an int type and write it. The remaining two once declared do not need to be declared again unless the type changes.

Variable assignment

Variables can be assigned to other variables as they are. However, both the type to be assigned and the type to be assigned must be the same. It can be inserted by converting the type, but it is omitted here.

Wrong_Assignment.java


int oneMagicNumber = 33-4
int similarMagicNumber = 33.4

Although similarMagicNumber is declared int, it is assigned a real number including a decimal point instead of an integer. 29, which is the result of the expression, is stored in oneMagicNumber.

reference

I haven't written it before, so I'll give you my reference book. 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 starting from beginner, variables and types
[Java] Variables and types
Java starting from beginner, override
Java, instance starting from beginner
Java starting from beginner, inheritance
Java Primer Series (Variables and Types)
Java overload constructor starting from beginner
Java starting from beginner, nested / break / continue
Java, if statement / switch statement starting from beginner
Java, for statement / while statement starting from beginner
Java starting from beginner, class declaration / object generation
Java programming (variables and data)
Java life starting from scratch
Differences between "beginner" Java and Kotlin
Java, abstract classes starting from beginners
Java variable declaration, initialization, and types
[Java] Basic types and instruction notes
[Java beginner] About abstraction and interface
Basic data types and reference types (Java)
[Java beginner] == operator and equals method
Java array variables are reference types
Java, interface to start from beginner
About Java primitive types and reference types
Java basic data types and reference types
[Java] Exception types and basic processing
java beginner 4
[Introduction to Java] Variables and types (variable declaration, initialization, data type)
Basics of Java development ~ How to write programs (variables and types) ~
java beginner 3
java beginner
Java starting from beginners, logical operators / conditional operators
[Java] Differences between instance variables and class variables
Getting Started with Java Starting from 0 Part 1
Java programming (static clauses and "class variables")
[Introduction to Java] Variable declarations and types
[Java] output, variables
IntelliJ starting from 1
About Java data types (especially primitive types) and literals
Build Java environment and output hello world [Beginner]
[WIP] Java variables
Java and Swift comparison (1) Source control / Scope / Variables
Java Beginner Exercises
Capture and save from selenium installation in Java
Basics of threads and Callable in Java [Beginner]
Java and JavaScript
XXE and Java
For Java engineers starting Kotlin from now on
Generate OffsetDateTime from Clock and LocalDateTime in Java
Java Exercise "Beginner"
Convert Java enum enums and JSON to and from Jackson
[Java] Types of comments and how to write them
Java language from the perspective of Kotlin and C #
[Java] Difference between static final and final in member variables
[Java] Refer to and set private variables with reflection
Java review ① (development steps, basic grammar, variables, data types)
About Java basic data types and reference type memory
I summarized the types and basics of Java exceptions
Reverse Enum constants from strings and values in Java
About Java setters and getters. <Difference from object orientation>
[Java] Platforms to choose from for Java development starting now (2020)
Equivalence comparison of Java wrapper classes and primitive types