Java Study Notes-Basic Types and computing priorities

Source: Internet
Author: User

Java Study Notes-Basic Types and computing priorities
1. Java provides eight types of raw data. The comparison is as follows:


 

Data Type Byte Length Range Default Value Packaging
Int 4 [-2147483648,2147483647]
(-2 ^ 31 ~ 2 ^ 31-1)
0 Integer
Short 2 [-32768,32767] 0 Short
Long 8 [-9223372042554775808,9223372010954775807]
(-2 ^ 63 ~ 2 ^ 63-1)
0L or 0l Long
Byte 1 [-128,127] 0 Byte
Float 4 32-bit IEEE754 single precision range 0.0F or 0.0f Float
Double 8 64-bit IEEE754 dual-precision range 0.0 Double
Char 2 Unicode [0,65535] U000 Character
Boolean 1 True and false False Boolean
The above basic types can be divided into the following four types:

 

 

Int length data types: byte (8bit), short (16bit), int (32bit), long (64bit) float length data types: single precision (32bit float), double precision (64bit double) values of boolean variables: true and falsechar; Data Type: unicode character (16 bit); example:

 

1. What the following expressions strive for is ()

A. byte B = 128; B. boolean flag = null; C. float f = 0.9239; D. a = 2147483648L;

The answer is that D.C is incorrect because it may cause loss of precision. Because 0.9239 is double type. The correct value should be 0.9239f.

2. Is String a basic data type?

No. See the table above.

3. Is the assignment statement float f = 3.4 correct?

The answer is incorrect. For the reason, see question 1st.

 

2. Immutable class.

First look at the code segment:

 

class ImmutableClass{private Date d;public ImmutableClass(Date d){this.d=d;}public void printState(){System.out.println(d);}}public class Test{public static void main(String args[]){Date d= new Date();ImmutableClass im = new ImmutableClass(d);im.printState();d.setMonth(5);im.printState();}}


 

Running result:

Sun Aug 03 17:41:47 CST 2015

Tue Jun 04 17:41:47 CST 2015

Obviously this is not the result we want. What should the correct code look like? As follows:

 

 

Class ImmutableClass {private Date d; public ImmutableClass (Date d) {this. d = (Date) d. clone (); // removed the reference link} public void printState () {System. out. println (d);} public Date getDate () {return (Date) d. clone () ;}} public class Test {public static void main (String args []) {Date d = new Date (); ImmutableClass im = new ImmutableClass (d); im. printState (); d. setMonth (5); im. printState ();}}


 

 

Running result:

Sun Aug 03 17:41:47 CST 2015

Sun Aug 03 17:41:47 CST 2015

The application of the object causes the status change of the above Code. Pay attention to these issues during design.

Iii. Operator priority

 

Priority Operator Associativity
1 . () [] From left to right
2 + (Positive)-(negative) ++ --~ ! From left to right
3 */% From left to right
4 + (Plus)-(minus) From left to right
5 <> (Shifts right without a symbol)> (shifts right with a symbol) From left to right
6 <<=>> = Instanceof From left to right
7 =! = From left to right
8 & From left to right
9 | From left to right
10 ^ From left to right
11 && From left to right
12 | From left to right
13 ? : From left to right
14 = + =-= * =/= % = & = | = ^ = ~ = <<=>=>>>>= From left to right
Example:

 

 

public class Test{public static void main(String args[]){byte a=5;int b=10;int c=a>>2+b>>2;System.out.println(c);}}


 

Answer: 0, equivalent to a> 12> 2. The value of a shifted to 12 is obviously 0, and the value of 2 to the right is still 0.

4. Math Functions

 

Math. floor (): rounded down. Example: Math. the value of floor (5.8) is 5. math. ceil: rounded up. Example: Math. the value of floor (5.3) is 6; Math. round (): rounding, for example, Math. the value of round (5.4) is 5;

 

V. What is the difference between =, equals, and hashCode?

 

= The operator compares the values of two variables to see if they are equal. Like the = Operator, equals compares references. However, the equals method can be overwritten. That is, you can override the equals method of the object. The hashCode () method returns an int value converted from the address in the memory of the object. Therefore, if this method is not rewritten, The hashCode () method of all objects will not wait.

 

 

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.