Java class application, java class

Source: Internet
Author: User

Java class application, java class
Basic data type packaging
Integer Character
Others use uppercase letters;

Conversion Between the package class and the basic type:
 
Integer int
Integer I = new Integer (int value );
Int i2 = I. intValue ();
 
Basic data type and String Conversion
String --> int
Static int parseInt (String s)
Int I = Integer. parseInt ("123 ");
Int-> String
String valueOf (int val );
String str = String. valueOf (123 );
 
Packing and unpacking:

Java 5 began to show automatic packing and unpacking;
 
Automatic packing:
You can directly assign a value of the basic data type to its corresponding packaging object;
Integer I = 17;
Automatic unpacking: Assign the packaging class object directly to a variable of the corresponding basic type.
Int i2 = new Integer (2 );
Object is the super parent class of all classes,
The Object can accept all data;
Metadata mode:
Byte, Short, Integer, Long caches data in a range. [-128,127] Check the source code.
Integer i1 = 123;
Integer i2 = 123;
I1 = i2; // true
New Integer (123) = new Integer (123); // false
Object Class: a class that describes an Object.
Is a super parent class;
Accept all data, including arrays;
Object o = {1, 2, 3 };
Object o = new int [] {1, 2, 3 };
Boolean equals (Object otherObj );
That is, the current object is compared with otherObj;
By default, the address in the memory is compared, which is the same as =;
Generally, child classes are required to override this method based on their own conditions,
String override equals Method
The Integer class also overwrites the equals method.
Int hashCode (); // returns a decimal hash value of an object. The hashCode of each object is different.
String toString (); // converts an object to the String type to describe the object information.
By default, the full qualified name of the class + @ + Integer. toHexString (this. hashCode ());
This method is generally required to be overwritten by subclass.
Usually we print the object. In fact, we print the toString method of the object. That is to say, at the underlying layer, this object will call the toString method.

Class Person {
String name;

Int age;
Public Person (String name, int age)
{
This. name = name;
This. age = age;
}

Public String toString ()
{
Return this. name + "," + this. age;
}
}
 
Public static void mian (String [] args)
{
System. out. println (new Person ("Will", 17 ));
System. out. println (new Person ("Lucy", 16 ));

}
 
Code block:
1. Common (local) code block, written in the method;
2. constructor code: inside the class, outside the method. features: the constructor executes the code before the constructor. Each time an object is created, the code is executed.
3. static code block: The constructed code block modified by static. features: it takes precedence over the execution of the main method and is only executed once. It can be used to assign values to static variables;
4. synchronous code block
 
Singleton mode:
Ensure that a class in the project has only one instance object during running;
// 1. privatize the constructor to prevent external users from creating objects
// 2. Create an object internally, and then modify it using private,
// 3. Expose a Global static method to return the internally created object;
Because static members can only access static members, the object must be modified using static,
Hungry Chinese style:
Class Singleton
{
Private Singleton (){}

Private static final Singleton instance = new Singleton ();
// Thread security
Public static Singleton getInstance ()
{
Return instance;
}
}
Lazy style:
Class Singleton
{
Private Singleton (){}

Private static final Singleton instance = null;

// The thread is insecure.
Public static Singleton getInstance ()
{
If (instance = null)
{
Instance = new Singleton ()
}
Return instance;
}
}
Final:
Indicates the final;
Class, method, and variable can be modified;
The final-modified class indicates the eunuch class and cannot be inherited. The basic data type packaging class is
The final modification method indicates that the quilt class cannot be overwritten;
Final modified variables:
Constant: a constant name is written in large numbers. If multiple words are composed, words are separated by underscores;
Global constant: Variable modified by public static final

Constants can be assigned only once.

If it is a constant of the reference type, the address of the referenced object cannot be changed, but the content of the object can be changed;

Internal classes in the method can only access the variables modified by using final in the method;
 
Abstract class:
Abstract-modified classes are generally abstract classes that contain abstract methods, but are not required;
If a class has abstract methods, the class must be an abstract class;

Objects cannot be created (new cannot be created), and others can be changed to common classes.

It can contain common methods;

When to use abstract methods:
The parent class defines the behavior that the subclass should possess, but the implementation of the specific behavior must be completed by a subclass;

Abstract method:
Using abstract modification,
There is only a declaration of the method, and there is no specific implementation of the method; (it is implemented by the subclass)

It is generally recommended to put the abstract in front of the modifier;

Abstract classes must have subclasses,
In addition, the subclass must overwrite the abstract methods in the abstract class, both of which are also used as the abstract class;

All young people have the behavior of eating (eat) and sitting (set;
However, ArtMan and NormalMan have different eating and sitting behaviors;

Abstract class Person
{
Abstract void eat ();
Abstract void set ();
}

Class ArtMan extends Person
{
Void eat ()
{
System. out. println ("one root eat ");
}
}

Class NormalMan extends Person
{
Void eat ()
{
System. out. println ("One bite ");
}
}

Class B2 extends Person
{
Void eat ()
{
System. out. println ("A lump of food ");
}

Void set ()
{
System. out. println ("stir your legs into a twist ");
}

Void show ()
{
System. out. println ("stir your legs into a twist ");
}
}
... Mian... // Main Function
{
Person p = new B2 ();
P. eat (); // actually calls the method in B2
P. set ();
// P. show (); // It cannot be found in Person.
}
How to Apply classes and objects in java

Objects are class instantiation, and java is a pure surface object programming language. The class and object are the soul of the class. The member variables and methods in the class are implemented through the object. Of course, the class is just an empty shelf and has no specific use. Therefore, this is interdependent.

What types of java applications are divided? What are their characteristics?

Java has three main directions: 1. J2SE; 2. J2EE; 3.
J2SE: java 2 Standard edition. It is the java Standard Edition and mainly contains the core classes of the java language. Before learning J2EE and J2SE, you must first learn J2SE as the syntax base and have a good syntax base, in order to better learn high-level technologies. It can be used to develop some desktop applications in CS mode.
J2EE: java 2 Standard edition. It is an Enterprise Edition of the java language and is mainly used for enterprise-level application development. It is a set of technical architecture completely different from traditional application development. It contains many components, which can simplify and standardize the development and deployment of application systems, thereby improving portability, security, and reuse value. It is mainly used to develop enterprise-level applications in BS mode.
Java 2 Micro Edition. It is mainly used for the development of electronic products.

Do you want to know this?

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.