Top 1,000 Features Creators Resources Extensions Blog Explorer Download
GitHub icon

Java

< >

Java is an open source programming language created in 1995 by James Gosling.

#4on PLDB 29Years Old 12mRepos
Download source code:
git clone https://github.com/openjdk/jdk

Try now: Riju Β· Replit

Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. Read more on Wikipedia...


Example from Compiler Explorer:
// Type your code here, or load an example. class Square { static int square(int num) { return num * num; } }
Example from Riju:
public class Main { public static void main(String[] args) { System.out.println("Hello, world!"); } }
Example from hello-world:
public class Java { public static void main(String[] args) { System.out.println("Hello World"); } }
// Hello World in Java class HelloWorld { static public void main( String args[] ) { System.out.println( "Hello World!" ); } }
Example from Linguist:
/** * Copyright (c) Rich Hickey. All rights reserved. * The use and distribution terms for this software are covered by the * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) * which can be found in the file epl-v10.html at the root of this distribution. * By using this software in any fashion, you are agreeing to be bound by * the terms of this license. * You must not remove this notice, or any other, from this software. **/ /* rich Apr 19, 2008 */ package clojure.lang; import java.lang.ref.Reference; import java.math.BigInteger; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.lang.ref.SoftReference; import java.lang.ref.ReferenceQueue; public class Util{ static public boolean equiv(Object k1, Object k2){ if(k1 == k2) return true; if(k1 != null) { if(k1 instanceof Number && k2 instanceof Number) return Numbers.equal((Number)k1, (Number)k2); else if(k1 instanceof IPersistentCollection || k2 instanceof IPersistentCollection) return pcequiv(k1,k2); return k1.equals(k2); } return false; } static public boolean equiv(long k1, long k2){ return k1 == k2; } static public boolean equiv(Object k1, long k2){ return equiv(k1, (Object)k2); } static public boolean equiv(long k1, Object k2){ return equiv((Object)k1, k2); } static public boolean equiv(double k1, double k2){ return k1 == k2; } static public boolean equiv(Object k1, double k2){ return equiv(k1, (Object)k2); } static public boolean equiv(double k1, Object k2){ return equiv((Object)k1, k2); } static public boolean equiv(boolean k1, boolean k2){ return k1 == k2; } static public boolean equiv(Object k1, boolean k2){ return equiv(k1, (Object)k2); } static public boolean equiv(boolean k1, Object k2){ return equiv((Object)k1, k2); } static public boolean equiv(char c1, char c2) { return c1 == c2; } static public boolean pcequiv(Object k1, Object k2){ if(k1 instanceof IPersistentCollection) return ((IPersistentCollection)k1).equiv(k2); return ((IPersistentCollection)k2).equiv(k1); } static public boolean equals(Object k1, Object k2){ if(k1 == k2) return true; return k1 != null && k1.equals(k2); } static public boolean identical(Object k1, Object k2){ return k1 == k2; } static public Class classOf(Object x){ if(x != null) return x.getClass(); return null; } static public int compare(Object k1, Object k2){ if(k1 == k2) return 0; if(k1 != null) { if(k2 == null) return 1; if(k1 instanceof Number) return Numbers.compare((Number) k1, (Number) k2); return ((Comparable) k1).compareTo(k2); } return -1; } static public int hash(Object o){ if(o == null) return 0; return o.hashCode(); } static public int hasheq(Object o){ if(o == null) return 0; if(o instanceof Number) return Numbers.hasheq((Number)o); else if(o instanceof IHashEq) return ((IHashEq)o).hasheq(); return o.hashCode(); } static public int hashCombine(int seed, int hash){ //a la boost seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2); return seed; } static public boolean isPrimitive(Class c){ return c != null && c.isPrimitive() && !(c == Void.TYPE); } static public boolean isInteger(Object x){ return x instanceof Integer || x instanceof Long || x instanceof BigInt || x instanceof BigInteger; } static public Object ret1(Object ret, Object nil){ return ret; } static public ISeq ret1(ISeq ret, Object nil){ return ret; } static public <K,V> void clearCache(ReferenceQueue rq, ConcurrentHashMap<K, Reference<V>> cache){ //cleanup any dead entries if(rq.poll() != null) { while(rq.poll() != null) ; for(Map.Entry<K, Reference<V>> e : cache.entrySet()) { Reference<V> val = e.getValue(); if(val != null && val.get() == null) cache.remove(e.getKey(), val); } } } static public RuntimeException runtimeException(String s){ return new RuntimeException(s); } static public RuntimeException runtimeException(String s, Throwable e){ return new RuntimeException(s, e); } /** * Throw even checked exceptions without being required * to declare them or catch them. Suggested idiom: * <p> * <code>throw sneakyThrow( some exception );</code> */ static public RuntimeException sneakyThrow(Throwable t) { // http://www.mail-archive.com/javaposse@googlegroups.com/msg05984.html if (t == null) throw new NullPointerException(); Util.<RuntimeException>sneakyThrow0(t); return null; } @SuppressWarnings("unchecked") static private <T extends Throwable> void sneakyThrow0(Throwable t) throws T { throw (T) t; } }
Example from Wikipedia:
// Hello.java (Java SE 5) import javax.swing.*; public class Hello extends JFrame { public Hello() { super("hello"); super.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); super.add(new JLabel("Hello, world!")); super.pack(); super.setVisible(true); } public static void main(final String[] args) { new Hello(); } }
abstract continue for new switch assert default goto package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static void class finally long strictfp volatile const float native super while _

Language features

Feature Supported Token Example
Scientific Notation βœ“
Binary Literals βœ“
// 0[bB][01][01_]*[lL]?
Integers βœ“
// 0|[1-9][0-9_]*[lL]?
Floats βœ“
// ([0-9][0-9_]*\.([0-9][0-9_]*)?|\.[0-9][0-9_]*)([eE][+\-]?[0-9][0-9_]*)?[fFdD]?|[0-9][eE][+\-]?[0-9][0-9_]*[fFdD]?|[0-9]([eE][+\-]?[0-9][0-9_]*)?[fFdD]|0[xX]([0-9a-fA-F][0-9a-fA-F_]*\.?|([0-9a-fA-F][0-9a-fA-F_]*)?\.[0-9a-fA-F][0-9a-fA-F_]*)[pP][+\-]?[0-9][0-9_]*[fFdD]?
Hexadecimals βœ“
// 0[xX][0-9a-fA-F][0-9a-fA-F_]*[lL]?
Octals βœ“
// 0[0-7_]+[lL]?
Conditionals βœ“
Inheritance βœ“
Access Modifiers βœ“
Switch Statements βœ“
Exceptions βœ“
Constants βœ“
Classes βœ“
While Loops βœ“
Booleans βœ“ true false
Case Sensitivity βœ“
MultiLine Comments βœ“ /* */
/* A comment
*/
Print() Debugging βœ“ System.out.println
Threads βœ“
Line Comments βœ“ //
// A comment
Increment and decrement operators βœ“
Module Pattern βœ“
// Package = directory. Java classes can be grouped together in packages. A package name is the same as the directory (folder) name which contains the .java files. You declare packages when you define your Java program, and you name the packages you want to use from other libraries in an import statement.
// The first statement, other than comments, in a Java source file, must be the package declaration.
// Following the optional package declaration, you can have import statements, which allow you to specify classes from other packages that can be referenced without qualifying them with their package.
// This source file must be Drawing.java in the illustration directory.
package illustration;
import java.awt.*;
public class Drawing {
 // ...
}
Zero-based numbering βœ“
Iterators βœ“
Iterator iter = list.iterator();
//Iterator iter = list.iterator();    in J2SE 5.0
while (iter.hasNext()) {
    System.out.print(iter.next());
    if (iter.hasNext())
        System.out.print(", ");
}
Interfaces βœ“
interface MyInterface{
   /* This is a default method so we need not
    * to implement this method in the implementation
    * classes
    */
   default void newMethod(){
       System.out.println("Newly added default method");
   }
   /* Already existing public and abstract method
    * We must need to implement this method in
    * implementation classes.
    */
   void existingMethod(String str);
}
public class Example implements MyInterface{
  // implementing abstract method
    public void existingMethod(String str){
        System.out.println("String is: "+str);
    }
    public static void main(String[] args) {
      Example obj = new Example();
      
      //calling the default method of interface
        obj.newMethod();
        //calling the abstract method of interface
        obj.existingMethod("Java 8 is easy to learn");
  
    }
}
File Imports βœ“
import javax.swing.*;
import javax.swing.JOptionPane;
// use fully qualified name without import:
javax.swing.JOptionPane.showMessageDialog(null, "Hi");
// There are 166 packages containing 3279 classes and interfaces in Java 5.
// import java.io.*; Input-output classes.
Garbage Collection βœ“
Constructors βœ“
Comments βœ“
Strings βœ“ "
"hello world"
Single Dispatch βœ“
Generics βœ“
List v = new ArrayList();
v.add("test");
Integer i = v.get(0); // (type error)  compilation-time error
Assert Statements βœ“
// By default, assertions are disabled
// java –enableassertions Test
int score = 10;
assert score >= 10 : " Below";
System.out.println("score is "+score);
Case Insensitive Identifiers X
Semantic Indentation X
Operator Overloading X
Macros X
Pointers X
Variable Substitution Syntax X
S-Expressions X

View source

- Build the next great programming language Β· About Β· Acknowledgements Β· Published by Breck's Lab