What's the Difference Between JavaScript and Java

What's the difference between JavaScript and Java?

Java and Javascript are similar like Car and Carpet are similar.

What is the difference between operator in Java and JavaScript?

Both are the logical right shift, but JavaScript has some weirdness in how it handles numbers. Normally numbers in JavaScript are floats, but the bitwise operations convert them to unsigned 32 bit integers. So even though the value looks like it shouldn't change, it converts the number to a 32 bit unsigned integer.

The value that you see 4294843840 is just the same bits as -123456, but interpreted as unsigned instead of signed.

Difference between java and JavaScript for loop

In the JavaScript, you're setting the entire contents of the demo1 container to the new string. At the end of the iterations, the element only contains the final value in the string.

In contrast, in the Java, you're logging and looking at the string on every iteration, instead of only the final value.

If you append new HTML markup in the JavaScript instead of overwriting what's previously in the container, you'll see the same output:

let str = "XYZ ";
for (let i = 0; i < 5; i++) {
str += i;
demo1.innerHTML += str + '<br>';
}
<pre id="demo1"></pre>

Difference between classes in Java vs JavaScript?

Unlike some languages like Java where we can reference variables within a class without using this, this binding behaves much differently in JavaScript, as does scoping.

In Java, this is legal:

class Test {
int i;
void method() {
System.out.print(i); // <-- Java compiler knows to look for the `i` on the class instance (technically it first looks for `i` locally in the scope of `method` and the goes to look on the class)
}
}

In JavaScript, we don't have this behavior, there's no such thing as a class variable (yet).

class Test {  i; // <-- Uncaught SyntaxError: Unexpected token ;  method() {    console.log(i);  }}

What is the different of bitwise operation between javascript and java

In javascript, you can perform bitwise operations on 32-bit numbers only. Even javascript use IEEE 754 floating-point standard to represent the number. Bitwise operation can be performed on 32-bit number. Hence rest of the bits will get ignored.

To convert any number to its 32-bit representation, number | 0 with it. When javascript reads x & y it considers 32-bits of x and y and ignores other bits silently. In your case, the numbers on which you are performing bitwise are x = -271733879 and y = -1732584194.

The safe range for bitwise operations in Javascript is Math.pow(-2,31) (-2147483648, about -2.1 billion) to Math.pow(2,32)-1 (2147483647, about +2.1 billion).

x= 4023233417;console.log(x.toString(2))y = 2562383102 ;console.log(y.toString(2));
result = x & y;
console.log(result.toString(2)); //-1110111011101110111011101111000
x = x | 0; //-271733879y = y | 0; //-1732584194console.log("32 bit x: " + x);console.log("32 bit y: " + y);result = x & y;
console.log(result.toString(2)); //-1110111011101110111011101111000

difference in calculation results between Java and Javascript

The code is not quite equivalent between Javascript and Java.

In Javascript, all numbers are doubles, so floating-point math will take place. However, in Java, integer division will take place, so that for example 1/4 will yield 0, not 0.25.

As written, in Java I get 0.6856620239020387.

Changing the Java numbers to use double literals:

Double mu = M / (a * (1.0 - esq * (1.0 / 4.0 + esq * (3.0 / 64.0 + 5.0 * esq / 256.0))));

Now, I get the other number: 0.6868129133457879, the value I get in Javascript.

What's the difference between a JavaScript object and an OO/UML/Java object?

JavaScript objects are different from classical OO/UML (C++/Java/C# etc.) objects. In particular, they need not instantiate a class. And they can have their own (instance-level) methods in the form of method slots, so they do not only have (ordinary) property slots, but also method slots. In addition they may also have key-value slots. So, they may have three different kinds of slots, while classical objects (called "instance specifications" in UML) only have property slots.

JavaScript objects can be used in many different ways for different purposes. Here are five different use cases for, or possible meanings of, JavaScript objects:

  1. A record is a set of property slots like, for instance,

    var myRecord = { firstName:"Tom", lastName:"Smith", age:26}
  2. An associative array (or 'hash map') is a set of key-value slots. It supports look-ups of values based on keys like, for instance,

    var numeral2number = { "one":"1", "two":"2", "three":"3"}

    which associates the value "1" with the key "one", "2" with "two", etc. A key need not be a valid JavaScript identifier, but can be any kind of string (e.g. it may contain blank spaces).

  3. An untyped object does not instantiate a class. It may have property slots and method slots like, for instance,

    var person1 = {  
    lastName: "Smith",
    firstName: "Tom",
    getInitials: function () {
    return this.firstName.charAt(0) + this.lastName.charAt(0);
    }
    };
  4. A namespace may be defined in the form of an untyped object referenced by a global object variable, the name of which represents a namespace prefix. For instance, the following object variable provides the main namespace of an application based on the Model-View-Controller (MVC) architecture paradigm where we have three subnamespaces corresponding to the three parts of an MVC application:

    var myApp = { model:{}, view:{}, ctrl:{} };
  5. A typed object o that instantiates a class defined by a JavaScript constructor function C is created with the expression

    var o = new C(...)

    The type/class of such a typed object can be retrieved with the introspective expression

    o.constructor.name  // returns "C"

See my JavaScript Sumary for more on JavaScript objects.

Difference between javascript getTime() and Java getTime()

Using nashorn, you can see when they deviate:

> function printDate(x) { System.out.println(new Date(x) + " - " + new java.util.Date(x)); }

> printDate(-12219292800000)
Fri Oct 15 1582 00:00:00 GMT+0000 (GMT) - Fri Oct 15 00:00:00 GMT 1582

> printDate(-12219292800001)
Thu Oct 14 1582 23:59:59 GMT+0000 (GMT) - Thu Oct 04 23:59:59 GMT 1582

This corresponds to a switch from the Julian to Gregorian calendar.

Java's date library takes this change into account, but JavaScript's doesn't.

Why is JavaScript called JavaScript, since it has nothing to do with Java?

JavaScript was originally named Mocha, later it was renamed to LiveScript, and then to JavaScript.

The LiveScript to JavaScript name change came because Netscape and Sun did a license agreement.

The language was then submitted for standarization to the ECMA International Organization. By that time, Netscape didn't allow the use of the "JavaScript" name, so the standarized language is named ECMAScript.

JavaScript isn't actually an open name. It is currently a trademark of Oracle (formerly Sun).

There still a lot of confusion, some people still think that JavaScript, JScript, and ECMAScript are three different languages.

ECMAScript is the "standards" name for the language.

JavaScript is technically a "dialect" of ECMAScript, the Mozilla Foundation can use "JavaScript" as the name of their implementations (currently present on the Rhino and SpiderMonkey engines).

In the early days, Microsoft decided also to do what Netscape was doing on their own browser, and they developed JScript, which is also an ECMAScript dialect, but was named in this way to avoid trademark issues.



Related Topics



Leave a reply



Submit