Java basics that are usually easy to ignore

Source: Internet
Author: User
Tags bitwise

Usually we study may simply press the book to hit the code, perhaps just wait for the teacher to instill knowledge of the habit, often is the case or the teacher's case copy over the book or re-knock code, see the effect, oh,nice! Then no matter how the program is running, without a deep understanding of the meaning of each code, understand the program compiled environment. Although Java is an object-oriented language, there are a number of methods that are not normally used to consult the JDK help documentation, but we have to learn its most basic syntax, such as various basic data types, operators, to control the various uses of the process statements. More importantly, the core concepts of Java, class inheritance, abstract classes, inner classes, methods of overwriting and overloading, interface implementation. So here's to get you to know the details.

Let's start with an operator that's easy to ignore:& and &&.

    & and && Can be used as the logical AND operator, representing logic and (and), when the result of an expression on either side of the operator is true, the result of the operation is false, the result is false.    && also features short-circuit That is, if the first expression is false, the second expression is no longer evaluated, for example, for if (str! = null &&!str.equals ("")) Expression, when str is null, subsequent expressions do not execute, so && is changed to &, then if (x==33 & ++y>0) y will grow, if (x==33 && ++y>0) will not grow.

& can also be used as a bitwise operator, when an expression on either side of the & operator is not a Boolean,& represents bitwise AND operation, we typically use 0x0f to perform a & operation with an integer , To get the minimum 4 bit bits of the integer , for example,0x31 & 0x0f result is 0x01.

In addition to the confusion of the basic types:int and integer.

int is java provides java provides the encapsulation class for each primitive type, integer is java to int default value is 0, while integer The default value is null, that is integer can distinguish between unassigned and the value of 0, int is unable to express the case of unassigned, for example, to express the failure to take the exam and test scores for 0, you can only use integer. In jsp development, integer The default is null, so When the El expression is displayed in a text box, the value is a blank string, and the default value of int is 0,                 

So when the El expression is displayed in a text box, the result is 0, soint does not work as a type of form data for the Web tier.

In hibernate, if the OID is defined as an integer type, hibernate can determine whether an object is temporary based on whether its value is null, or if the OID is defined in order to int type, you also need to set its Unsaved-value property to 0 in the HBM mapping file .

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

Confusion about the "= =" and equal methods:

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 (); the variable obj is a memory,new Object () is another memory, at which point the value stored in memory for the variable obj is the first address of the 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 statements create two objects, and then use a, B, to point to one of the objects, which is two different objects whose first address is different, that is, the values stored in a and B are not the same, so the expression a==b will return false, and the contents of the two objects are the same, so the expression a.equals (b) returns 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 method of the object class , and theimplementation code for the equals method of the object class 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, and if the object that the two variable points to is the same object, use the equals and use = = 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 write your own code to determine at what time that the contents of the two objects are the same.

in the end, I personally think there's one more easy to overlook: 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, only one Staticvar variable is always assigned, and each instance object is created, the Staticvar adds 1; However, each instance object is created with a Instancevar, that is, multiple instancevar may be assigned , and each Instancevar value is only 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);

}

Java basics that are usually easy to ignore

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.