Introduction to Java Programming

Source: Internet
Author: User

This tutorial describes the installation and use of the Java programming language and includes some programming examples.

Introduction to Java History


The Java programming language was created in 1991 by Sun Microelectronics's James Gosling. 1995 release of the first version (Java 1.0). In 2010, Sun Microelectronics was acquired by Oracle, and now the Java language is controlled and managed by Oracle Corporation. In 2006, Sun announced that Java was following the GNU General Public License (GPL) and that Oracle continued the project, OpenJDK. Over time, the new enhanced version of Java has been released, and the latest version is Java 1.8, or Java 8.

Java is defined by specifications and includes programming languages, compilers, core libraries, and JVMs (Runtime Java Virtual machine). The Java runtime allows software developers to encode in other languages and still run on a Java virtual machine. Java platforms are typically associated with Java virtual machines and Java core libraries.

Java Virtual Machine


A Java virtual machine (JVM) can be understood as a virtual machine implemented by software that can execute program code just like a physical computer. Java virtual machines have specific versions under different operating systems, such as: The version for the Linux operating system is not the same as the version for the Windows operating system.

The Java program is compiled by the Java compiler into bytecode (bytecode), and the compiled bytecode is interpreted by the Java Virtual machine for execution.

JRE and JDK


There are two versions of Java, Java Runtime Environment (JRE) and Java Development Kit (JDK).

Java Runtime Environment (JRE) contains the necessary components to run Java programs: Java Virtual machines and Java class libraries.

Java Development Kit (JDK) contains the necessary tools for creating Java applications, such as Java compilers, Java virtual machines, and Java class libraries.

Features of the Java language

The design goal of the Java language is to write once, running everywhere.

The Java language has the following features:

    • Platform-agnostic: Java runs Java programs using Java virtual machines, and Java virtual machines are an abstraction layer between applications and operating systems, and applications do not directly access the operating system. This makes Java applications highly portable. A compatible standard and rules-compliant Java application can work on all supported platforms without modification, such as Windows and Linux.
    • Object-Oriented programming language: In addition to the native data type, everything in the Java language is object.
    • Strongly typed programming languages: The Java language is a strongly typed programming language. For example: variable types need to be pre-defined, strict type conversion checks (most cases have a program ape complete).
    • Explanatory and compiled languages: Java source code is compiled into bytecode (bytecode), which makes the Java language platform-Independent. These bytecode (bytecode) directives are interpreted by the Java Virtual Machine (JVM) for execution. The JVM uses the Hotspot compilation technique to translate performance-related bytecode directives into binary code execution with the operating system.
    • Automatic memory management: Java. Managing the memory allocation and recycling work of newly created objects application code does not directly access memory. A mechanism called garbage collection (garbage collector) automatically removes objects without references.

The syntax of the Java language is very close to the syntax of the C + + language, where the Java language is case-sensitive, such as: the myvalue variable and the myvalue variable are two different variables.

The Java language development process


Java source code files are plain text documents, and Java programmers typically write Java programs in the Integrated Development evvironment (IDE). The IDE is a tool to help programmers do the coding work, it has automatic formatting code, syntax highlighting and other functions.

The Java programmer (or IDE) calls the Java Compilation tool (Javac) to compile the source code, and the Java Compilation tool compiles the source code into a bytecode (bytecode) instruction. These instructions are saved in the. class file and run by the Java virtual machine (JVM).


Garbage collection (garbage collector)


The JVM automatically reclaims no referenced memory space, it checks all the object references and looks for those objects that can be automatically reclaimed. The garbage collection mechanism frees the programmer from having to manage the memory manually, but the programmer still needs to ensure that there are no unwanted object references in the program, otherwise the garbage collection mechanism cannot automatically free up object memory. We usually refer to unwanted object references as often referred to as "memory leaks".

Classpath


The Java compiler and the Java runtime use the Classpath (CLASSPATH) to locate and mount the. class file. For example, if you are going to use a third-party Java class library in your application then you need to add the path to your class library to your classpath, or your application will not compile or run.

Installing Java

Check the installation

Your computer may already have Java installed, and you can use the following command in the console to test if Java is installed (if you are using a Windows operating system, you can press WIN+R, enter it, and return to the cmd console):

Java-version

If your computer already has Java installed, you should see the output of the installed Java version information. If the command line returns the application is not found, then you need to install Java.

Installing Java

In the Ubuntu operating system, you can install Java using the following command:

sudo apt-get install OPENJDK-7-JDK

For the Microsoft Windows operating system, you can download the corresponding installation package to the Oracle website, which also has documentation to guide you on how to install Java on other operating systems.

If there is a problem during the installation process, you can use "How to install the JDK on your_os" keyword in google search (for domestic users use "How to install JDK in Your_os" keyword in Baidu search) remember to "Your_os" Replace with your operating system name, for example: Windows, Ubuntu, Mac OS X, and so on.

2.3 Verifying the installation

Go back to the command line just now (don't know that?) Refer to section 2.3) to execute the following command:

Java-version

You will get the following output:

" 1.7.0_25 "  2.3.10) (7u25-2.3.10-1ubuntu0.13.04.264-bit Server VM (build 23.7-b01, Mixed mode)
2.4 How to view the 32-bit or 64-bit version of Java that is currently in use

You can use a 32-bit or 64-bit version of Java on a 64-bit operating system, if the output of the java -version command contains a 64-bit string stating that the Java version you are using is 64-bit, otherwise the Java version you are using is 32-bit. The following is a 64-bit version of the output:

" 1.7.0_25 "  2.3.10) (7u25-2.3.10-1ubuntu0.13.04.264-bit Server VM (build 23.7-b01, Mixed mode)

   


/span>
Writing source code

The following Java code is written on the Linux operating system command line using a text editor (vim, Emacs, and so on). Similar to other operating systems, this is no longer an introduction.

First you need to create a new directory to save the source code, here we use the directory \home\vogella\javastarter . If you use the Windows directory it is possible c:\temp\javastarter that later we will use "Javadir" to represent this path.

Open a text editor, as follows: Linux operating system gedit, vim, emacs, etc. under Windows Notepad, and then enter the following code:

Helloworld.java:

 public class  HelloWorld {  public static   void  Main (string[] args) {    System.out.println ("Hello World");}  }

Note : Do not use the Rich text editor, such as Microsoft Word or LibreOffice, to write the source code.

Save the source code to a file in the "Javadir" directory HelloWorld.java . The Java source file name is always the same as the class name in the source code and is .java used as a suffix. In this example, the source file HelloWorld.java is named because the class name we defined is HelloWorld .

Compile, run

Open a Shell (Linux and Unix-like) or command line (Windows), using the cd javadir "Javadir" directory, in our case the command is cd \home\vogella\javastarter . Use ls (in Window dir ) to verify that the source file exists.

Compile the source file using the following command:

Javac Helloworld.java

Once the command is complete, re-use ls (or) the command to view the contents of the dir directory, you can see a file in the directory HelloWorld.class , indicating that you have successfully compiled the source code into bytecode.

tip : By default, the compiler places each class file in a directory that is common to the source files. You can use parameters at compile time -d to specify a different directory.

Now it's time to run your first Java application. Make sure you are still in the "Javadir" directory, and then execute the following command to run the program:

Java HelloWorld

The program will output the "Hello World" string in the terminal, reference

Using Classpath (CLASSPATH)

You can run the application from another location by specifying the CLASSPATH. Open the shell or console, then go to a directory and enter the following command:

Java HelloWorld

If you are not currently in the same directory as the compiled class file, the Java virtual machine will prompt the error: "Exception in Thread" main "Java.lang.NoClassDefFoundError: HelloWorld ".

To run the program correctly, enter the following command (replace Mydirectory with your "javadir"):

" mydirectory " HelloWorld

You can then see the "HelloWorld" string output.

Fundamentals of the Java language architecture: Packages, classes (Class), and objects (object)

It is important to understand the basic terminology of the Java package, class, and Object (object), which outlines these terms.

Packages (Package)

Java uses packages to organize classes, typically grouping classes into different packages according to business logic. For example: All graphical interfaces of an application may be grouped into com.vogella.webapplication.views packages.

The usual practice is to use the reverse of the company domain name as a top-level package, such as: The company's domain name is "4byte.cn" then the top-level package name of the company's Java application may be cn.4byte .

Another important use of packages is to avoid class naming conflicts, which means that two developers use the same fully qualified name for the classes they write. The fully qualified name of the class in Java is Registration + '. ' + Class name, for example: cn.4byte.HelloWorld .

If there is no package, when two program apes give him a class namedTest  a naming conflict (and the operating system cannot create files). Combined with the Java package mechanism, we can explicitly tell The virtual machine we will use which   Test Span class= "Apple-converted-space" >  class, for example: The first programmer will   Test   class is placed in   report   package, another the programmer wrote him   Test   class put to  , XmlReader   package, Then they can clearly distinguish two classes of   by fully qualified names; report. Test   and   XmlReader. Test .

Classes (Class)

Definition: A class is a template that defines the data and behavior of an object and can understand the blueprint of the class as an object.

Use keywords in Java class to define classes, and the first letter of a class name must be capitalized. The class body needs to be in ' {..} ' defined in the. such as:

Myclass.java:

 Package test; class MyClass {}

The data of the class is persisted in the attribute, and the class behavior is implemented by the method. The Java source file needs to be "class name" + ". Java "in the form of save.

Objects (object)

definition : An object is an instance of a class. An object is a real element that has data and an executable operation. Each object is created according to the definition of the class.

Inherited

A class can derive from another class, which we call a subclass. Another common argument is that a class extends another class. The class that is derived (or inherited or extended) is called the "parent class".

Inheritance allows subclasses to inherit the methods and behavior of the parent class (which is not mentioned here, which is described later), and the following code shows how to inherit a class, Java is a single-inheritance system (unlike C + +) A class can have only one parent class.

Mybaseclass.java:

 Package com.vogella.javaintro.base; class MyBaseClass {  @Override  publicvoid  hello () {    System.out.println (" Hello from MyBaseClass ");}  } class extends MyBaseClass {}

object is the parent class of all classes

All classes in Java implicitly inherit the Object class. The object class defines some of the following methods for each of the Java objects:

    • equals(other)Checks whether the current object is equal to the other object

    • getClass()Returns the class of the object (class object)

    • hashCode()Returns the unique identifier of the object

    • toString()Returns the string description of the current object

English Address: http://www.vogella.com/tutorials/JavaIntroduction/article.html

Chinese Address:

Introduction to Java Programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.