Home >Web Front-end >JS Tutorial >What is the difference between Java and JavaScript

What is the difference between Java and JavaScript

清浅
清浅Original
2018-11-22 09:48:003374browse

JavaScript is a scripting language, and Java is an object-oriented language. They are very different in terms of execution environment, closures, OOP, and operational issues.

For our learners, we often confuse Java and JavaScript, thinking that they are related. In fact, they are two languages ​​that have no relationship at all. Next, we will share some information about the relationship between them. difference.

JavaScript is an HTML-based client-side scripting language mainly used for interactive web pages, while Java is a programming language developed by Sun Microsystems and is an object-oriented language. There are many differences in their writing, compilation and execution methods, and there are also great differences in functionality

The difference between Java and JavaScript

(1) On the execution environment

Java is a compiled and interpreted language. Java code is compiled into a class file containing byte code rather than executed by the JVM. This indicates that Java runs in the JVM and A JDK or JRE is required to run, while JavaScript code is executed directly by the browser, and almost every modern browser supports JavaScript.

(2) Static and dynamically typed languages ​​

JavaScript is a dynamically typed language, while Java is a statically typed language. This means that variables are declared with a type at compile time and can only accept values ​​allowed by that type, other manual variables are declared in JavaScript using the vary keyword and can accept values ​​of different types such as String, numble, etc.

(3) Closure

JavaScript supports closures in the form of anonymous functions. Simply put, you can pass a function as a parameter to another function. Java can only simulate closures by using anonymous classes. Java 8 brings true closure support to Java in the form of lambda expressions, which makes things even easier.

(4) OOP (Object-Oriented Programming)

Java is an object-oriented programming language. Although JavaScript also supports classes and objects, it is more like An object-oriented scripting language. It's much easier to code large enterprise applications in Java. Java provides packages that group related classes together, providing better deployment control using JARs, WARs, and EARs.

(5) Running issues

Java uses byte code to achieve platform independence. JavaScript runs directly on the browser, but code written in JavaScript is subject to browser compatibility. The impact of the problem is that a program that displays normally in Firefox may not run in Internet Explorer. Later, because jQuery (a JS library for JavaScript) can help web developers solve this browser compatibility issue

(6) Constructor

Java has the concept of a constructor, which has some special properties , JavaScript constructors just represent another function, there are no special rules like they cannot have a return type, or their name must be the same as the class.

(7) Applicability

JavaScript has its own space, used together with HTML and CSS in web development, while Java is everywhere

Summary: Through the above It can be seen from the content that they are completely different languages, so don't confuse them again in the future.



The above is the detailed content of What is the difference between Java and JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn