Basic syntax for Java _3

Source: Internet
Author: User

1, usingFinalkeyword retouchingA variable, is the reference immutable, or does the referenced object not change?

When you use the final keyword to decorate a variable, it means that the reference variable cannot be changed, and the contents of the object to which the reference variable is pointing can be changed. For example, for the following statement:

Final StringBuffer a=new StringBuffer ("immutable");
Executing the following statement will report a compile-time error:

A=new StringBuffer ("");
However, the following statements can be executed by compiling:

A.append ("broken!");

When someone defines a method's parameters, you might want to block the method from modifying the passed-in Parameter object in the following form:

public void method (final stringbuffer param)

{

}

In fact, this cannot be done, and the following code can still be added inside the method to modify the Parameter object:

Param.append ("a");

2,"=="and theequalsWhat is the difference between the methods?

(Make one thing clear, and then another, so that the difference will come out naturally, and it's hard to say if you mix it up)

the = = operator is specifically used to compare the values of two variables, that is, whether the value stored in the memory used to compare the variables is the same, to compare two basic types of data or two reference variables equal, only with the = = operator.

If the data pointed to by a variable is of the object type, then two blocks of memory are involved, the object itself occupies a chunk of memory (heap memory), and the variable occupies a chunk of memory, such as Objet obj = new Object (); variables obj is a memory, new Object () is another memory, at this point, the variable obj The value stored in the corresponding memory is the first address of the block of memory that the object occupies. For variables that point to the object type, if you want to compare whether the two variables point to the same object, that is, to see if the values in memory for the two variables are equal, then you need to compare them with the = = operator .

The Equals method is used to compare the contents of two separate objects, as compared to two people whose looks are the same, compared to the two objects that are independent of each other. For example, for the following code:

String A=new string ("foo");

String B=new string ("foo");

Two new statement creates two objects and then uses Span style= "Font-family:times New Roman" >a,b a The values stored in span style= "Font-family:times New Roman" >b are not the same, so the expression A==b false , and the contents of these two objects are the same, so the expression Span style= "Font-family:times New Roman" >a.equals (b) will return true .

In real-world development, we often have to compare whether the string content passed in is, for example, stringinput = ...; Input.equals ("Quit"), many people do not pay attention to use = = To compare, this is wrong, casually from the Internet to find a few project real-life teaching video to see, there are a lot of such errors. Remember that the comparison of strings is basically using the equals method.

If a class does not define its own equals method, it inherits the equals methodof the object class , the object class's equals The implementation code for the method is as follows:

Boolean equals (Object o) {

return this==o;

}

This means that if a class does not have its own definition of the Equals method, its default equals Method (inherited from the Object Class) is using the = = operator. It is also in comparison to whether the object pointed to by two variables is the same object, when using equals and using = = will get the same result , if the comparison is two separate objects will always return False. If you are writing a class that wants to compare the contents of the two instance objects created by the class, then you must override the equals method, and you write your own code to determine in what case that the contents of the two objects are the same.

3, the difference between a static variable and an instance variable?

The difference between the syntax definitions: Static variables are added with the static keyword, and the instance variable is not added before.

The difference when the program runs: instance variables belong to an object's properties, and an instance object must be created where the instance variable is allocated space before the instance variable can be used. Static variables are not part of an instance object, but belong to a class, so also known as class variables, as long as the program loads the class's bytecode, without creating any instance objects, static variables will be allocated space, static variables can be used. In summary, an instance variable must be created before it can be used by the object, and a static variable can be referenced directly using the class name.

For example, for the following program, no matter how many instance objects are created, there is always only one staticvar variable assigned, and each instance object created, this staticvar will add 1; However, each instance object is created with a Instancevarthat may be assigned multiple instancevar, and each Instancevar the values are only self-added. 1 times.

public class Varianttest

{

public static int staticvar = 0;

public int instancevar = 0;

Public Varianttest ()

{

staticvar++;

instancevar++;

System.out.println ("staticvar=" + Staticvar + ", instancevar=" + Instancevar);

}

}

Note: This solution in addition to clear the difference between the two, and finally with a specific application examples to illustrate the difference between the two, reflecting their own good explanation of the problem and the ability to design cases, quick thinking, more than the general programmer, have the ability to write!

4, whether it can be from aStaticthe method internally issues a non-Staticmethod is called?

No. Because the non- static method is to be associated with an object, you must create an object before you can make a method call on the object, and the static method call does not need to create the object, which can be called directly. That is, when a static method is called, there may not be any instance objects created, if The non- static is emitted from a static method method call, the non- Static which object does the method relate to? This logic cannot be set up, so a static method is issued inside a call to a non- static method.

5,Integerwith theintthe Difference

Intis aJavaprovided by8one of the original data types. Javaprovides a wrapper class for each primitive type,Integeris aJavato beintthe package class provided. intThe default value is0, whileIntegerThe default value isNULL, i.e.Integeryou can distinguish between unassigned values and values of0the difference,intIt is not possible to express an unassigned condition, for example, to express a failure to take the exam and test scores as0the difference, you can only use theInteger. In theJSPin development,Integerthe default isNULL, so withElWhen an expression is displayed in a text box, the value is a blank string, and theintThe default default value is0, so withElWhen an expression is displayed in a text box, the result is0, So,intnot cooperating forWebThe type of form data for the layer.

In hibernate oid defined as integer hibernate can be based on whether its value is null oid definition for int HBM mapping file set its unsaved-value 0

In addition , the integer provides multiple integer-related action methods, such as converting a string to an integer, and also defining constants that represent the maximum and minimum values of integers.

6,Math.Round (11.5)So how much?? Math.Round ( -11.5)So how much??

MathThe three methods that are related to rounding are provided in a class:Ceil, Floor,round, the effects of these methods correspond to the meanings of their English names, for example,Ceilthe meaning of the English is the ceiling, the method means to take up the whole, so,Math.ceil (11.3)The result is12,math.ceil ( -11.3)The result is-11; Floorthe meaning of the English is the floor, the method means to take the whole down, so,Math.floor (11.6)The result is11,math.floor ( -11.6)The result is-12The hardest thing to master isroundmethod, which represents "rounding", the algorithm isMath.floor (x+0.5), the original number is added0.5And then rounded down, so,Math.Round (11.5)The result is A,Math.Round ( -11.5)The result is-11.



Basic syntax for Java _3

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.