A person writing C ++ tried writing Java

I usually write C ++, but for some reason I suddenly decided to write Java. So, I will write a simple comparison if you have any impressions. (I wrote it with a much more appropriate feeling than other articles ...)

for statement

First, from the same part. Both languages

for (int i = 0; i < size; ++i) {
    a[i] = i;
}

Can be used!

Mold

type C++ Java
logic bool boolean
letter char char
integer (signed) short, int, long, long long byte, short, int, long
Floating point number float, double, long double float, double

――Basically, there are many similar things. --The spelling of the logical type is different. --There are no unsigned integers in Java.

The ones listed here are called basic data types, primitive types, etc. in Java. There are a lot of other classes that are created, but they make a big difference between C ++ and Java. If the type name is Test

C++ Java (basic data type) Java (other classes)
Test test; Test test; None
Test* test;,Test& test; None Test test;

It feels like. In Java, anything that is not a basic data type is a reference type that has an address inside. Members are accessed in the form `test.member```, but can be replaced unlike C ++ references, or with new like Test test = new Test (); ``. It also has pointer-like characteristics such as initialization. By the way, Java has a convenient specification that if you leave it alone without delete, it will be deleted automatically (is it counting the number of references like shared_ptr?).

Classes and functions

In C ++

class Test {
private:
    double _val;
public:
    Test(double val) : _val(val) {}
    double get() const { return _val; }
    void set(double val) { _val = val; }
};

What is written is Java

public class Test {
    private double _val;
    Test(double val) {
        _val = val;
    }
    public double get() {
        return _val;
    }
    public void set(double val) {
        _val = val;
    }
} 
// ;I don't need

It will be. Basically, you can make a class with similar feelings. However, in C ++, the class is only in the public state, but in Java you can choose several types. Also, write private / public of member variables / functions for each member. And it is meaningful to not write the access specification. Also, the meaning of protected is different. The access specifications of the members in the class are as follows.

Designation Meaning in Java C++Meaning in
private Only from within the class Only from within the class
None Only in the same package Same as private
protected Within the same package or from a subclass From within a class or a derived class
public From anywhere From anywhere

The package is similar to the C ++ namespace, but I think it differs greatly from C ++ in that it is created using a directory structure.

Inheritance

Inheritance in C ++

class Derived : public Base {
    (members)
};

But with Java

public class Derived extends Base {
    (members)
}

is. There is no base class access specification, just write ʻextends Base. Members can be overridden without specifying virtual. Use final` to prevent it. In addition to mere inheritance, there is also an interface,

interface Base {
    void func();
}
class Impl implements Base {
    void func() {
        // implementation
    }
}

Write like this. It looks similar to an abstract class in C ++. Variables of Java derived classes (subclasses) can be normally assigned to variables of the base class, and functions overloaded on the derived class side can be called from variables of the base class (same as C ++ reference).

Although there are various differences like this, basic object-oriented programming could be done in Java as it is. It was fun to write a program normally just by buying one reference book.

Recommended Posts

A person writing C ++ tried writing Java
If a person from Java learns PHP
java I tried to break a simple block
I tried hitting a Java method from ABCL
Differences in writing Java, C # and Javascript classes
Summarize the differences between C # and Java writing
I tried to break a block with java (1)
I tried running Java on a Mac terminal
ABC --013-A & B & C
ABC --023 --A & B & C
ABC --036-A & B & C
ABC --010 --A & B & C
ABC --028 --A & B & C
[Java] Create a filter
ABC --015 --A & B & C
ABC --128 --A & B & C
ABC --012-A & B & C
ABC --018 --A & B & C
ABC --054 --A & B & C
ABC --017 --A & B & C
ABC --029- A & B & C
java build a triangle
ABC --022 --A & B & C
I tried to create a Clova skill in Java
ABC --019 --A & B & C
ABC --020 --A & B & C
ABC --030- A & B & C
ABC --127 --A & B & C
I tried to make a login function in Java
I tried using Log4j2 on a Java EE server
ABC --007 --A & B & C
ABC --132- A & B & C
ABC --026 --A & B & C
ABC --014- A & B & C
I tried OCR processing a PDF file with Java
[Java] When writing the source ... A memorandum of understanding ①
ABC --016 --A & B & C
ABC --011-A & B & C
ABC --031 --A & B & C
ABC --021 --A & B & C
ABC --025 --A & B & C
I tried scraping a stock chart using Java (Jsoup)
I tried to chew C # (reading and writing files)
ABC --024 --A & B & C
ABC --027 --A & B & C
ABC --080- A & B & C
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
Update your Java knowledge by writing a gRPC server in Java (1)
I tried to convert a string to a LocalDate type in Java
I tried to make a client of RESAS-API in Java
I tried OCR processing a PDF file with Java part2
A note about Java GC
ABC --129- A & B & C & D
ABC --133- A & B & C & D
Create a java method [Memo] [java11]
ABC --122 --A & B & C & D
Fastest Primality Test C # Java C ++
[Java] Create a temporary file
I tried Drools (Java, InputStream)
Find a subset in Java