When comparing Java vs C++, the Slant community recommends Java for most people. In the question“What is the best programming language to learn first?” Java is ranked 22nd while C++ is ranked 25th. The most important reason people chose Java is:
Because Java is statically typed, integrated development environments (IDEs) for Java can provide a lot more feedback on errors you will encounter. Java IDEs can give you specific errors in the location where they occur without having to run the code every time. This makes is faster to debug and learn from your mistakes. IDEs also have extensive auto complete capabilities that can help you learn the programming libraries you are using faster and tell you what functions are available.
Ranked in these QuestionsQuestion Ranking
Pro Fantastic IDEs
Because Java is statically typed, integrated development environments (IDEs) for Java can provide a lot more feedback on errors you will encounter. Java IDEs can give you specific errors in the location where they occur without having to run the code every time. This makes is faster to debug and learn from your mistakes.
IDEs also have extensive auto complete capabilities that can help you learn the programming libraries you are using faster and tell you what functions are available.
Pro Most commonly used language in industry
Pro Consistent programming standards
Most Java code follows very standardized coding styles. This means that when you're starting out, there are fewer questions about how you should implement something as the programming styles and patterns are well established and consistent. This consistent style means that it's often easier to follow others' example code, and that it's more likely to meet at least a certain minimum standard of quality. This discipline with consistent stylistic standards also becomes useful later, when collaborating on projects with larger teams.
Pro Best introduction to "C style" languages
The Java syntax is very similar to other C style languages. Learning the fundamentals of Java will port over well to other languages so you can apply what you've leaned to other languages afterwards.
Pro Introduces you to object oriented languages
Object Oriented Programming (OOP) is a paradigm that teaches you to split your problem into simpler modules with few connections between them; it's the most common paradigm used in industry. Java is the best choice as an introduction to object oriented languages because, as a statically-typed OOP-only language, it very clearly highlights core OOP principles such as encapsulation, access control, data abstraction, and inheritance.
While a scripting language provides more flexibility and terseness, learning a scripting language first would not instill these fundamental concepts as well, as they tend to obscure details such as how types work, and are less encouraging of an object oriented style.
Pro Platform Independent
Because of the Java Virtual Machine, the Java programming language is supported wherever a JVM is installed.
Pro Massive amount of libraries and APIs
Java has been around for such a long time that there have been tens of thousands of APIs and libraries written for almost anything you want to do.
Pro Huge language supports most everything
C++ is a large language with an even larger community and following. It has libraries for every kind of task that is possible to do with C++
Pro Powerful memory management
Allows puting large arrays on the "heap" to avoid "stack overflow".
Pro Teaches fundamental OOP
Teaches you to leverage object oriented programming.
Pro Teaches problem solving
The great STL is the most powerful Data Structure and Algorithms Library. It would benefit you very much in problem solving, your main main way to love programming. The code is much compact compared to Java and C#. No unnecessary classes are in your way; yet when you need classes they are available unlike C. The code runs very fast.
Pro Excellent compiler optimization
Both open source compilers (such as Clang and GCC), and proprietary ones (like Intel's and Microsoft's) are very good at analyzing program flow and program optimization. This is mostly due to the widespread usage of C/C++ applications running everything from mobile/desktop/server Operating Systems, to search engines and webserver software, and the demand for performance.
Pro C code can be used in C++ code
Most C code will work as C++
Pro STD is often updated
The functionalities keep growing throughout the years. C++11 gave us a soft type of garbage collecting with the smart pointers.
Pro Best way to understand algorithms
Con Worst-of-both-worlds static type system
It's just barely good enough to make decent IDEs, but it's not at the level of Idris or even Haskell. For large enterprise projects, the IDE support is important, but the static typing in Java just gets in the way for the smaller projects beginners would start with.
Python is duck typed and this makes small programs easy to develop quickly, but the price is that you have to write unit tests to avoid breaking larger programs. In contrast, you can be reasonably certain that a program that actually compiles in Idris does what you want, because assertions are built into the powerful type system. Java can't make that claim and still requires unit tests. Java has the worst of both worlds because of its poor static type system.
Con Lacks modern features
Java evolves very slowly - lambda expressions weren't available until Java 8 (which is not available on Android), and despite getters/setters being a long-time convention, the language still doesn't have native accessor syntax (a la C#'s properties). It's unlikely newer, popular features like list comprehensions or disjoint union types will be available anytime soon. While not strictly required for novice programmers, these make problems more complicated and tedious than they need to be - for example, when a simple local function would do, (portable) Java demands anonymous inner classes, an interface and a class, or worse, no abstraction at all.
Con Confusing mis-features
Some features in Java can be quite confusing for beginners.
Encapsulation is needlessly obfuscated with a confusing access control model. As an example, the "protected" keyword not only grants access to child classes, but to the entire package. Since small programs are written as one package, it becomes functionally equivalent to "public".
In OOP, everything is supposed to be an object, but, in Java, primitive types such as integers, booleans and characters are not, and must be handled as special cases.
Java continues to lack many high-level features, and, particularly prior to Java 7, compensated by adding confusing Java-only features, such as anonymous subclasses. Some example code is unreadable without knowing a special-case feature, libraries differ in style based on when they were released or what platform they target(e.g., Android vs. Desktop), and some solutions just aren't available on some platforms.
Con Too verbose
- A Hello world needs package, class, static method and the actual
- Reading a line from input requires instatiating 5 objects in the right order.
- Exceptions are everywhere, particularly since all values are nullable.
- Java has a getter/setter culture, but without native syntax support.
- portable Java code lacks anonymous functions, and continues to lack good support for partial application, compensating instead with verbose design patterns, kludges like anonymous inner classes, or just inline code.
- It is statically typed without type inference, with a culture that promotes long class names.
- Poor support for sum-types and pattern matching leads to overuse of inheritance for dynamic dispatch and chains of nested conditionals
Especially for beginners, this can make reading Java code feel overwhelming; most Java courses tell students to simply copy, paste, and ignore a significant percentage of the code until they've learned enough to understand what it means.
For experienced programmers, this makes Java feel tedious, especially without an IDE, and actively discourages some solutions and some forms of abstraction.
Con Locks you into the static OOP mindset
Con Half-baked generics
Type erasure means it doesn't even exist at runtime. The whole generics system is confusing for beginners.
Con Garbage collection may teach bad habits
Java is a garbage collected language and it does not force programmers to think about memory allocation and management for their programs. This is fine most of the time. However, it may cause some difficulties in adjusting to a non-GC language (such as C for example), where memory management needs to be done manually. But if good coding practices and habits are followed, this shouldn't be much of a problem.
Con Enforces some misguided principles
Java utilizes principles that organize code into "classes" as the central concept, instead of more familiar organizational methods.
Con Long learning curve
Con Static typing but no type inference
The type system gets in your way more than it helps. Heavy IDE support is absolutely required for reasonable productivity. This means beginners have to learn not just the language, but eventually a complex, heavyweight IDE too.
Con Checked exceptions
Catch blocks can be a frequent source of mistakes.
Con Huge language gets in the way of learning
C++ is such an atrociously over-complicated language that its learning curve may get in the way of learning fundamentals. Learning C++ well is a ten-year project, and even experts are frequently surprised by the language.
Con Module system is not great
C++ uses the
#include mechanism provided by C. Which unfortunately is a poor way of accessing the API of a library. Some of the reasons why the module system is weak are:
Compile time scalability: The compiler must preprocess every header included in a file, and every header included in those headers. This process must be repeated for every translation unit in the program. As can be imagined, this doesn't scale very well. For each header added you are increasing the compilation time exponentially.
Fragile: modules included are treated as textual imports by the compiler. This causes all sorts of problems since they are subject to any macro definitions in the time of the inclusion. If any of these macro definitions collide with a name in the library it can break the library API .
Con Undefined behavior
Subtle errors can render the entire program "undefined" by the complicated C++ standard. The standard imposes no requirements in such cases. Thus C++ compiler writers are free to ignore the existence of such cases and Bad Things are prone to happen instead. Even experts can't reliably avoid undefined cases in C++, so how can beginners be expected to do so?
Con Retains nearly all bad habits of C
Con No two programmers can agree on which 10% subset of C++ to use
C++ is such a huge and complicated language, that programmers have to learn a disciplined subset of it to reliably get anything done. The problem is, no-one can agree on which subset to use and they can't understand each other.
Con Bugs easily corrupt the memory you need to find them
You can usually get a core dump, but often the call stack gets completely overwritten. Compilers are not even consistent in how they map the binary objects to code.
Con No reflection
C++ objects are frustratingly opaque. This makes debugging especially difficult, something beginners have to do a lot.
Con Complicated types
Con Painfully slow compilation
Beginners need fast feedback
Con Standard library missing important features
Con Arcane binding rules
Con No way to locate definitions
No modules, just files, and no way to tell where anything came from.
Con Duplicates C features in incompatible ways
Arrays, strings, pointers, etc. have both C and C++ versions. Sometimes the C++ versions are worse. This is more useless trivia beginners have to sort through.