Java study # 3 (type conversion and instruction execution)

About type conversion

--double d = 3; ← You can assign an int type 3 to a double type variable, --String s = "Best" + 3; ← You can concatenate String type and int type. This is because Java automatically evaluates and converts expressions.

Three types of type conversion

  1. Automatic conversion at the time of substitution
  2. Explicit type conversion
  3. Automatic type conversion during calculation

The automatic type conversion at the time of assignment seems to be that the value is automatically converted to the box and assigned only when the value of "small type" is assigned to the variable of "large type". As an example


float f = 3;
double d = f;

Even so, when it is output, f is converted to 3.0 and d is also converted to 3.0 and substituted. So, if you put a decimal point in the int type, an error will occur. However, there seems to be a method that can be forcibly assigned ...

int a = (int)3.2;

↑ Instruction to convert 3.2 to int and assign This forced instruction (* int) is called a cast operator.

int a = (int)3.2;

Is output, 3 is displayed. Is this the last resort because you are often asked to be accurate? Is this a fairly used technique ...? I'm scared personally so I don't want to use it (^ o ^)

Instruction execution statement

System.out.println("Ranka");

↑ This is the statement of instruction execution In other words, it is in the form of the name (argument); of the command to be called.

Type of instruction

  1. System.out.print (); ← Display characters without line breaks.
  2. int m = Math.max (①, ②); ← Compare the two values and substitute the larger number.
  3. int n = Integer.parseInt (①); ← Convert a character string to a number.
  4. int r = new java.util.Random (). nextInt (①); ← Instruction to generate and assign random numbers
  5. String input = new java.util.Scanner (System.in) .nextInt (); ← Accepts one line of character string input from the keyboard
  6. int input = new java.util.Scanner (System.in) .nextInt (); ← Accepts one integer input from the keyboard

That's why the second chapter is over! After that, I will try the exercises that are prepared. (Because I started working on it earlier than I expected today, w) I haven't been able to memorize it when I get to this point, but I'm excited to be able to write small games using what I've learned ^ _ ^ I was studying with Umesh in one hand today, so I feel tipsy ... Good night (´ ・ ω ゞ)

Recommended Posts

Java study # 3 (type conversion and instruction execution)
Java type conversion
[Java ~ Variable definition, type conversion ~] Study memo
[Java] Calculation mechanism, operators and type conversion
Java instruction execution statement
Java study # 1 (typical type)
[Java] Date type conversion
Java review (2) (calculation, escape sequence, evaluation rule, type conversion, instruction execution statement)
Study Java # 2 (\ mark and operator)
[Java] List type / Array type conversion
[Java] Type conversion speed comparison
Java study # 7 (branch syntax type)
Java Primer Series (Type Conversion)
[Java] Basic types and instruction notes
Java study # 5 (iteration and infinite loop)
Conversion between Kotlin nullable and Java Optional
[Easy-to-understand explanation! ] Reference type type conversion in Java
[Basic knowledge of Java] About type conversion
Java 8 LocalDateTime type conversion stuff (String, java.util.Date)
[Java ~ Classes and External Libraries ~] Study Memo (6)
Type conversion from java BigDecimal type to String type
Let's study Java
[Introduction to Java] About type conversion (cast, promotion)
[JAVA] Stream type
[Java] Enumeration type
Java Optional type
[Processing x Java] Data type and object-oriented programming
Java double type
Java and JavaScript
[Java] Study notes
How to get and study java SE8 Gold
XXE and Java
Java 8 study (repeatable)
Java type conversion (String, int, Date, Calendar, etc.)
Java-automatic type conversion
Java study memorandum
Study Java Silver 1
Java variable declaration, initialization, data type (cast and promotion)
What I learned in Java (Part 3) Instruction execution statement
About Java basic data types and reference type memory
Mutual conversion between Java objects and JSON using Moshi
[Java] Full-width ⇔ half-width conversion
Java true and false
Java Silver Study Day 1
[Java] String comparison and && and ||
[Java] Data type ①-Basic type
Uri → String, String → Uri type conversion
[Java ~ Method ~] Study memo (5)
[Java, Kotlin] Type Variance
Java class type field
Type determination in Java
[Java ~ Array ~] Study memo 4
Java --Serialization and Deserialization
[Java] Arguments and parameters
My Study Note (Java)
timedatectl and Java TimeZone
[Java] Branch and repeat
[Java] About enum type
[Java] Variables and types
java (classes and instances)
Parallel execution in Java