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 ...)
First, from the same part. Both languages
for (int i = 0; i < size; ++i) {
a[i] = i;
}
Can be used!
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?).
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 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