JavaRush /Java Blog /Random EN /Coffee break #118. What is the difference between C++ and...

Coffee break #118. What is the difference between C++ and Java languages. Private constructors in Java

Published in the Random EN group

What is the difference between C++ and Java

Source: Hackernoon The programming languages ​​C++ and Java are among the most popular today. Let's discuss the main differences between C++ and Java. Coffee break #118.  What is the difference between C++ and Java languages.  Private Constructors in Java - 1Java is a general purpose programming language. It belongs to object-oriented languages. The creators of Java wanted to implement the WORA principle - “Write Once, Run Anywhere”. This means that when you develop an application written in Java, it can run on any platform as long as it has the Java Runtime Environment (JRE) installed on it. The program runs by compiling code written in Java into bytecode. This format is executed using the JVM, that is, the Java Virtual Machine. Accordingly, the JVM is part of the Java Runtime Environment (JRE). The virtual machine is platform independent. Java also has a memory management mechanism called Garbage Collector (GC). The developer creates objects, and the JRE uses the garbage collector to clean up memory when the objects stop. The syntax of the Java language is similar to that of other C-like languages. Here are some of its features:
  • Case Sensitivity - User and user ID in Java are different entities.

  • Method name . If the method name consists of one word, it must begin with a lowercase letter. Example: firstMethodName() .

  • UpperCamelCase is used to name classes. If the name consists of one word, it must begin with a capital letter. Example: FirstClassName .

  • The name of the program files must exactly match the name of the class, taking into account case sensitivity. For example, if the class is called FirstClassName , the file should be called FirstClassName.java .

  • Identifiers always begin with a letter (AZ, az), $, or an underscore _.

What is the C++ language?

C++ is a compiled, strongly typed, general-purpose programming language. It supports different programming paradigms: procedural, generic, functional; Most of the attention is paid to supporting object-oriented programming. Essentially, C++ is a set of commands that tell the computer what to do. This set of instructions is usually called source code or simply code. Commands are either “functions” or “keywords”. Keywords (C/C++ reserved words) are the basic building blocks of the language. Functions are complex building blocks because they are written in terms of simpler functions. The structure of C++ and Java functions resembles the contents of the book. The table of contents can display chapters of a book; each chapter in a book can have its own table of contents, consisting of paragraphs; Each paragraph can have its own subparagraphs. Although C++ provides many common functions and reserved words that you can use, the language still requires you to write your own functions.

Key differences between C++ and Java

Both Java and C++ are object-oriented programming languages, but that's where their similarities end. There are more differences between them. Of course, there is a critical difference between C++ and Java when it comes to choosing one of the languages ​​for developing a project or any other specific case. Here are the criteria to pay attention to.

Libraries

Java or C++? Which library is bigger or better? Java has many more libraries. But how do you know which ones to use and which ones not to? Here is a brief description of some of them:
  • lang, util, nio, io, net.
  • Java also has libraries for working with GUI frameworks: javax.swing (a rather outdated extension of the even older java.awt).
  • You can even play music and create MIDI files using java.sound etc.
The C++ language has the C Standard Library and the C++ Standard Library. Developers should first make sure they understand the difference between C and C++. In addition, they must study the libraries that they are going to use in a particular project. Java has built-in support for documentation comments. C++ does not support documentation comments. You can also use a convenient online Java compiler.

Input mechanism

Java is primarily interpreted, so it is platform independent. C++ generates object code; the same code cannot run on different platforms.

Thread support

Java has built-in support for threads. Java has thread classes that are inherited to create a new thread and override the start method. Please note that C++ does not have native threading support. For these purposes, non-standardized third-party libraries are used.

Supporting method

Java has method overloading, but does not have operator overloading. The Stringclass does not use the + and += operators to concatenate strings, and Stringexpressions use automatic conversion types, but this is a special built-in case. C++ supports both method overloading and operator overloading.

Memory management

Java supports automatic heap deallocation control, but does not support destructors. In C++, support for destructors is automatically enabled when an object is destroyed.

Multiple inheritance

Java doesn't provide multiple inheritance, at least not in the same way as C++. Multiple inheritance is a feature of C++ in which a class can inherit from more than one class.

Conclusion

That's the whole difference between Java and C++. The Java language is ideal for commercial projects. It is in great demand in large companies, since maintaining and refactoring existing projects will always remain relevant - at least in banks and other financial institutions. You can handle cross-platform development quite well and use it to create a system for any platform. C++ has other advantages over Java, however, such as support for both method overloading and operator overloading, support for destructors, etc.

Private constructors in Java

Source: Dev.to A constructor in Java is a special type of method that has the same name as the class name or file name. When we call an object, the internal constructor is always called. It is mainly used to initialize the state of an object or the values ​​of variables in a class. Coffee break #118.  What is the difference between C++ and Java languages.  Private Constructors in Java - 2Now let's see what private constructors are. Java allows us to change methods depending on our usage. So, can we make the constructor private like other methods? And if so, then what is the use of a private constructor? Answer: Yes, we can declare a constructor in Java as a private constructor using the private access specifier. But remember that if we declare the constructor private, then we will not be able to create an object of the class. Instead, we can use this private constructor in the Singleton design pattern. Rules for private constructors:
  • It doesn't allow a class to be a subclass.
  • Does not allow you to create an object outside the class.
  • If a class has a private constructor, then when we try to extend the class, a compile-time error occurs.
  • We cannot access a private constructor from any other class.
  • If our class has all constant methods, we can use a private constructor.
  • If all methods are static, we can use a private constructor.
  • We can use a public function to call a private constructor if the object is not initialized.
  • We can only return an instance of this object if the object is already initialized.
public class PrivateConstructorDemo
{
     //creating an instance variable of the class Tester
     private static PrivateConstructorDemo pcd;
     //creating a private constructor
     private PrivateConstructorDemo()
     {
     }
    //creating a static method named getInstance()
    public static PrivateConstructorDemo getInstance()
    {
       if(pcd == null)
       {
        //creating a constructor of the class
        pcd = new PrivateConstructorDemo();
       }
     return pcd;
    }
    //main() method
    public static void main(String args[])
    {
     PrivateConstructorDemo pcd = PrivateConstructorDemo.getInstance();
    PrivateConstructorDemo pcd1 = PrivateConstructorDemo.getInstance();
    //invokes the getInstance() method and prints the corresponding result
    System.out.println(pcd.equals(pcd1));
   }
}
Conclusion:
true
Now let's look at the use cases for private constructors. The main purpose of using a private constructor is to restrict the creation of objects. Private constructors are also used to implement the Singleton design pattern. The options for using a private constructor are as follows:
  • It can be used with static members-only classes.
  • It can be used with static utility or constant classes.
  • It can also be used to create singleton classes.
  • It can be used for naming, such as creation using factory methods.
  • It is also used to avoid subclassing.
  • It includes factory methods.
I hope you learned more about Java constructors through this post.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION