Nice to meet you. I am a graduate student who has no choice. Again. We are conducting research on networks in a laboratory surrounded by many pickled stones. Since this is my first time writing a blog, I think that both quantity and quality will be poor, but I look forward to working with you in the future.
An acquaintance who got a job in the IT industry this year told me to teach the basics of Java, so I decided to write an article. I hope it will be helpful for people in similar circumstances. Also, please read the references at the bottom of the page for books that have helped me a lot in learning Java.
--Those who understand other languages but do not understand Java very much --Those who were studying Java but got stuck in the middle and gave up
This time, I will touch on some of the classes that are an essential part of object-oriented programming for understanding Java. I will write about the class over the next few days, so thank you.
If you know C language, you should recognize the class as if it had hair on the structure. C language: wa, data Java: let data wa The Java language has classes and methods. A class is a bundle of methods. As I will write in a future article, you can make effective use of classes only by using instances and encapsulation. In this article, I hope you can get a feel for what the class is. The advantage of using classes is that the code is easier to manage and more readable. Now, let's actually compare the four arithmetic programs written in C and Java, and explain that using classes improves code management and readability.
Left half C language, right half Java
#include <stdio.h>
#include <stdlib.h>
typedef struct calculate { public class Calculate {
int a, b; int a, b;
} *Calculate;
Calculate new_Calculate(int a, int b) { Calculate(int a, int b) {
Calculate this = malloc(sizeof(*this));
this->a = a; this.a = a;
this->b = b; this.b = b;
return this;
} }
int add(Calculate this) { int add() {
return this->a + this->b; return this.a + this.b;
} }
int main(int argc, char *argv[]) { public static void main(String[] args) {
Calculate data = new_Calculate(123, 456); Calculate data = new Calculate(123, 456);
printf("%d\n", add(data)); System.out.printf("%d\n", data.add());
} }
}
In C language, four arithmetic operations are defined separately as functions, but in Java language, they can be described together in one class called keisan. If you want to write a program that finds the remainder of the quotient here, you have to add more functions in C language. However, in the Java language, you can add it to the keisan class. By using classes in this way, it becomes easier to understand where and what kind of program is. In other words, it makes the code easier to manage and improves readability. The larger and more complex the program, the more noticeable this difference will be. I will write about public and static described in Java programs in a future article. Please read it for now.
I wrote a blog for the first time, but it's very difficult. .. .. Let's get used to it and improve the quantity and quality! !! !! So the goal is to publish articles every day during GW. I will do my best. I would appreciate it if you could point out any mistakes in this article. Tomorrow I will write about calling another class. If you like, please stay with me tomorrow. Thank you for reading this far.
[Introduction to Java 2nd Edition (Refreshing Series)](https://www.amazon.co.jp/%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA % E3% 82% 8F% E3% 81% 8B% E3% 82% 8BJava% E5% 85% A5% E9% 96% 80-% E7% AC% AC2% E7% 89% 88-% E3% 82% B9 % E3% 83% 83% E3% 82% AD% E3% 83% AA% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA-% E4% B8% AD% E5% B1% B1-% E6% B8% 85% E5% 96% AC / dp / 484433638X / ref = zg_bs_515820_1? _Encoding = UTF8 & psc = 1 & refRID = JGCC33P1VGV3V5VKQGQR)
Recommended Posts