Week 16 Day 2 - Java for C++ programmers

Channel:
Subscribers:
2,640
Published on ● Video Link: https://www.youtube.com/watch?v=xASxcIGLnZU



Duration: 1:30:58
131 views
0


Today we went over the basics of the Java languages. The core language is very, very close to C++, with a few differences. In short, if you know C++, then you know most of Java. The key differences in the language are:
1) No unsigned integers and some small differences in primitive types
2) No public or private sections, but rather each member is tagged as public, private, protected or nothing (nothing means private, but public to other classes in your package)
3) Everything is in a class - no global (free) functions or variables
4) Everything is bounds checked by the Java virtual machine. You can't have a dangling pointer or go out of bounds or do a double free in Java.
5) Every class inherits from one class (if you don't specify one, it will inherit from Object, the Java superclass)
6) Every non-primitive variable is new-ed, and is a pointer. They don't look like pointers, but regular variables, but they're secretly just pointers under the hood. They can be set to null, for example.


The *biggest* difference between Java and C++ though are in the standard libraries. Instead of cout, we use System.out.println(). Instead of a vector, we use ArrayList. Instead of using the mt19937 nonsense in C++, we call Math.random(). Instead of using doing networking with sockets from the ancient C library (because C++ has no networking in the standard library), we use the Socket and ServerSocket classes. And so forth.


Basically, to do Java programming, coming from C++, you just google, "How do I do X in Java" and have at it. Takes a few minutes to get used to a slightly different workflow, but it's worth the investment.



Oh, and in C++ we talked about [[deprecated]] and [[nodiscard]] which are nice things to know.







Tags:
java
c++
nodiscard
deprecated
javadoc