Java Core Technology 3rd Chapter

Source: Internet
Author: User

Chapter III:

The main method must be public

There are eight basic types:

Java Shaping:

Type Storage requirements Range of values
Int 4 bytes -2147483648~2147483647 (just over 2 billion)
Short 2 bytes -32768~32767
Long 8 bytes -9223372036854775808~9223372036854775807
Byte 1 bytes -128~127

Java shaping range of values and machines

The long shape ends with an L, hexadecimal is prefixed with 0x, octal is prefixed with 0, and after Java7, the number prefixed with 0b is the binary number

No unsigned type in Java unsigned

Java floating point type:

Type Storage requirements Range of values
Float 4 bytes Approx. ±3.40282347e+38f (6~7 bit of significant digits)
Double 8 bytes Approx. ±1.79769313486231570e+308 (15-bit significant number of digits)

A double indicates that the numeric precision of this type is twice times that of float. Most applications use double

There is a suffix f (3.14F) after the number of float types, and no suffix or postfix is D for the double type.

Char type:

The char type is used to represent a single character and is typically used to represent a character constant. For example: ' A ' is a character constant encoded as 65.

In the Unicode standard, code points are written in 16 and prefixed with u+. Example: u+0041 represents the code point for the letter A.

A Unicode code point is divided into 17 code levels. The first code level is called the basic multilanguage level, code points from u+0000 to U+ffff, the remaining 16 additional levels, code points from u+10000 to U+10FFF, including some auxiliary characters.

Java uses UTF-16. In the basic multi-lingual level, each character is represented by 16 bits, often called a code unit. The auxiliary characters are encoded with a pair of contiguous code units. The encoded values that make up must fall within the 2048 bytes of the basic multilingual level, often referred to as an alternate region. You can use this method to quickly know whether a character is encoded or a first or second part of a secondary character.

Boolean type:

The Boolean type has two values: false and True. An integer value and a Boolean value cannot be converted to each other.

Variable:

After declaring a variable, the variable must be displayed initialized with an assignment statement, never using a variable that is not initialized.

Constant:

In Java, the keyword final is used to indicate constants.

The keyword final indicates that the variable can only be assigned once and cannot be changed once it has been assigned. In practice, the constant name uses uppercase.

Operator:


The virtual machine Designer allows the intermediate calculation results to be extended precision. However, methods that use the STRICTFP keyword notation must use strict floating-point calculations to produce the desired results. For example:

public static strictfp void Main (string[] args)

Bitwise operators:

The argument to the right of the shift operator requires a modulo 32 operation (unless the left operand is a long type, the modulo 64 operation is required). For example: 1<<35 and 1<<3 or 8 are the same.

Conversions between numeric types:

Char

                           |

Byte--short--int--long

Float--double

No precision loss for the above conversions

Several other conversions:

int or long turn float, may have loss of precision

int to double no precision loss

Long to double possible loss of precision

The above conversions can be done automatically

To force type conversions:

If necessary, int can be automatically converted to double, and if the double needs to be converted to int it is necessary to implement this operation by forcing type conversion (CAST). Some information may be lost.

Example: Double x = 90997; int NX = (int) x;

If you want to round out floating-point numbers, you need to use the Math.Round method

Double x = 9.997;

int NX = (int) math.round (x);

Because Math.Round () returns a long type, a long conversion to an int requires casting.

Casts are truncated if they are out of range:

Example: The actual value of (byte) 300 is 44.

string:

The string class does not provide a way to modify the string.

String greeting = "Hello";

Modify greeting to "help!"

Greeting = greeting.substring (0, 3) + "p!";

Detect if strings are equal:

S.equals (t)

Returns true if S equals T, otherwise false.

S,t can be either a string variable or a string constant. such as: "help!". Equals (greeting)

Case-insensitive use: equalsignorecase.

equals is used to detect whether the contents of two strings are the same, = = is used to determine if two strings are in the same position and returns true if placed in the same position.

Input:

Scanner in = new Scanner (system.in);

Reads a line : String name = In.nextline ();

Read a word: String firstName = In.next ();

Reads an integer: int age = In.nextint ();

JAVA6: Read password

Console cons = System.Console ();

String username = cons.readline ("User name:");

char[] passwd = cons.readpasswd ("passwd");

Formatted output:

System.out.printf ("%8.2f", x) 8 character width, 2 decimal places after the decimal point. The number of digits is insufficient and the left side is blank.

Block scope:

A variable with the same name cannot be declared in a nested two block. A domain variable of a class can have the same name as a variable in a method block.

Large value:

If the basic integer and floating-point precision does not meet the requirements, you can use two useful classes in the Java.math package: BigInteger and BigDecimal. These two classes can handle numeric values that contain a sequence of arbitrary lengths.

Use the static method valueof to implement the transformation:

BigInteger a = biginteger.valueof (100);

You cannot use the usual arithmetic operators for large numeric operations. Instead, you need to use the methods provided in the large values:

BigInteger C = A.add (b);

Bigiteger d = c.mutiply (B.add (biginteger.valueof (2)));

Division is: divide

Modulo as: MoD

These methods return the BigInteger type.

Array:


Declaring an array:

Int[] A;

int a[];

Java is accustomed to the first kind

Int[] A = new int[100];

When you create a numeric array, all the elements are initialized to 0. The elements of the Boolean array are initialized to false. The elements of the object array are initialized to a special value of NULL.

string[] names = new STRING[10];

For Each loop:

For Each loop is easier to use.

for (variable:collection) statement

Cases:

for (int element:a)

SYSTEM.OUT.PRINTLN (Element);

A more concise way to print array elements:

System.out.println (Arrays.tostring (a));

Array initialization and anonymous arrays:

Int[] Smallpimes = {2, 3, 5, 7, 11, 13};

Initialize an anonymous array:

New Int[]{17, 19, 23, 29, 31, 37}

Array size is the size of the initial value

Smallprimes = new int[]{17, 19, 23, 29, 31, 37};

Equivalent to:

int[] Anonymous = {17, 19, 23, 29, 31, 37};

Smallprimes = anonymous;

Array copy:

In Java, an array variable is allowed to be copied to another array variable. At this point, two variables will reference the same array:

int[] Luckynumbers = smallprimes;

LUCKYNUMBERS[5] = 12;

Tell an array all the values are copied into a new array:

int[] Copiedluckynumbers = arrays.copyof (luckynumbers, luckynumbers.length); The second parameter is the length of the new array. This method is usually used to increase the size of the array:

Luckynumbers = arrays.copyof (luckynumbers, 2*luckynumbers.length);

If the array element is numeric, the extra element is assigned a value of 0. If it is a Boolean, it will be assigned a value of false. If the length is less than the original array length, only the front-most array elements are copied.

Array sorting:

Int[] A = int[1000];

Arrays.sort (a);

Multidimensional Arrays:

For each cannot automatically process each element of a two-dimensional array. To access all the elements of a two-dimensional array A, you need to use two nested loops.

Java does not actually have multidimensional arrays, only one-dimensional arrays. Multidimensional arrays are interpreted as "arrays of arrays." “

Cases:

Int[][] odds = new int[max+1][];

for (int n = 0; n <= MAX; n++)

Odds[n] = new int[n+1];

Java Core Technology 3rd Chapter

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.