Java 简明教程

Java vs C++

Java 是一种通用、高级编程语言。Java 用于 Web 开发、机器学习和其他尖端的软件开发。Java 编程语言最初是由 Sun Microsystems 开发的,由 James Gosling 发起,并于 1995 年作为 Sun Microsystems 的 Java 平台(Java 1.0 [J2SE])的核心组件发布。

Java is a general-purpose, high-level programming language. Java is used for web development, Machine Learning, and other cutting-edge software development. Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform (Java 1.0 [J2SE])

C 是一种中级、区分大小写、面向对象编程语言。Bjarne Stroustrup 在贝尔实验室创建了 C。C 是一种独立于平台的编程语言,可用于 Windows、Mac OS 和 Linux。C 接近硬件,允许低级编程。这为开发者提供了对内存的控制、提高的性能和可靠的软件。

C is a middle-level, case-sensitive, object-oriented programming language. Bjarne Stroustrup created C at Bell Labs. C is a platform-independent programming language that works on Windows, Mac OS, and Linux. C is near to hardware, allowing low-level programming. This provides a developer control over memory, improved performance, and dependable software.

请阅读本文,以了解 C++ 和 Java 的概述,以及这两种编程语言之间的不同之处。

Read through this article to get an overview of C++ and Java and how these two programming languages are different from each other.

What is Java?

Java Standard Edition 的最新版本是 Java SE 21。随着 Java 的进步及其广泛普及,已构建多种配置以适应各种类型的平台。例如:J2EE 用于企业应用程序,J2ME 用于移动应用程序。

The latest release of the Java Standard Edition is Java SE 21. With the advancement of Java and its widespread popularity, multiple configurations were built to suit various types of platforms. For example: J2EE for Enterprise Applications, J2ME for Mobile Applications.

新 J2 版本分别更名为 Java SE、Java EE 和 Java ME。Java 有保证 Write Once, Run Anywhere.

The new J2 versions were renamed as Java SE, Java EE, and Java ME respectively. Java is guaranteed to be Write Once, Run Anywhere.

Features of Java

Java 是 −

Java is −

  1. Object Oriented − In Java, everything is an Object. Java can be easily extended since it is based on the Object model.

  2. Platform Independent − Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by the Java Virtual Machine (JVM) on whichever platform it is being run on.

  3. Simple − Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.

  4. Secure − With Java’s secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.

  5. Architecture-neutralJava compiler generates an architecture-neutral object file format, which makes the compiled code executable on many processors, with the presence of Java runtime system.

  6. Portable − Being architecture-neutral and having no implementation dependent aspects of the specification makes Java portable. Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.

  7. Robust − Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error checking and runtime checking.

  8. Multithreaded − With Java’s multithreaded feature it is possible to write programs that can perform many tasks simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly.

  9. Interpreted − Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light-weight process.

  10. High Performance − With the use of Just-In-Time compilers, Java enables high performance.

  11. Distributed − Java is designed for the distributed environment of the internet.

  12. Dynamic − Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.

Java Example

看看以下简单的 Java 程序 −

Take a look at the following simple Java program −

package com.tutorialspoint;

import java.util.Scanner;

public class JavaTester {

   public static void main(String args[]) {
      String a, b;
      Scanner scanner = new Scanner(System.in);
      System.out.println("Enter The value for variable a");
      a = scanner.nextLine();
      System.out.println("Enter The value for variable b");
      b = scanner.nextLine();

      System.out.println("The value you have entered for a is " + a);
      System.out.println("The value you have entered for b is " + b);
      scanner.close();
   }
}

在我们的示例中,我们取两个变量“a”和“b”并为这些变量分配一些值。请注意,在 Java 中,我们需要显式为变量声明数据类型,因为 Java 是强类型语言。由于 Java 是一种面向对象的语言,因此我们使用对象来执行任何操作。在该示例中,我们使用了 Scanner 类对象从由 System.in 对象表示的控制台中读取用户输入。System.out 对象方法 println() 用于打印接收到的值。

In our example, we have taken two variables "a" and "b" and assigning some value to those variables. Note that in Java, we need to declare datatype for variables explicitly, as Java is strictly typed language. As Java is an object oriented language, we uses objects to perform any action. In the example, we’ve used Scanner class object to read user input from console which is represented by System.in object. System.out object method println() is used to print the values received.

执行后,此 Java 代码将生成以下输出 −

On execution, this Java code will produce the following output −

Enter The value for variable a
10
Enter The value for variable b
20
The value you have entered for a is 10
The value you have entered for b is 20

What is C++?

C 是一种静态类型、已编译、多范式、通用编程语言,具有陡峭的学习曲线。视频游戏、桌面应用程序和嵌入式系统广泛使用它。C 与 C 非常兼容,它几乎可以构建所有 C 源代码,而无需进行任何更改。面向对象编程使 C++ 成为比 C 结构更好、更安全的语言。

C is a statically typed, compiled, multi-paradigm, general-purpose programming language with a steep learning curve. Video games, desktop apps, and embedded systems use it extensively. C is so compatible with C that it can build practically all C source code without any changes. Object-oriented programming makes C++ a better-structured and safer language than C.

Features of C++

让我们看看 C++ 的一些特性及其受欢迎的原因。

Let’s see some features of C++ and the reason of its popularity.

  1. Middle-level language − It’s a middle-level language since it can be used for both systems development and large-scale consumer applications like Media Players, Photoshop, Game Engines, etc.

  2. Execution Speed − C++ code runs quickly. Because it’s compiled and uses procedures extensively. Garbage collection, dynamic typing, and other modern features impede program execution.

  3. Object-oriented language − Object-oriented programming is flexible and manageable. Large apps are possible. Growing code makes procedural code harder to handle. C++'s key advantage over C.

  4. Extensive Library Support − C++ has a vast library. Third-party libraries are supported for fast development.

C++ Example

让我们通过下面编写的示例了解 C++ 的语法。

Let’s understand the syntax of C++ through an example written below.

#include
using namespace std;

int main() {
   int a, b;
   cout << "Enter The value for variable a \n";
   cin >> a;
   cout << "Enter The value for variable b";
   cin >> b;
   cout << "The value of a is "<< a << "and" << b;
   return 0;
}

在我们的示例中,我们通过键盘从用户那里获取两个变量“a”和“b”的输入,并在控制台上显示数据。

In our example, we are taking input for two variables "a" and "b" from the user through the keyboard and displaying the data on the console.

执行后,它将生成以下 output

On execution, it will produce the following output

Enter The value for variable a
10
Enter The value for variable b
20
The value of a is 10 and 20

Difference Between Java and C++

Java 和 C++ 都是最流行的编程语言。它们都有各自的优点和缺点。在本教程中,我们将仔细了解它们的特征,这些特征使它们彼此区分开来。

Both Java and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this tutorial, we shall take a closure look at their characteristic features which differentiate one from another.

Sr.No.

Criteria

Java

C++

1

Developed by

Java was developed by James Gosling at Sun Microsystems. Initially it was designed for embedded systems, settop boxes, televisions etc. Later it become a preferred language for internet based application development

C was developed by Bjarne Stroustrup at Bell Labs, as an extension to C language. It supports both high level as well as low level machine code access. C is mainly used to develop system softwares, compilers etc.

2

Influenced by

It was influenced by Ada 83, Pascal, C++, C#.

It was influenced by Ada, ALGOL 68, C, ML, Simula, Smalltalk.

3

Dependency on Architecture

The Java bytecode works on any operating System. Bytecode is targeted for JVM. JVM or Java Virtual Machine then interpret the byte code and run the underlying machine specific code. Thus Java code needs not to be changed to run on different machines.

It doesn’t work on every operating system since libraries are different on different systems.

4

Platform independence

It can run on any OS. Java code is platform independent. No platform specific code is required. Size of int, long remains same on all platforms.

It is compiled differently on different platforms, can’t be run on any OS.

5

Portablity

It is portable. Being platform independent, a java code can be transferred as it is on any machine without any plaform specific modification. A java code written in Windows machine can run in Unix machine in same fashion without any modification.

It isn’t portable.

6

Interpreted/Compiled

It is an interpreted language.

It is a compiled language.

7

Memory management

Memory management is done automatically. Java provides Garbage Collector service which automatically deallocates memory once an object is not required.

Memory management is to be done manually.

8

virtual Keyword

It doesn’t have ‘virtual' keyword.

It has the ‘virtual' keyword.

9

Multiple Inheritance support

It supports single inheritance only. Multiple inheritance can be achieved using interfaces (partial only). A class can extend only a single class. Although interface can extend multiple inheritance. Multiple inheritance can lead to ambigous results. As virtual keyword is not supported in Java, multiple inhertance is not supported.

It supports single and multiple Inheritance. Using virtual keyword, ambigous reference can be resolved.

10

operator overloading support

It doesn’t support operator overloading. Java supports only method overloading. Operator overloading is considered to add the complexity to base language so is not implemented to keep language simple.

It supports operator overloading. In C++, we can overload both methods and operators.

11

pointers

It provides limited support to pointers. Pointers being a complex functionality, Java refrains from using them. It provides concept of reference to point to objects or precisely their addresses.

It supports pointer operations. Developers can perform complex operations, can write optimized memory based code using pointers. But it is quite complex and requires strong programming skills to master them.

12

Low level machine code access

They have high level functionalities. Java is platform independent language and the compiled code of java as byte code is for JVM which further converts code to low level code. So using java, developer cannot write low level machine code. This is the reason, that Java is mainly used for application development.

They have low level functionalities. As C++ supports low level machine code code. It is mainly used to write system softwares, compilers etc.

13

Native libraries access

It doesn’t support direct native library call. Java is not designed to work with low level machine code and it is not supporting native call. But we can configure native call using third party libraries.

It supports direct system library calls.

14

documentation comment

It supports documentation comment (/**.. */) for source code. Javadoc tool can read the documentation comments from source code and generate html based java documentation based on the comments.

It doesn’t support documentation comment for source code.

15

Multithreading

It supports thread operations. Java has by default support for multithreading. It allows concurrent programming to improve efficiency and to reduce time taken

It doesn’t support threads by design. It can be done by using third party threading libraries.

16

Console Input

It uses the 'System' class, i.e System.in for input. System.in class can be used to take input from user on console.

It uses 'cin' for input operation. cin allows user to enter value in console.

17

Console Output

It uses System.out for output. System.out.println() method prints the required value on system’s console.

It uses 'cout' for an output operation. cout prints the required value on system’s console.

19

global support

It doesn’t support global scope. Java is a strict object oriented language and global scope is not available. Using packages, it supports across package scope though.

It supports global scope as well as namespace scope.

20

struct/union support

It doesn’t support structures and unions.

It supports structures and unions.

21

goto keyword

It doesn’t have the 'goto' keyword. But same functionality is achivable using label. A break/continue statement can jump to a labelled statement location.

It supports the 'goto' keyword. Using goto keyword, we can jump to any labelled location.

22

pass by value/reference

It supports Pass by Value method only. Even object reference is technically passed to a method as value.

It supports Pass by Value and pass by reference methods. In case of pass by reference, pointer or & notation is required.

23

Memory Management

It performs object management automatically using garbage collector. Developers are not required to do memory allocation for objects or deallocate them when objects are not in use. Garbage Collector service automatically detects and frees up the space. Due to GC service, memory leaks are very less possible in Java.

It performs object management manually with the help of ‘new' and ‘delete'. Developers have to take measures to ensure memory is properly allocated/deallocated to prevent memory leaks.