This time, I will briefly explain the necessary parts of the refreshing introduction to Java published by Impress, and describe the questions and personal interpretations about them. This time, I will explain about Chapters 1 to 5.
First of all, java is a language that is relatively object-oriented among programming languages. Object orientation will be described later. To run a java program, roughly follow the steps below.
What I would like you to pay attention to here is the compilation part. An exe file is often generated at compile time, but java generates bytecode called a class file. This class file contains the source code converted into machine language that cannot be read by humans. If you have binary code, you can run the program in any environment. Then use the JVM to load that code. If you have a JVM and binary code, the program will work.
Example.java
public class Example{
publuc static void main(String args[]){
…
…
}
}
As described above, first define the class name and then the method. Make the class name of the class where the main method is described the same as the created java file name. And the class name must start with a capital letter.
When adding the value of a variable, you can add it to the value with the notation ++. However Please note that there will be a difference in the calculation process between ○ ++ and ++ ○. In ○ ++, the process of adding is performed after performing a certain round of processing. In ++ ○, the value is added before the round process is performed, and then the process is performed. This difference will be affected by iterative processing such as the for statement described later.
When writing code, some data type differences will be converted internally. When converting from a small type to a large type, the conversion is done internally, but in the opposite case, it is necessary to explicitly use the cast operator etc. to perform type conversion or explicit type conversion, so be careful. please.
This section describes if and for statements.
When describing a condition in an if statement and branching the process, a relational operator is used for comparison. Use == if the expressions are equivalent. Note that if the relational operator is set to 1, that operator means assignment. When comparing strings, be aware that constructing an expression with strings == will always return false. The character string is stored in the String type, but if you create two String type character strings, different addresses will be assigned to each, and if you express it with String ○○ == String ○○, each element will point to the address instead of the value inside. I will. So it will be false. So use XX.equals () when comparing strings.
As the number of if statements increases, the code becomes more complicated and it becomes difficult to determine the error location, and the result may be different from the expected result. Therefore, use the switch case statement when there are many branches.
The iterative syntax includes a for statement, a while statement, and a do while statement. The criteria for deciding which repetition syntax to use in a program -The number of repetitions is fixed -The number of repetitions is not fixed There are two. Basically, in the former case, it is desirable to use the for statement. In the latter case, it is preferable to use a while statement. The for statement first describes the initial value, the condition (number of times) to exit the for statement, and how to repeat it. For example, 10 When you want to find the total of all in this element array, you can specify the number of repetitions, but when you search for the specified character in a certain character string, it is difficult to decide the repetition cycle from the beginning. The while statement can express the end condition until XX is found instead of XX times, or as long as the character string is not the end. I think it will be easier to create a program if you understand this difference.
Note that when creating an array, the starting point of the element starts at 0.
A method has the same meaning as a function in other languages. Specify the argument and return value and describe the corresponding process in it. Note that if you want to use the variable declared in the main in the method, you cannot use it unless you pass it as an argument.
It's a good idea to use overloads when implementing similar processing in multiple methods. You can declare with the same method name by changing the type of the argument in the method definition. Overloading eliminates the need for others to remember multiple methods one by one.
When passing an array as an argument in a method, note that some pass by value and some pass by reference are reflected in the original value and some are not.
When I read this book with a little learning of other languages this time, there was no rough difference. However, there are still some lumps that are difficult to understand for the classes described from the next time onward and for the String type. To solve this, it is necessary to deepen the knowledge and understanding of object orientation.
Java that you can understand clearly (Impress, Inc. Kiyotaka Nakayama, by Daigo Kunimoto)
Recommended Posts