[Java ~ Variable definition, type conversion ~] Study memo

Learning Java. I will make a note so that I can look back on it for review. It's almost my study memo. Please do not excessive expectations.

Output instructions

--In programming, a computer is made to perform various instructions and processes. Write System.out.println () to output (display) the contents of (). "

--String Enclose the string in double quotation marks ("). The code will not work unless it is enclosed in "".

--Semicolon In Java, you must add a semicolon (;) to the end of the statement. If you forget this, the code will not work.

--Comment All comments are ignored when the code is executed. Therefore, use it when you want to keep notes. By writing " // " </ b>, the part from there to the end of the line is regarded as a comment.

Example


//Output as Hello World
System.out.println("Hello World");

//Output result → Hello World

--Numerical value Numbers are not enclosed in double quotes, unlike strings. All symbols are written in half-width.

Example


//Output the number 17
System.out.println(17);
//Output result → 17

//Outputs 5 plus 3
System.out.println(5 + 3);
//Output result → 8
    
// 「5 +Output "3" as a character string
System.out.println("5 + 3");
//Output result → 5+ 3

--Concatenation of strings

Example


// "Hello"When"world"Concatenate and output
System.out.println("Hello" + "world");
//Output result → Hello world
    
// "38"When"19"Concatenate and output
System.out.println("38" + "19");
//Output result → 3819


Variable definition

--A variable is a box for storing data (value). To use a variable, you first need to "define" it. To define variables in Java (1) Specify the data type of the value to be put in the variable ② Decide the name of the variable Two tasks are required.

--Data type There are two types of data types, character strings and integers. The String type is the character string type, and the ʻint type is the integer type`. Note that the String type S is uppercase and the int type i is lowercase. In addition, there is also a double type </ b> data type that represents a decimal number.

--Value assignment After defining the variable, put the value in the variable. It can be assigned with " variable name = value ".

Example


//Define int type variable number
int number;
    
//Substitute 3 for variable number
number = 3;
    
//Output variable number
System.out.println(number);

//Output result → 3



//Define a variable name of type String
String name;
    
//In the variable name"Aki"Substitute
name = "Aki";
    
//Output variable name
System.out.println(name);

//Output result → Aki

--Variable initialization It means assigning a value at the same time as defining a variable.

How to write


//Define int type variable number and assign 7
int number = 7;

//Define the variable text of String and substitute "Good morning"
String text = "Good morning";


--Variable update Be careful not to add a data type when updating. It tries to define a variable by prepending the data type to the variable name, Variables with the same name cannot be defined in the same process, so do not add a data type when updating.

How to write


int number = 7;

//Overwrite variable number with 9
number = 9;

//Output result → 9

--Self-substitution Add the variables number and 8 and assign to the variable number again, This method of substitution is called self-assignment.

How to write


int number = 7;

//Overwrite variable number by adding 8 to the value of variable number
number = number + 8;

//Output result → 15

--Omission of self-assignment Uninflected word: X = X + 1; → Abbreviation: X + = 1; → Further abbreviation: X ++; Uninflected word: X = X-1; → Abbreviation: X-= 1; → Further abbreviation: X-;

Type conversion

--Operations such as "+" can only be performed with the same data type. Therefore, when operating on different types, the types are converted to the same type. In Java, there are automatic conversion and manual conversion as methods of this type conversion.

--Automatic type conversion If you add String type and int type as shown below, int type is automatically converted to String type. The strings are combined.

Example


int month = 12;
int date = 31;
    
//Output by concatenating variables and character strings so that it becomes "December 31"
System.out.println(month + "Moon" + date + "Day");

//Output result → December 31st

The result of int type and double type calculation is double type.

Example


//7 to 2.Please output the value divided by 0
System.out.println(7/2.0);

//Output result → 3.5


// 7.Please output the value obtained by dividing 0 by 2.
System.out.println(7.0/2);

//Output result → 3.5

--Forced conversion The type is forcibly converted by setting (data type to be converted) value.

Example


int number1 = 7;
int number2 = 2;

//Convert number1 to double type and output the value divided by number2
System.out.println((double)number1 / number2);

//Output result → 3.5

Recommended Posts