Basic Java Basics Types and operations "important"

Source: Internet
Author: User

0. Java Basic data Type

The bitwise operations of Java (bitwise operators) operate directly on bits of integer types, including long,int, short,Char and byte, the bitwise operator is the following table:

Operator

Description

<<

Left shift, 0 in low position

>>

Right SHIFT, if a positive number is high 0, if negative, high 1

>>>

Unsigned right shift, either positive or negative in the high 0

&

With (and), a Boolean algebra is executed for the corresponding bit in the two integer operands, and two bits are 1 o'clock output 1, otherwise 0.

|

or (or), a Boolean algebra is executed for the corresponding bit in the two integer operands, and two bits are 0 o'clock output 0, otherwise 1.

~

Non (not), unary operator.

^

XOR (XOR), performs a Boolean algebra on the corresponding bit in two integer operands, two bits equal 0, unequal to 1.

<<=

Left shift assignment.

>>=

Right shift assignment.

>>>=

Unsigned right shift assignment.

&=

Bitwise AND Assignment.

|=

Bitwise OR assignment.

^=

Bitwise XOR or assignment.

Java provides a total of 8 primitive data types (byte, short, int, long, float, double, char, Boolean), which are not objects, but rather special types in Java that differ from classes. These basic types of data variables are immediately allocated space on the stack after they are declared. In addition to these basic types, other types are reference types, which are declared without allocating memory space, but storing only one memory address.



Attention:

1) in Java, the default declaration of decimals is a double type, and an integer by default is an int type.

2) in Java,null is not a valid object instance , so the compiler does not allocate memory for it, it is only used to indicate that the reference is not currently pointing to any object. Null is to place the value of the reference variable all 0.

1. Immutable classes

Http://www.cnblogs.com/qlqwjy/p/7944456.html

Immutable Classes : The so-called immutable class refers to an instance of this class that, once created, cannot change its member variable values. There are many immutable classes inside the JDK: Interger, long, and string.
mutable classes: In contrast to immutable classes, variable classes can change the value of their member variables after an instance is created, and most of the classes created in development belong to mutable classes.

The following comprehension may be understandable:

{concept: Immutable class means that after an instance of the class is created, the properties of the instance are immutable. The 8 wrapper classes and string classes in Java are immutable classes. So immutable classes do not mean that the class is final decorated, but that the properties of the class are final decorated.

Custom immutable classes adhere to the following principles:

1. Use the private and final modifiers to decorate the properties of the class.

2. Provides a constructor with parameters for initializing properties based on the parameters passed in.

3. Only provide getter methods for this class of attributes, do not provide setter methods.

4. If necessary, rewrite the hashcode and equals methods, while guaranteeing two objects that are judged equal by the Equals method, and their hashcode should also be equal. }

2. Difference between value passing and reference passing

1) Value Pass: When the method is called, the actual parameter passes its value to the corresponding formal parameter, the formal parameter only initializes its own storage unit content with the value of the actual parameter, and is two different storage units, so the change of the formal parameter value in the method execution does not affect the value of the actual parameter.

2) Reference passing: Also known as a pass-through address. When a method is called, the actual parameter is an object (or an array), then the actual parameter and the formal parameter point to the same address, in the method execution, the operation of the formal parameter is actually the operation of the actual parameters, so the change of the form parameter in the execution of the method will affect the actual parameters.

Attention:

In Java, the original data type is passed by value when the parameter is passed, and the wrapper type is passed by reference in the pass parameter, but the wrapper type is automatically removed when the calculation occurs.

Reference: JDK1.5 new features

3. Conversion rules for different data types

A type conversion (explicit or implicit) is required when the type of two objects participating in the operation is different, and the implicit conversion rule is from low precision to high precision, byte

Automatic type conversion

Low-level data can be automatically converted to advanced data types.
1. When char is converted to an advanced type, such as int or long, it is automatically converted to its corresponding ASCII code.
2. byte char short is automatically converted to int when participating in the operation, but does not occur when using the + = Operation
3. There is no conversion between the base data type and the Boolean
When mixing multiple types of data, it is automatically converted to the most advanced type for calculation.

Forcing type conversions

Forced type conversions are required when converting from an advanced data type to a low-level data type
BYTE--Char
Char, Byte Char
Short, Byte Char
int, byte short char
Long-byte short char int
Float--byte short char int long
Double-byte short char int long double

Forced Conversions Note:
For example, short s = 1; s = s + 1; Since S + 1 will be forced to convert to int for calculation, the assignment (s = s+1) when the int type is assigned to short, it will be an error (cant conver XXX). So you need to explicitly convert s= (short) (s+1);

But since the + = operator is an algorithm in the Java language, S+=1 does not error.


4. Precedence of Operators
    public class test{             publicstatic void Main (string[] args) {                    Bytea = 5;                    INTB = ten;                    INTC = a >> 2 + b >> 2;                    System.out.println (c);             }      }  

The answer is 0 because the "+" has a higher priority than ">>", so the expression in the program is equivalent to a>> (2 + b) >>2.

5. Move right shift left

1) Shift Left (<<): The binary number of the object that participates in the operation is shifted to the left by the specified number of digits, the right bit 0, and the overflow is discarded after the high-left shift. The left-shift N-bit indicates that the original value is multiplied by 2 of the N-square, often used instead of multiplication, because the CPU directly supports bit operations, so the bit operation is more efficient than the multiplication operation.

2) Right shift is divided into signed right shift (>>) and unsigned Right shift (>>>). The difference between the two is that ">>" when performing a right-shift operation, if the number of participants in the operation is positive, then 0 in the high position, or a negative number, the high 1. and ">>>" is different, regardless of the numbers of the participating bit operations are positive or negative, in the execution of the operation, Will fill 0 in the high position.

Attention:

1) before shifting the number of char, Byte, and short, the compiler automatically converts the value to the int type before the shift operation.

2) in Java, in order to ensure the validity of the shift, the use of the remainder of the operation, that is a>>n equivalent to a>> (n%32).

6. Char-type variable stores Chinese characters

In Java, the default Unicode encoding, which takes two bytes per character, can be used to store Chinese. Although the string is made up of char, it uses a more flexible way to store, that is, English occupies a character, Chinese occupies two characters , the use of this storage is an important role is to reduce the required storage space, improve storage efficiency.

    Import Java.util.regex.Matcher;      Import Java.util.regex.Pattern;             Public Classtest {public                 static voidjudgechinesecharactor (String str) {            stringregex= "[\u4e00-\u9fa5]";            Determines if there is a Chinese character            if (Str.getbytes (). Length = = Str.length ()) {  //A Kanji account of two bytes               System.out.println ("no kanji");            } else{               PATTERNP = Pattern.compile (regEx);               Matcherm = P.matcher (str);               while (M.find ()) {                  System.out.print (m.group (0) + "");                }}} public static void Main (string[] args) {            judgechinesecharactor ("Hello World");            Judgechinesecharactor ("Hello hi");         }             }  

In the above example, the string length and string length are used first to determine whether the strings contain Chinese characters and, if they are included, to find all the Chinese characters in the string in the same way that the regular expression matches

Basic Java Basics Types and operations "important"

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.