77

What's the difference between JavaScript and Java?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Guy
  • 59,547
  • 93
  • 241
  • 306

15 Answers15

573

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

Greg Hewgill
  • 828,234
  • 170
  • 1,097
  • 1,237
408

One is essentially a toy, designed for writing small pieces of code, and traditionally used and abused by inexperienced programmers.

The other is a scripting language for web browsers.

Shog9
  • 146,212
  • 34
  • 221
  • 231
  • 2
    If we could do categorical votes, I'd vote +1 funny, but otherwise, meh. – Don Wakefield Oct 28 '08 at 22:16
  • 19
    I was about to berate you for dissing Javascript when it had come so far in the last few years. Then I rid the second paragraph and L'dOL. But, hang on a mo, Java is my main income earner... :-) – paxdiablo Oct 28 '08 at 22:55
  • 2
    downvoted for dissing Javascript, then noticing you were talking about JAVA and upvoted (and lol'ed alot). – Pim Jager Dec 07 '08 at 12:05
  • 12
    It's sad that I can only upvote once. This is going on my Facebook profile somewhere... – Samantha Branham Feb 10 '09 at 05:37
  • 1
    But which language goes with which description(wasn't java meant to be used as a well not quite scripting but everything else for the net during some point in the 90s-by sun and netscape)? Also who writes small pieces of code in java? – Roman A. Taycher Apr 21 '10 at 11:30
  • 3
    @Roman well of course, the proper Java way is to write at least 20 different classes using intricate inheritance and other cool OOP features to complete a FizzBuzz :) – Earlz Aug 20 '10 at 04:17
  • 1
    Actually, javascript isn't just for browsers. It's a complete scripting language often used for many applications that requires scripts. And Java isn't just for inexperienced or idiots. It can acomplish many things within no time. – SHiRKiT Oct 09 '11 at 14:45
188

Here are some differences between the two languages:

  • Java is a statically typed language; JavaScript is dynamic.
  • Java is class-based; JavaScript is prototype-based.
  • Java constructors are special functions that can only be called at object creation; JavaScript "constructors" are just standard functions.
  • Java requires all non-block statements to end with a semicolon; JavaScript inserts semicolons at the ends of certain lines.
  • Java uses block-based scoping; JavaScript uses function-based scoping.
  • Java has an implicit this scope for non-static methods, and implicit class scope; JavaScript has implicit global scope.

Here are some features that I think are particular strengths of JavaScript:

  • JavaScript supports closures; Java can simulate sort-of "closures" using anonymous classes. (Real closures may be supported in a future version of Java.)
  • All JavaScript functions are variadic; Java functions are only variadic if explicitly marked.
  • JavaScript prototypes can be redefined at runtime, and has immediate effect for all referring objects. Java classes cannot be redefined in a way that affects any existing object instances.
  • JavaScript allows methods in an object to be redefined independently of its prototype (think eigenclasses in Ruby, but on steroids); methods in a Java object are tied to its class, and cannot be redefined at runtime.
Chris Jester-Young
  • 206,112
  • 44
  • 370
  • 418
  • 8
    2 years late, but thank you so much for this thoughtful explanation. It's way more useful and informative than the selected answer, and deserves far more votes IMO – Eric Hu Mar 31 '11 at 21:29
  • 2
    I upvote both in such cases. We need the right answer, but holy shmoly, geeks like us we really need more of this humor – Michael Durrant Nov 15 '11 at 12:58
  • You might also here strongly-typed and loosely-typed instead of statically-typed and dynamically-typed. – San Dec 14 '11 at 14:56
51

Take a look at the Wikipedia link

JavaScript, despite the name, is essentially unrelated to the Java programming language, although both have the common C syntax, and JavaScript copies many Java names and naming conventions. The language was originally named "LiveScript" but was renamed in a co-marketing deal between Netscape and Sun, in exchange for Netscape bundling Sun's Java runtime with their then-dominant browser. The key design principles within JavaScript are inherited from the Self and Scheme programming languages.

toolkit
  • 47,529
  • 17
  • 103
  • 134
38

Everything.

JavaScript was named this way by Netscape to confuse the unwary into thinking it had something to do with Java, the buzzword of the day, and it succeeded.

The two languages are entirely distinct.

ddaa
  • 47,639
  • 7
  • 48
  • 56
  • 8
    Javascript was originally called Livescript, and the world would probably be a better place today if they hadn't renamed it! – Greg Hewgill Oct 28 '08 at 22:13
  • 2
    I doubt it was to cause confusion - what's the benefit to the company of that? I suspect the _intent_ was to ride on the success of Java, although the _effect_ was what you stated. – paxdiablo Oct 28 '08 at 22:58
  • "ride the success of java" by calling something unrelated with a similar name? That sounds very much like "causing confusion for marketing purposes" to me. – ddaa Oct 28 '08 at 23:04
  • 2
    Sun tried this naming trick again with the "Sun Java Desktop", which was essentially a branded version of Gnome and had nothing to do with Java. – Dan Dyer Oct 29 '08 at 00:07
  • @Pax - Put another way, it confused the unwary into thinking it had something to do with Java, the buzzword of the day. – Kirk Strauser Oct 29 '08 at 00:47
  • 1
    Yeah, and it worked. @Greg Hewgill, the world might be a better place, but Javascript probably wouldn't be where it is today. – Dan Rosenstark Jan 12 '09 at 05:32
  • @Just Some Guy - Well said. I improved my answer by adding some of your wit. – ddaa Nov 15 '09 at 01:01
  • The reason they changed the name was to in some way pacify Sun who at that point were involved with Netscape – Toby Allen Jul 05 '11 at 19:15
24

enter image description here
Java is to JavaScript as ham is to hamster

NullUserException
  • 77,975
  • 25
  • 199
  • 226
Alpine
  • 3,730
  • 1
  • 23
  • 18
23

JavaScript is an object-oriented scripting language that allows you to create dynamic HTML pages, allowing you to process input data and maintain data, usually within the browser.

Java is a programming language, core set of libraries, and virtual machine platform that allows you to create compiled programs that run on nearly every platform, without distribution of source code in its raw form or recompilation.

While the two have similar names, they are really two completely different programming languages/models/platforms, and are used to solve completely different sets of problems.

Also, this is directly from the Wikipedia Javascript article:

A common misconception is that JavaScript is similar or closely related to Java; this is not so. Both have a C-like syntax, are object-oriented, are typically sandboxed and are widely used in client-side Web applications, but the similarities end there. Java has static typing; JavaScript's typing is dynamic (meaning a variable can hold an object of any type and cannot be restricted). Java is loaded from compiled bytecode; JavaScript is loaded as human-readable code. C is their last common ancestor language.

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
  • 2
    Javascript isn't just for HTML pages, Java6 now includes it, BIRT uses it for report scripting - I'm sure that there are other non-HTML uses beyond these two. – paxdiablo Oct 28 '08 at 22:56
  • You're right. With the development of Rhino, and some of the other developments you mentioned, Javascript has come pretty far out of the browser sandbox. – Bill the Lizard Dec 30 '08 at 14:24
  • 2
    Scripting is simply one particular kind of programming... instead you should say perhaps that Java is a compiled programming language and javascript is a scripting or interpreted programming language. – Blacktiger Aug 23 '10 at 18:38
17

In addittion to being entirely different languages, in my experience:

  • Java looks nice at first, later it gets annoying.
  • JavaScript looks awful and hopeless at first, then gradually you really start to like it.

(But this may just have more to do with my preference of functional programming over OO programming... ;)

Stein G. Strindhaug
  • 5,036
  • 2
  • 26
  • 40
14

Everything. They're unrelated languages.

Kirk Strauser
  • 27,753
  • 5
  • 45
  • 62
  • *Punches in the arm*. According to wikipedia, I am required to do so after you jinxed me and before I can speak again. http://en.wikipedia.org/wiki/Jinx – ddaa Oct 28 '08 at 22:22
  • 2
    You misread that - I get to punch you now. And get me my Coke. – Kirk Strauser Oct 29 '08 at 00:45
8

They are independent languages with unrelated lineages. Brendan Eich created Javascript originally at Netscape. It was initially called Mocha. The choice of Javascript as a name was a nod, if you will, to the then ascendant Java programming language, developed at Sun by Patrick Naughton, James Gosling, et. al.

Don Wakefield
  • 8,315
  • 2
  • 31
  • 53
5

Like everybody's saying, they're pretty much entirely different.

However, if you need a scripting language for your Java application, Javascript is actually a really good choice. There are ways to get Javascript running in the JVM and you can access and manipulate Java classes pretty seamlessly once you do.

Darcy Casselman
  • 2,494
  • 3
  • 24
  • 26
2

They have nothing to do with each other.

Java is statically typed, compiles, runs on its own VM.

Javascript is dynamically typed, interpreted, and runs in a browser. It also has first-class functions and anonymous functions, which Java does not. It has direct access to web-page elements, which makes it useful for doing client-side processing.

They are also somewhat similar in syntax, but that's about it.

Claudiu
  • 206,738
  • 150
  • 445
  • 651
2

Don't be confused with name..
Java was created at Sun Microsystems (now Oracle).
But, JavaScript was created at Netscape (now Mozilla) in the early days of the Web, and technically, “Java-Script” is a trademark licensed from Sun Microsystems used to describe Netscape’s implementation of the language. Netscape submitted the language for standardization to ECMA (European Computer Manufacturer’s Association) and because of trademark issues, the standardized version of the language was stuck with the awkward name “ECMAScript.” For the same trademark reasons, Microsoft’s version of the language is formally known as “JScript.” In practice, just about everyone calls the language JavaScript. The real name is “ECMAScript”.

Both are fully different languages!!!

user577898
  • 203
  • 5
  • 16
1

Practically every PC in the world sells with at least one JavaScript interpreter installed on it.

Most (but not "practically all") PCs have a Java VM installed.

Will Peavy
  • 2,259
  • 3
  • 20
  • 21
0

A Re-Introduction to Javascript by the Mozilla team (they make Firefox) should explain it.

isomorphismes
  • 7,610
  • 9
  • 53
  • 69