Introduction to java

Introduction

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.

About java

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.

  1. Creating source code
  2. Compile
  3. Run

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.

Rough program writing

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.

About expressions and operators

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.

if statement and for statement

This section describes if and for statements.

About the if statement

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.

About the for statement

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.

Array

Note that when creating an array, the starting point of the element starts at 0.

Method

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.

Impressions

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.

References

Java that you can understand clearly (Impress, Inc. Kiyotaka Nakayama, by Daigo Kunimoto)

Recommended Posts

[Java] Introduction to Java
Introduction to java
Introduction to java command
[Java] Introduction
[Java] Introduction to lambda expressions
[Java] Introduction to Stream API
[Introduction to rock-paper-scissors games] Java
Introduction to Ruby 2
[Introduction to Java] About lambda expressions
[Introduction to Java] About Stream API
Introduction to Functional Programming (Java, Javascript)
Introduction to web3j
Initial introduction to Mac (Java engineer)
Introduction to migration
Introduction to Doma
Introduction to java for the first time # 2
Introduction to algorithms with java --Search (depth-first search)
[Introduction to Java] How to write a Java program
Output of the book "Introduction to Java"
Introduction to monitoring from Java Touching Prometheus
[Introduction to Java] Variable declarations and types
Introduction to Java Web Apps Performance Troubleshooting
Introduction to algorithms with java --Search (breadth-first search)
Java Performance Chapter 1 Introduction
Introduction to JAR files
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
Introduction to design patterns (introduction)
Kotlin's improvements to Java
Introduction to Practical Programming
Introduction to javadoc command
Introduction to jar command
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Introduction to javac command
[Introduction to Java] About type conversion (cast, promotion)
Introduction to algorithms with java --Search (bit full search)
Road to Java Engineer Part1 Introduction & Environment Construction
An introduction to Groovy for tedious Java engineers
[Introduction to Java] Basics of java arithmetic (for beginners)
[Java] How to use Map
How to lower java version
Migration from Cobol to JAVA
[Java] How to use Map
Convert Java Powerpoint to XPS
Java adds table to PDF
How to uninstall Java 8 (Mac)
Java to play with Function
Java --How to make JTable
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to Android application development
Introduction to RSpec 3. Model specs
Introduction to Ratpack (5) --Json & Registry
How to use java Optional