Java (Github, Java EE, Github, OpenJDK, Github, Topics), is a set of computer software and specifications developed by James Gosling at Sun Microsystems, which was later acquired by the Oracle Corporation, that provides a system for developing application software and deploying it in a cross-platform computing environment.
TOC
- Version
- Ecosystem
- Compiler
- Package
- Servlet container
- Framework
- Syntax
- Documentation
- Exception
- Java JDBC
- Test
- Resources
Java | Description |
---|---|
Java Card | A technology that allows small Java-based applications (applets) to be run securely on smart cards and similar small-memory devices. |
Java ME (Micro Edition) | Specifies several different sets of libraries (known as profiles) for devices with limited storage, display, and power capacities. It is often used to develop applications for mobile devices, PDAs, TV set-top boxes, and printers. |
Java SE (Standard Edition) | For general-purpose use on desktop PCs, servers and similar devices. |
Jakarta EE (Enterprise Edition) | Java SE plus various APIs which are useful for multi-tier client–server enterprise applications. |
Version
Version | Release date | End of Free Public Updates | Extended Support Until |
---|---|---|---|
JDK Beta | 1995 | ? | ? |
JDK 1.0 | January 1996 | ? | ? |
JDK 1.1 | February 1997 | ? | ? |
J2SE 1.2 | December 1998 | ? | ? |
J2SE 1.3 | May 2000 | ? | ? |
J2SE 1.4 | February 2002 | October 2008 | February 2013 |
J2SE 5.0 | September 2004 | November 2009 | April 2015 |
Java SE 6 | December 2006 | April 2013 | December 2018 |
Java SE 7 | July 2011 | April 2015 | July 2022 |
Java SE 8 (LTS) | March 2014 | January 2019 for Oracle (commercial), December 2020 for Oracle (personal use), At least May 2026 for AdoptOpenJDK, At least June 2023[7] for Amazon Corretto | December 2030 |
Java SE 9 | September 2017 | March 2018 for OpenJDK | N/A |
Java SE 10 | March 2018 | September 2018 for OpenJDK | N/A |
Java SE 11 (LTS) | September 2018 | At least August 2024[7] for Amazon Corretto, October 2024 for AdoptOpenJDK | September 2026 |
Java SE 12 | March 2019 | September 2019 for OpenJDK | N/A |
Java SE 13 | September 2019 | March 2020 for OpenJDK | N/A |
Java SE 14 | March 2020 | September 2020 for OpenJDK | N/A |
Java SE 15 | September 2020 | March 2021 for OpenJDK | N/A |
Java SE 16 | March 2021 | September 2021 for OpenJDK | N/A |
Java SE 17 (LTS) | September 2021 | TBA | TBA |
Ecosystem
Writing in the Java programming language is the primary way to produce code that will be deployed as byte code in a Java virtual machine (JVM), or Java Runtime Environment (JRE).
- Java Development Kit (JDK) is a Sun product aimed at Java developers.
- Open Java Development Kit (OpenJDK) is a free and open-source implementation of the Java Platform, Standard Edition (Java SE).
The three most basic parts of the Java ecosystem are the Java Virtual Machine (JVM), the Java Runtime Environment (JRE), and the Java Development Kit (JDK), which are stock parts that are supplied by Java implementations.
Compiler
Third parties have produced many compilers or interpreters that target the JVM. Some of these are for existing languages, while others are for extensions to the Java language. These include:
- BeanShell – A lightweight scripting language for Java[25]
- Ceylon – An object-oriented, strongly statically typed programming language with an emphasis on immutability
- Clojure – A modern, dynamic, and functional dialect of the Lisp programming language on the Java platform
- Gosu – A general-purpose Java Virtual Machine-based programming language released under the Apache License 2.0
- Groovy – A fully Java interoperable, Java-syntax-compatible, static and dynamic language with features from Python, Ruby, Perl, and Smalltalk
- JRuby – A Ruby interpreter
- Jython – A Python interpreter
- Kotlin – An industrial programming language for JVM with full Java interoperability
- Rhino – A JavaScript interpreter
- Scala – A multi-paradigm programming language with non-Java compatible syntax designed as a “better Java”
Package
Maven
Build automation tool used primarily for Java projects,
- validate
- generate-sources
- process-sources
- generate-resources
- process-resources
- compile
- process-test-sources
- process-test-resources
- test-compile
- test
- package
- install
- deploy
Gradle
Build automation tool for multi-language software development.
- compileJava
- processResources
- classes
- jar
- assemble
- compileTestJava
- processTestResources
- testClasses
- test
- check
- build
Servlet container
Framework
Web
Unit Test
Syntax
Naming conventions
The name must not contain any white spaces. The name should not start with special characters like & (ampersand), $ (dollar), _ (underscore).
Class
- It should start with the uppercase letter.
- It should be a noun such as Color, Button, System, Thread, etc.
- Use appropriate words, instead of acronyms.
public class Employee {
// code snippet
}
Interface
- It should start with the uppercase letter.
- It should be an adjective such as Runnable, Remote, ActionListener.
- Use appropriate words, instead of acronyms.
interface Printable {
// code snippet
}
Method
- It should start with lowercase letter.
- It should be a verb such as main(), print(), println().
- If the name contains multiple words, start it with a lowercase letter followed by an uppercase letter such as actionPerformed().
class Employee {
// method
void draw() {
// code snippet
}
}
Variable
- It should start with a lowercase letter such as id, name.
- It should not start with the special characters like & (ampersand), $ (dollar), _ (underscore).
- If the name contains multiple words, start it with the lowercase letter followed by an uppercase letter such as firstName, lastName.
- Avoid using one-character variables such as x, y, z.
class Employee {
// variable
int id;
// code snippet
}
Package
- It should be a lowercase letter such as java, lang.
- If the name contains multiple words, it should be separated by dots (.) such as java.util, java.lang.
Constant
- It should be in uppercase letters such as RED, YELLOW.
- If the name contains multiple words, it should be separated by an underscore(_) such as MAX_PRIORITY.
- It may contain digits but not as the first letter.
class Employee {
// constant
static final int MIN_AGE = 18;
//code snippet
}
CamelCase in java naming conventions Java follows camel-case syntax for naming the class, interface, method, and variable. If the name is combined with two words, the second word will start with uppercase letter always such as actionPerformed(), firstName, ActionEvent, ActionListener, etc.
Name | Description |
---|---|
package | namespace domain_name. (lower case) |
import | reuse existing code |
class | class_className {} , template |
method | funcName(args) {} , behavior |
variable | Type: instance , static , local , parameter , argument |
public | public class className { public void funcName(args) {} } |
void | return empty |
@Test | Annotation |
CamelCase | CamelCase |
dot | instName.funcName(args); |
colon | |
object | object-oriented programming language |
constructor | new className(args); |
variable declaration | className varName; |
object allocation | className varName = new className(args); |
OOPs
Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts:
- Object
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
- others
- Coupling
- Cohesion
- Association
- Aggregation
- Composition
Abstract class vs Interface
Abstract class | Interface |
---|---|
1) Abstract class can have abstract and non-abstract methods. | Interface can have only abstract methods. Since Java 8, it can have default and static methods also. |
2) Abstract class doesn’t support multiple inheritance. | Interface supports multiple inheritance. |
3) Abstract class can have final, non-final, static and non-static variables. | Interface has only static and final variables. |
4) Abstract class can provide the implementation of interface. | Interface can’t provide the implementation of abstract class. |
5) The abstract keyword is used to declare abstract class. | The interface keyword is used to declare interface. |
6) An abstract class can extend another Java class and implement multiple Java interfaces. | An interface can extend another Java interface only. |
7) An abstract class can be extended using keyword “extends”. | An interface can be implemented using keyword “implements”. |
8) A Java abstract class can have class members like private, protected, etc. | Members of a Java interface are public by default. |
9)Example: public abstract class Shape{ public abstract void draw(); } |
Example: public interface Drawable{ void draw(); } |
Keywords
abstract
: Java abstract keyword is used to declare abstract class. Abstract class can provide the implementation of interface. It can have abstract and non-abstract methods.boolean
: Java boolean keyword is used to declare a variable as a boolean type. It can hold True and False values only.break
: Java break keyword is used to break loop or switch statement. It breaks the current flow of the program at specified condition.byte
: Java byte keyword is used to declare a variable that can hold an 8-bit data values.case
: Java case keyword is used to with the switch statements to mark blocks of text.catch
: Java catch keyword is used to catch the exceptions generated by try statements. It must be used after the try block only.char
: Java char keyword is used to declare a variable that can hold unsigned 16-bit Unicode charactersclass
: Java class keyword is used to declare a class.continue
: Java continue keyword is used to continue the loop. It continues the current flow of the program and skips the remaining code at the specified condition.default
: Java default keyword is used to specify the default block of code in a switch statement.do
: Java do keyword is used in control statement to declare a loop. It can iterate a part of the program several times.double
: Java double keyword is used to declare a variable that can hold a 64-bit floating-point numbers.else
: Java else keyword is used to indicate the alternative branches in an if statement.enum
: Java enum keyword is used to define a fixed set of constants. Enum constructors are always private or default.extends
: Java extends keyword is used to indicate that a class is derived from another class or interface.final
: Java final keyword is used to indicate that a variable holds a constant value. It is applied with a variable. It is used to restrict the user.finally
: Java finally keyword indicates a block of code in a try-catch structure. This block is always executed whether exception is handled or not.float
: Java float keyword is used to declare a variable that can hold a 32-bit floating-point number.for
: Java for keyword is used to start a for loop. It is used to execute a set of instructions/functions repeatedly when some conditions become true. If the number of iteration is fixed, it is recommended to use for loop.if
: Java if keyword tests the condition. It executes the if block if condition is true.implements
: Java implements keyword is used to implement an interface.import
: Java import keyword makes classes and interfaces available and accessible to the current source code.instanceof
: Java instanceof keyword is used to test whether the object is an instance of the specified class or implements an interface.int
: Java int keyword is used to declare a variable that can hold a 32-bit signed integer.interface
: Java interface keyword is used to declare an interface. It can have only abstract methods.long
: Java long keyword is used to declare a variable that can hold a 64-bit integer.native
: Java native keyword is used to specify that a method is implemented in native code using JNI (Java Native Interface).new
: Java new keyword is used to create new objects.null
: Java null keyword is used to indicate that a reference does not refer to anything. It removes the garbage value.package
: Java package keyword is used to declare a Java package that includes the classes.private
: Java private keyword is an access modifier. It is used to indicate that a method or variable may be accessed only in the class in which it is declared.protected
: Java protected keyword is an access modifier. It can be accessible within package and outside the package but through inheritance only. It can’t be applied on the class.public
: Java public keyword is an access modifier. It is used to indicate that an item is accessible anywhere. It has the widest scope among all other modifiers.return
: Java return keyword is used to return from a method when its execution is complete.short
: Java short keyword is used to declare a variable that can hold a 16-bit integer.static
: Java static keyword is used to indicate that a variable or method is a class method. The static keyword in Java is used for memory management mainly.strictfp
: Java strictfp is used to restrict the floating-point calculations to ensure portability.super
: Java super keyword is a reference variable that is used to refer parent class object. It can be used to invoke immediate parent class method.switch
: The Java switch keyword contains a switch statement that executes code based on test value. The switch statement tests the equality of a variable against multiple values.synchronized
: Java synchronized keyword is used to specify the critical sections or methods in multithreaded code.this
: Java this keyword can be used to refer the current object in a method or constructor.throw
: The Java throw keyword is used to explicitly throw an exception. The throw keyword is mainly used to throw custom exception. It is followed by an instance.throws
: The Java throws keyword is used to declare an exception. Checked exception can be propagated with throws.transient
: Java transient keyword is used in serialization. If you define any data member as transient, it will not be serialized.try
: Java try keyword is used to start a block of code that will be tested for exceptions. The try block must be followed by either catch or finally block.void
: Java void keyword is used to specify that a method does not have a return value.volatile
: Java volatile keyword is used to indicate that a variable may change asynchronously.while
: Java while keyword is used to start a while loop. This loop iterates a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.
Documentation
// import statements
/**
* @author Firstname Lastname <address @ example.com>
* @version 1.6 (current version number of program)
* @since 1.2 (the version of the package this class was first added to)
*/
public class Test {
// class body
}
/**
* Short one line description. (1)
* <p>
* Longer description. If there were any, it would be (2)
* here.
* <p>
* And even more explanations to follow in consecutive
* paragraphs separated by HTML paragraph breaks.
*
* @param variable Description text text text. (3)
* @return Description text text text.
*/
public int methodName (...) {
// method body with a return statement
}
/**
* Description of the variable here.
*/
private int debug = 0;
/**
* The horizontal and vertical distances of point (x,y)
*/
public int x, y; // AVOID
/**
* The horizontal distances of point.
*/
public int x;
/**
* The vertical distances of point.
*/
public int y;
Tag & Parameter | Usage | Applies to | |
---|---|---|---|
@author John Smith | Describes an author. | Class, Interface, Enum | |
{@docRoot} | Represents the relative path to the generated document’s root directory from any generated page. | Class, Interface, Enum, Field, Method | |
@version version | Provides software version entry. Max one per Class or Interface. | Class, Interface, Enum | |
@since since-text | Describes when this functionality has first existed. | Class, Interface, Enum, Field, Method | |
@see reference | Provides a link to other element of documentation. | Class, Interface, Enum, Field, Method | |
@param name description | Describes a method parameter. | Method | |
@return description | Describes the return value. | Method | |
@exception classname description @throws classname description |
Describes an exception that may be thrown from this method. | Method | |
@deprecated description | Describes an outdated method. | Class, Interface, Enum, Field, Method | |
{@inheritDoc} | Copies the description from the overridden method. | Overriding Method | |
{@link reference} | Link to other symbol. | Class, Interface, Enum, Field, Method | |
{@linkplain reference} | Identical to {@link}, except the link’s label is displayed in plain text than code font. | Class, Interface, Enum, Field, Method | |
{@value #STATIC_FIELD} | Return the value of a static field. | Static Field | |
{@code literal} | Formats literal text in the code font. It is equivalent to {@literal} . |
Class, Interface, Enum, Field, Method | |
{@literal literal} | Denotes literal text. The enclosed text is interpreted as not containing HTML markup or nested javadoc tags. | Class, Interface, Enum, Field, Method | |
{@serial literal} | Used in the doc comment for a default serializable field. | Field | |
{@serialData literal} | Documents the data written by the writeObject( ) or writeExternal( ) methods. | Field, Method | |
{@serialField literal} | Documents an ObjectStreamField component. | Field |
Exception
public class JavaExceptionExample{
public static void main(String args[]) {
try {
//code that may raise exception
int data=100/0;
} catch(ArithmeticException e) {
System.out.println(e);
}
//rest code of the program
System.out.println("rest of the code...");
}
}
Keyword | Description |
---|---|
try | The “try” keyword is used to specify a block where we should place exception code. The try block must be followed by either catch or finally. It means, we can’t use try block alone. |
catch | The “catch” block is used to handle the exception. It must be preceded by try block which means we can’t use catch block alone. It can be followed by finally block later. |
finally | The “finally” block is used to execute the important code of the program. It is executed whether an exception is handled or not. |
throw | The “throw” keyword is used to throw an exception. |
throws | The “throws” keyword is used to declare exceptions. It doesn’t throw an exception. It specifies that there may occur an exception in the method. It is always used with method signature. |
Java JDBC
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query with the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC drivers to connect with the database. There are four types of JDBC drivers:
- JDBC-ODBC Bridge Driver,
- Native Driver,
- Network Protocol Driver, and
- Thin Driver
Test
src
main
java
com.quanpan302.tutorial
person
Person.java
resources
test
java
com
quanpan302
tutorial
person
PersonTest.java
Person.java
package com.quanpan302.tutorial.person;
import com.quanpan302.tutorial.person.Name;
/**
*
*/
public class Person{
// instance variable
// private: modifier
// static: memory management, get memory only once in the class area at the time of class loading
private static Name personName;
// default constructor
public Person() {
}
public Person(Name personName) {
// this: reference variable
this.personName = personName;
}
// instance method
public static String helloWorld() {
return "Hello World";
}
public static String hello(String name) {
return "Hello " + name;
}
}
PersonTest.java
package com.quanpan302.tutorial.person;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
*
*/
public class PersonTest{
@Test
public void returnHelloWorld() {
Person quanpan302 = new Person();
assertEquals( "Hello World", quanpan302.helloWorld() );
}
@Test
public void returnObject() {
Person quanpan302 = new Person("QPan");
assertEquals( "Hello QPan", quanpan302.helloWorld() );
}
}