The basic Java Essentials for Android development

Source: Internet
Author: User

The basic Java Essentials for Android development

Java type System

There are two types of Java language base data type: Object and base type (primitives). Java ensures type safety by forcing the use of static types, requiring each variable to be declared before it can be used.

This mechanism differs greatly from non-static types of languages, and non-static languages do not require variables to be declared. Although explicit type declarations look cumbersome, they help the compiler prevent many programming errors, such as the creation of unused variables, calls to methods that do not exist, and so on, due to misspelled variable names. Explicit declarations can completely prevent these errors from being generated into the running code. A detailed description of the Java type System can be found in the Java language Specification (Java Language specification).

Basic type

The basic types of Java are not objects, they do not support object-related operations. Basic data types can only be modified by a few predefined operators. The basic types in Java are as follows:

    • Boolean (Boolean): A value of TRUE or False
    • Byte (bytes): 8-bit binary integer
    • Short integer: 16-bit binary integer
    • Int (integer): 32-bit binary integer
    • Long (length): 64-bit binary integer
    • char (character type): 16-bit unsigned integer representing a UTF-16 encoding unit
    • Float (float): 32-bit IEEE-754 standard floating-point number
    • Double (dual precision floating point): 64-bit IEEE-754 standard floating-point number

Objects and classes

Java is an object-oriented language that focuses not on the underlying data types, but on objects (the combination of data and the manipulation of that data). class defines member variables (data) and methods (Programs) that together form an object. In Java, the definition (the template used to build the object) is itself a specific type of object, the class. In Java, a class is the foundation of a type system that developers can use to describe arbitrarily complex objects, including complex, specialized objects and behaviors.

As with most object-oriented languages, in the Java language, some types can inherit from other types. If a class is inherited from another class, it can be said that the class is a subclass of its parent class (subtype or subclass), and its parent class is called a superclass (supertype or superclass). Classes that have more than one subclass can be referred to as the base class (base type) for these subclasses.

In a class, both the scope of the method and the member variable can be global, and they can be visited outside the object by a reference to an instance of the class.

The following gives an example of a very simple class, it has only one member variable Ctr and one Method incr ():

 Public class Trivial
{ /**/ Private Long Ctr; /* * /publicvoid incr ()
{ ctr+ +; } }

Creation of objects

Use the keyword new to create a new object, which is an instance of a class, such as:

Trivial Trivial = new Trivial ();

A variable named trivial is defined on the left side of the copy operator "=". The type of the variable is trivial, so it can only be assigned to an object whose type is trivial. To the right of the assignment, the instance of the newly created trivial class is allocated memory and the instance is manifested. The assignment operator assigns a reference to the newly created object variable.

Excerpt from: "Android Programming"

The basic Java Essentials for Android development

Related Article

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.