Java Basic Small Problems collection

Source: Internet
Author: User

1, can I store a Chinese character inchar type ?  Why not ?

char-type variables are used to store Unicode encoded characters, and theUnicode encoding character set contains Chinese characters, so thechar type can of course store Chinese characters. However, if a particular kanji is not included in the Unicode encoding character set,

In this case, this char type variable cannot store this special character. Supplemental Note:Unicode encoding consumes two bytes, sovariables of type char are also two bytes.

2, self-increment self-reduction problem

int x = 4;
5 + 5 + 60
The self-increment self-reduction operation is followed by the a=a++; the first operation (Assignment) is then self-built.
The self-increment self-built operation is in front of the operation, namely A=--a; First self-decrement and re-operation (Assignment)

3, shorts1 = 1; s1 = s1 + 1; What's wrong? ? short S1 = 1; s1 + = 1; What's wrong?  ?

For short s1 = 1; s1 = s1 + 1; Because the type of the expression is automatically promoted by the s1+1 operation, the result is type int, and then to the short type S1, the compiler reports an error that requires a cast type

For short s1 = 1; s1 + = 1; since + = is a Java-language-defined operator, theJava compiler will treat it specially, so it can compile correctly.

the correct wording is:

s = (byte) (s+1);

4, two types of casts

float F = 12.3f;
Long g = 12345;
f = g; //Implicit conversion
g = (long) F; //Forced conversion

5,byte cross-border problem, byte B = 130 and Byte B = 300 is correct?

byte B = 130;
Because the range of byte is:-128 to 127. ( -2^7--2^7-1), and 130 is not within this range, so error.
We can use forced type conversions
byte B = (byte) (126 + 4);
System.out.println (b); -126
byte b2 = (byte) 300;
System.out.println (B2); 44

Gets the binary of this data 130. 00000000 00000000 00000000 10000010 This is the original code of 130, is also anti-code, or complement.




Complement: 1 0000010
Anti-code: 1 0000001
Original code: 1 1111110

6, two methods of preserving decimals

System.out.println ( -10/3.0);
Keep 3 as Decimal, Math.Round
System.out.println (New DecimalFormat ("0.00"). Format ( -10/3.00)); //Reserved 2 decimal places, Decimalformat.format ()

7, Evaluation-3 5

System.out.println ( -3 5);//-3

8, do not introduce a 3rd value or variable, Exchange 2 data values

int x = 10;int y = 5;
x = x ^ y; 10 ^ 5
y = x ^ y; Ten ^ 5 ^ 5 y = 10
x = x ^ y; Ten ^ 5 ^ ten x = 5
System.out.println ("x =" + x + ", y =" + y ");

9, the most efficient calculation of 2 of the 3-time Square

System.out.println (2<<3);
Shift a number to the left N-bit, which is equivalent to multiplying by 2 of the N-square, then, a number multiplied by 8 to move it to the left 3-bit, and bit computing CPU directly support, the most efficient

10, the difference between "= =" and equal

the "= =" operator is specifically used to compare the values of two variables, that is, whether the value stored in the memory used to compare variables is the same, to compare two basic types of data or two reference variables are 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 returns false, and the contents of the two objects are the same, so the expression a.equals (b) returns true. Remember that the comparison of strings is basically using the equals method .

The difference between an Integer and an int

int is one of the 8 raw data types provided by Java. Java provides encapsulation classes for each primitive type, andinteger is the wrapper class provided by Java for Int.

The default value for int is 0, and the default value for integer is null, that is, the difference between an unassigned and a value of 0 can be distinguished by an int, which cannot express an unassigned condition, for example, To express the difference between not taking the test and the exam score of 0, you can only use Integer.

< Span lang= "en-US" > 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, while the default value of int is 0,  So when the el expression is displayed in a text box, the result is 0, int does not work together as a type of form data for web layers.

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.

Java Basic Small Problems collection

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.