Java Stunote 3

Source: Internet
Author: User

1. Building a class

A. Properties

B. methods

The property is the owner, the method for the property service, is the servant.

C. building granularity of classes

Satisfies the design needs of the program, the properties and methods not involved can not be added to the design of the class.

2. Process Introduction

Computer memory is allocated as a process unit. Each process is logically isolated, each process is considered to be the only program on this computer, its own face is all the memory space, this technology is called process isolation technology, ensure that the system will not because of a process failure, resulting in the crash.

A process can request memory from the operating system, and the memory that the process requests is generally used in two formats, one in the form of Stacks ( stack) and one in heap . a process can be multiple stacks, but only one heap, one stack corresponds to a thread, and a process requires at least one thread (thread) , that is, at least one stack.

The program we are currently compiling is a single-process single-threaded program that has a stack and a heap.

3. object creation and method invocation

Car c=new car ();

1) Use class loader to read Car into code area

2) Create reference variable C (Car type ) on the stack

3) Request memory in the heap to create the bare object, and initialize the This , super and the code pointers and other implicit properties.

4) Call the constructor method to implement the initialization of the Car object's own explicit properties.

5) Gets the initialized good Car Object memory address assigned to the reference variable c.

C.showcarinfo ();

1) An instruction that calls showcarinfo () is made to the Car objectby referencing the variable C .

2) when the Car object receives the instruction, it accesses its code pointer immediately, accesses the Showcarinfo method code in the code area and executes.

4. Cascade Construction Method (Cascade constructor)

Student () {

This ("Mary", 12);

}

Student (String name) {

This (name,12);

}

Student (String name,int age)

{

this. name=name;

this. age =age;

}

The Cascade construction method provides a variety of construction methods, which facilitates the creator of the object.

Cascade Construction Method Set all the processing logic in the whole parameter construction method, easy to centrally modify and maintain.

It is important to note that in the process of mutual invocation of the constructor method, This is used instead of the constructor name, and the class name cannot be called directly.

5. instance variables and class variables

Example (instance) is the meaning of the object, more written.

Instance variables are also called instance properties, and each instance has a copy, and in the process of creating a bare object, the space of the instance variable is opened up, and in the constructor, the space of the instance variable is initialized.

Ex:int count;

Class variables belong to the class itself, meaning that it belongs to the template (stencil), the class variable is not in the object, all objects can access the class variable, and the class variable is shared by all class objects.

During class loading, the class object is created in the static zone.

ex:static int count;

1. Package Concept

A good class name, everyone loves to use, then in the case of the same class name, it will be difficult to distinguish between the owner of the class, so we must give the Class A scope ( Attribution ), JAVA is through the package mechanism to solve.

Case:

Zhang San wrote the student class Zhangsan. Student

John Doe wrote the student class Lisi. Student

Harry wrote a program that used the above two class of students:

Zhangsan. Student s=new Zhangsan. Student ();

A package is a logical structure that is the attribution flag of a class and cannot have a similar class name in the same package.

How is the package physically implemented? is represented by the disk directory.

The package name may also appear similar phenomenon, in order to avoid this situation, we generally use the unit's domain name flip to as the package name, because the domain name will not be duplicated, a domain name as a package name may be created on disk multiple levels of directory.

To avoid the classes you write, use problems in subsequent systems, and do not write the No package class / Default Package Class .

Case:

Package Edu.fzu.cs; Package keyword that describes the packages that this class belongs to

public class Student {

}

Package name must be all lowercase

2. Packing class (wrapper)

Any basic type:byte,int, float, double, bOolean,char (16bits) ..... (no unsigned data type)

Char:utf-8 Internationalization Code

In JAVA , there is a class type that corresponds to it.

Intàjava.lang.integer (Java.lang Java Language Pack , Lang=language,

all the classes in the Java.lang package are imported by the compiler by default in any JAVA program, and the syntax is:

Import java.lang.*;

Because the class used in the Lang packet is extremely frequent, is the language extension and the service class, in order to reduce the programmer's import burden, all the classes under the package are imported by the compiler help, the user does not need to import manually.

)

The main role of the packaging class:

A) Intàinteger

b) floatàfloat

c) doubleàdouble

D) Booleanàboolean

e) Charàcharacter

1. Compensate for basic types of semantic deletions

The base type cannot be represented: "No"

float Mark; 0

Float Mark 0, NULL

2. Providing services for basic types

System.out.println (integer.tobinarystring (100));

System.out.println (integer.tohexstring (100));

System.out.println (Integer.parseint ("12") +1);

Class types are created in the heap, and the space consumption is low.

Packing (Inbox) and unpacking (outbox)

Represents the conversion behavior between a class type and a base type.

Manual version:

Integer b = new Integer (10);

Int a = B.intvalue;

Automatic Version:

Integer b=30; Àinteger b=new Integer (30);

Int a=b; Àint a =b.intvalue ();

Because of the frequent swap operations between the base type and the class type,SUN provides automatic boxing and unboxing in the higher versions of the JDK , and the compiler helps you write the conversion code.

Automatic boxing and unboxing are supported in versions above JDK5.

3. the String class understands

string s=new string("abc");

string m=new string("abc");

The new string () syntax tells the compiler to create an object, so two string objects are created here.

System. out. println (s==m);

String a="xyz";

String b="xyz";

System. out. println (a==b);

In order to be compatible with the C Developer's Custom,SUN also allows to not write new string, but the strings are assigned values.

When the "XYZ" string object does not exist in memory, the system will create a string object that will be reused when it already exists.

Invariance of Strings (immutable)

Once any string object is created, it cannot be changed, and the modification of the string by reference to the variable will result in the creation of the new string object.

A=a+ "MN";

System.out.println (A==B);

A large number of string additions result in a large number of string objects, such as the following:

String a= "Superstar";

A=a+ "M";

If there are more string additions involved, use the stringbuffer class.

String s="This is a book!";

System. out. println (S.length ()); // Find string length

System. out. println (S.trim (). Length ()); // Remove kinsoku spaces

System. out. println (S.touppercase ());

System. out. println ("ABcD". toLowerCase ());

System. out. println (S.indexof (' s ')); // Find out where s This character first appears in the string

System. out. println (S.lastindexof (' s ')); // Find out where s This character last appeared in the string

System. out. println (S.indexof ("book")); // Find out where the substring appears

System. out. println (S.indexof ("world!"));

System. out. println (S.tochararray () [3]);

System. out. println (S.charat (3));

System. out. println (S.concat ("hello,world!")); // string addition

System.  out. println ("abc". CompareTo ("BCA")); //-1,0,1

System.  out. println ("ABC". CompareTo ("abc")); //-1,0,1

System.  out. println ("CBC". CompareTo ("BCA")); //-1,0,1

System. out. println ("ABC". Comparetoignorecase ("abc"));

System. out. println (S.contains ("book"));

System. out. println (S.endswith ("!"));

System. out. println (S.isempty ()); //Is empty

System. out. println (S.replace (' s ', ' m '));

System. out. println (s.valueof (1));

System. out. println (1+"");

System. out. println ("ABCDEFG". Substring (3));

System. out. println ("ABCDEFG". Substring(3,6));



Key: Process & Thread

A process is a logical unit, a thread similar to a physical unit, a process like a class, a thread like a student, a general class travel application funding, is the class name, so here is represented by

Process to apply, the operating system will only assign space to the process, a process contains at least one thread, the process has requested resources, as the class applied for Banfei, Banfei 800 yuan, to each student split 500 yuan, this 500 yuan is called the stack, the remaining 300 common called heap-

Program Ape by invoking the reference variable within the stack, the class object to send a signal, the class object receives a signal, the object's code pointer to call the corresponding code, access to the code in some cases need to use the class object's property content, so there is a this pointer, The purpose of this pointer is to facilitate the code layer to more accurately navigate to the desired object's property data




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Stunote 3

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.