Java Programming Ideas (1)

Source: Internet
Author: User

Java and the Internet

Client/Server System core ideas:
* The system has a central information storage pool, which is used to store some kind of data.
* It usually exists in the database and distributes it to certain people or machine clusters as needed.
* The information storage pool, the software used to distribute the information, and the information and software that reside on the machine or cluster are always called servers.
* Mainstream software on the user's machine communicates with the server, wondering about information processing.
* They are then displayed on the user machine that becomes the client.

In order to minimize the response delay:
* Programmers work to reduce the load on processing tasks, usually distributed to client machine processing.
* But sometimes middleware is used to distribute the load to other machines on the server side.
* Middleware is also used to improve maintainability.

Client-side programming Techniques:
* Plugins (Plug-in): In this way, programmers can download a piece of code and insert it into the browser in the appropriate location to add new features to the browser.
* Scripting language: Represented as JavaScript and can be supported without any plug-ins between Web browsers.
* Java: Client-side programming via applets and Java Web start.
* . NET and C#:.net platforms are equivalent to the JVM and Java class libraries, and C # corresponds to Java.

Server-side programming technology:
* Most requests to the server simply request a file.
* More complex requests to servers typically involve database transactions. A common scenario is a complex database search request.
* The server then formats the result so that it becomes an HTML page that is sent back to the client.
* Java is often written as a servlet program to implement server-side programming.
* Servlet and its derivative JSPs are the two main reasons why many companies that develop Web sites migrate to Java.


everything is an object .

Operational data elements are simplified in Java:
* Everything is treated as an object, so a single fixed syntax can be used.
* Although everything is considered an object, the identifier of the action is actually a reference to the object.
* Use the new operator to create an object and create a reference to associate with the object.
<pre>
string s = new String ("asdf");
</pre>

About basic types:
* Basic type creation is a special case: Int,char, and so on, the same as creating an automatic variable method in C + +.
* All numeric types in the base type have a positive sign.
* The size of the base type is deterministic and does not change with the hardware platform architecture as in C/s + +.
* * Char 2byte,short 2byte,int 4byte,float 4byte,double 8byte,long 8Byte.
* * This non-variability in the amount of storage space makes Java portability more powerful.
* The base type has a wrapper class that allows you to create a non-basic object in the heap that represents the corresponding base type.
* The JVM provides automatic wrapper functionality that converts the base type to the wrapper type and can be reversed.
Cases:
<pre>
char c = ' x ';
Character ch = new Character (c);
</pre>
Or
<pre>
Character ch = new Character (' X ');
</pre>
Conversion method
<pre>
Character ch = ' x ';
char c = ch;
</pre>
In addition, Java provides two classes for high-precision computing, Biginterger and BigDecimal. There is no corresponding base type.

About arrays:
* Java ensures that the array is initialized and cannot be accessed outside of its scope.
* This range check is at the expense of a small amount of memory overhead on the array and runtime subscript checking.

The following code is legal in C + +, but cannot be written in Java:
<pre>
{
int x = 12;
{
int x = 96; Illegal
}
}
</pre>
The practice of hiding in a larger scope in C + + is not allowed in Java.

Example01. Swap in JAVA isn't easy.

In Java:
* Never need to destroy objects.
* The release of object-occupied memory is done to the Java Virtual machine's garbage collector.

Default values for basic members:
* If a member of a class is a basic data type, Java ensures that it will live a default value even if it is not initialized.
* Boolean defaults to false,int/short default to 0,char ' \u0000 ' (null), long defaults to 0L
* However, the above method of ensuring initialization does not apply to "local" variables, i.e. (not a field of a class).

Name Visibility: Import indicates that the compiler imports a package, which is a class library.

Comments and embedded Documents:
* Javadoc is a tool for extracting annotations.
* Some Java compiler techniques are used to find special tags within the program.
* It not only parses the information tagged by these tags, but also extracts the class name or method name of the adjacent annotation.
* Javadoc output is an HTML file that can be viewed in a Web browser. 】

Javadoc Syntax Introduction:
* All Javadoc commands can only appear in the "/**" comment, and, as usual, the comment ends with "*/".
* There are two main ways to use Javadoc.
* * Embed HTML
* * Use document Tags: some commands that start with the "@" character.
* There are three types of annotation documents, corresponding to the three elements following the location of the annotation: class, Domain, and method.

<--Note: Javadoc can only document comments for public and protected members. (The private comment is only included with the-private tag)

Some examples of tags:
* @see: Refer to other classes.
* @author: Describes author information.
* @param: Used to describe identifiers in the parameter list.
* @return: Used to describe the meaning of the return value.


operator

Java operators use:
* Almost all operators can only operate "base type".
* The exception operator is "=", "= =", "! =", these operators can manipulate all objects.
* In addition, the string class supports "+" and "+ =".

Priority level:
* When the compiler observes a string followed by a "+" and "+" immediately following a non-string element.
* will attempt to convert an element of this non-string type to String.

Object Assignment Value:
* When working with objects, we really manipulate references to this object.
* If you assign an object to another object, you are actually copying the reference from one place to another.
* If you use C=d for objects, then both C and D point to the object that originally had only d pointing.

Example02. Assignment for Objects.

Compare operations:
* Basic types use "= =" and "! =" to compare content directly.
* But two objects are compared with "= =" and "! =", which is actually a reference to the object.
* If you want to compare the actual contents of two objects, you must use the Special method equals () that applies to all objects.
* But the default behavior of equals () is to compare references, so for classes that you define, you need to override the Equals () method in the new class.
* Most Java class libraries implement the Equals () method to compare the contents of an object, rather than a reference to a comparison object.

Example03. Comparation for Objects.

Logical operation:
* "and", "or", "non" actions can only be applied to Boolean values.
* Unlike C/s + +, you cannot use a non-Boolean value as a Boolean value in a logical expression.
* In addition, if a Boolean value is used where a string value should be used, the Boolean value is automatically converted to the appropriate text form.

Direct constants:
* The suffix character of a direct constant marks its type.
* l/l for long, f/f for float type, d/d for double type.
* 16 binary numbers are used for all integer data types, with prefix 0x/0x followed by 0~9 and a-f representations.
* If an attempt is made to initialize a variable to a value beyond its own representation range, the compiler will report an error message as we do.
* C + + or Java, the binary number does not have a direct constant representation method, which can be achieved by using the static method Tobinarystring () of the integer and long classes.

Shift operators:
* Left move operation "<<", signed right Shift action ">>".
* Added an "unsigned" right-shift operation in Java, no matter whether the positive and negative highs are 0, this operation is not in C + +.

The operator also needs to be aware of:
* Java programmers cannot implement their own overloaded operators like C + + and C #.
* For strings, if the expression starts with a string, all subsequent operands must be of string type.

Type conversions:
* Java allows us to convert any basic data type to another base data type, except for the Boolean type.
* Boolean data does not allow any type of conversion processing at all.
* The class data type does not allow for type conversions, and special methods are required in order to convert one kind into another.
* objects can be type-cast between class families of their owning type.

Truncation and rounding of data:
* When you convert a float or double to an integer value, the number is always truncated.
* If you want to get rounded results, you need to use the round () method in Java.lang.Math.

Java does not have sizeof:
* The maximum reason for using sizeof in C/s + + is "porting".
* Java does not require the sizeof () operator to meet this need.
* Because all data types are the same size in all machines, it has been designed in the language.


control the execution process

foreach Syntax:
* Java SE5 introduces a new, more concise for syntax for arrays and containers.
* You do not have to create variables to count the sequences that consist of access items, and foreach automatically generates each item.
<pre>
float f[] = new FLOAT[10];
for (int i = 0; i <; i++)
{
F[i] = Rand.nextfloat ();
}
for (int x:f)
{
SYSTEM.OUT.PRINTLN (x);
}
</pre>

<--the remainder of the flow control statements are the same as C + + usage.

Example04. Usage of Foreach

initialization and cleanup

Constructor:
* By providing a constructor, you ensure that each object will be initialized.
* When creating an object, the constructor is automatically invoked before the user has the ability to manipulate the object, guaranteeing initialization.
* Constructors that do not receive any parameters are called default constructors.
* "Initialize" and "create" are bundled together in Java, they cannot be separated.
* constructor is a special type of method that has no return value.

Attention:
* No return value is different from null (void) return value.
* For NULL return values Although the method itself does not automatically return anything, you can still choose to have it return something else.
* The constructor does not return anything.

Overload:
* Overloading mechanism enables different implementations of the same name method.
* The premise is that each overloaded method must have a unique list of parameter types.
* The incoming data type is less than the method parameter type, and the actual data type is promoted.
* The incoming data type method is greater than the parameter type and must be type cast, or an error will be made.

Think: why is it not feasible to differentiate overloaded methods by return values?
<pre>
void F () {}
int F () {return 1;}
...
f ();
</pre>

Default constructor:
* If there is no constructor in the class, the compiler will automatically create a default constructor.
* If a constructor is already defined, the compiler does not automatically create a default constructor.

This keyword:
* Can only be used inside the method. Represents the reference to the object that called the method.
* Often used when you need to return the current object reference.
* This keyword is also useful for passing the current object to another method.
* Another use of the This keyword is to call the constructor in the constructor.

Constructor is called in the Builder:
* In the constructor, if you add a parameter list to this, you have a different meaning.
* This will result in an explicit call to a constructor that conforms to this parameter list.
* This usage, although it is possible to call a constructor with this, cannot call two.
* In addition, you must place the constructor call at the beginning, or the compilation will error.

Example05. Using this in Constructor

Finalize Method:
* Java allows you to define a method named Finalize () in a class.
* The operating principle is assumed to be:
* * Once the garbage collector is ready to release the storage space occupied by the object, its finalize () method is called first.
* * And when the next garbage collection action occurs, the memory occupied by the object is actually reclaimed.
* The need for finalize methods is often due to allocating heap memory in a way other than new when allocating memory.
* While creating a local object is not allowed in Java, you must use new to create the object.
* So the allocation method outside of new is mainly in the case of using the "local method". Call non-Java code in Java to allocate memory.
* Finalize another usage is validation of the terminating object condition.

Member Initialization:
* Java does its best to ensure that all variables are initialized before they can be used.
* For local variables, Java implements this guarantee in the form of compile-time errors.
* Java can assign values to members where class member variables are defined. (c + + is only supported in C + + 11)

Order of initialization:
* Initialization order is a static object and then a non-static object.
* Inside the class, the order in which the variables are defined determines the order in which they are initialized.
* Even if the variable definition is scattered between the method definitions, it will be initialized before any methods including the constructor are called.
* Static member initialization occurs only when the first object is created or first accesses static data, and thereafter is not initialized again.

Explicit initialization:
* Java allows multiple static initialization actions to be organized into a special static clause.
* Enclosed in curly braces followed by the static keyword.
* This code is also executed only once when the class object is first generated or when the class static data member is accessed for the first time.
* For non-static variables you can also use a block of statements in curly braces, without the static keyword, to be executed when the object is created.

Example06. Order of initialization

Array:
* To define an array, simply add square brackets after the type name. Example: int[] A; or int a[];
* All arrays have another intrinsic member length, the number of elements within the record group, and cannot be changed.
* Array access subscript range 1~length-1, run-time error occurs directly beyond the boundary.
* Basic data types in array elements are automatically initialized to null values.
* You can initialize an array of objects with a list of curly braces.

When creating an array, be aware that:
* If you create an array of non-primitive types, you are actually creating a reference array.
* After creation, initialization must be completed by creating a new object and assigning the object to the reference.
* If you forget to create an object and try to use a null reference in the array, an exception will be generated at run time.

<--the array out-of-bounds is silently accepted, allowing access to any memory, resulting in many errors.

Java provides a variable parameter list for ease of use
<pre>
static void PrintArray (Object ... args)
{
for (Object Obj:args)
System.out.print (obj + "");
System.out.println ();
}
</pre>
Any type of parameter can be used in a mutable parameter list, including the base type.

Enum variable enum is provided in <--Java, which is similar to that in C + +, slightly.

Java Programming Ideas (1)

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.