Java basics

Source: Internet
Author: User
Tags array definition reserved intel pentium

Symbol type:
1. In C and C ++, the integer indicated by int is related to the target machine. The 16-bit cpu occupies 2 bytes, and the 32-bit cpu occupies 4 bytes. On the intel pentium cpu,
C and C ++ integers depend on specific operating systems. For DOS and Windows3.1, integers occupy 2 bytes. When Windows uses the 32-bit mode, integers occupy 4 bytes.

Java has no unsigned type.

2. The float type value has a suffix F. No floating point value is of the double type by default (you can also add the suffix D to indicate differentiation ).

1): This method is convenient and I will use this method.

The code is as follows: Copy code
Float a = 123.2334f;
Float B = (float) (Math. round (a * 100)/100 ;(

Here 100 is a two-digit decimal point. If you want another digit, for example, four digits, here two 100 digits are changed to 10000)

(2): This method is also simple, but it must be converted to the float type:

The code is as follows: Copy code
Import java. text. DecimalFormat;
String a = new DecimalFormat ("####,########"). format (100.12345 );

(3): This can also be used.

The code is as follows: Copy code

Float ft = 134.3435f;
Int scale = 2; // set the number of digits
Int roundingMode = 4; // indicates rounding. You can select other value-based methods, such as tail and so on.
BigDecimal bd = new BigDecimal (double) ft );
Bd = bd. setScale (scale, roundingMode );
Ft = bd. floatValue ();

3. char uses UTF-16 encoding, occupies 2 bytes, it is recommended not to use the char type in the program
   
Code point:

The code points between U + 0000 and U + FFFF are represented by u0000 to uffff.
Between U + 10000 and U + 1FFFF, ud800 to udbff is used as the first unit, and udc00 to udfff is used as the second unit.

Char refers to u0000 to uffff, which occupies two bytes.
The rest use the concept of code point.

IsJavaIdentifierStart and isJavaIdentifierPart may be used as java characters for confirmation.


The code value corresponding to a character in an encoding table. In the Unicode standard, the code points are represented in hexadecimal notation, and the prefix U + is added. For example, U + 0041 indicates the code point of the letter.
Unicode code points are divided into 17 code levels. The basic multi-language level (BMP): U + 0000 ~ U + FFFF (including the classic Unicode code), each character is represented by 16 bits, called
Code unit; remaining 16 additional levels: U + 10000 ~ U + 10 FFFF (including some auxiliary characters), using a pair of code unit encoding (the first code unit: U + D800 ~ U + DBFF,
Second code unit: U + DC00 ~ U + DFFF). For example, for the mathematical symbols of an integer set, the code point is U + 1D56B, and two code units U + D835 and U + DD6B are used.
Encoding.


Type conversion:

The following figure shows a valid type conversion. The three virtual arrows indicate that conversion may result in loss of precision.
     


 

1. When two values are binary, one is double, float, and long, the other is automatically upgraded to the corresponding type; other types are upgraded to int type;

2. If you want to round the floating point to get the nearest integer, you should use the Math. round method:

The code is as follows: Copy code
Double x = 9.99;
Int nx = (int) Math. round (x );

Can it be rounded in? Of course we can. In our habits, we will think like this, but rounding means an error, and commercial operations may mean an error. At the same time, Java does not provide a rounding method that retains the specified number of digits, only one Math is provided. round (double d) and Math. the round (float f) method returns the long integer and integer values respectively. The round method cannot be set to retain a few decimal places. We can only keep two digits as follows ):

The code is as follows: Copy code

Public double round (double value ){

Return Math. round (value * 100)/100.0;

}

But unfortunately, the above code does not work normally. If you pass 4.015 to this method, it will return 4.01 instead of 4.02, as shown in the preceding figure.

The code is as follows: Copy code

4.015*100 = 401.49999999999994

Therefore, this method cannot meet our requirements for accurate rounding.

Another method is to use java. text. decimalFormat, but there are also problems. The format adopts the ROUND_HALF_DOWN round mode (the round mode is described below). For example, if 4.025 is reserved, the two decimal places will be 4.02, because. 025 distance from "nearest neighbor "(. 02 and. 03) the length is equal, and the rounding down is. 02. If it is 4.0251, the two decimal places reserved are 4.03.

The code is as follows: Copy code

System. out. println (new java. text. DecimalFormat ("0.00"). format (4.025 ));

System. out. println (new java. text. DecimalFormat ("0.00"). format (4.0251 ));

The output is

4.02

4.03

 

String: the meaning of an immutable String: a character in a String cannot be modified, but can be spliced or operated (in fact, StringBuild is also called ).
Strings are constants, and their values cannot be changed after creation. The string buffer supports variable strings. Because String objects are unchangeable, they can be shared. For example:
 
 
    

The code is as follows: Copy code
String str = "abc ";

It is equivalent:
 
 

The code is as follows: Copy code
Char data [] = {'A', 'B', 'C '};
String str = new String (data );

The following provides more examples of how to use strings:
 
 
   

The code is as follows: Copy code
System. out. println ("abc ");
String cde = "cde ";
System. out. println ("abc" + cde );
String c = "abc". substring (2, 3 );
String d = cde. substring (1, 2 );


Advantage: the compiler can share strings.
Note: The java string is not a C character array, but is more like a char * pointer.
 

= Differences from the equals method:
= You can only determine whether two strings are placed in the same location (address), while equals checks whether the values (content) of the two strings are equal. Only string constants are shared,
The result of the + or substring operation is not shared.


A variable cannot be nested in java.


Multi-dimensional array: Array (irregular)
Variable parameters:

The code is as follows: Copy code

Printf method definition:
Public class PrintStream
  {
Public PrintStream printf (String fmt, Object... args) {return format (fmt, args );}
  }

Type var []; or type [] var;

When declaring an array, you cannot specify its length (number of elements in the array ),

In Java, use the keyword new to create an array object in the format:
Array name = new array element type [number of array elements]

Instance:
TestNew. java:

Program code:

The code is as follows: Copy code

Public class TestNew {
Public static void main (String args []) {
Int [] s;
Int I;
S = new int [5];
For (I = 0; I <5; I ++)
{              
S [I] = I;
}          
For (I = 4; I> = 0; I --){
System. out. println ("" + s [I]);
}      
}  
}

Initialization:

1. Dynamic initialization: The array definition is separated from the operations for allocating space and assigning values to arrays;
2. Static initialization: when defining numbers, allocate space for array elements and assign values;
3. Default initialization: The array is a reference type, and its elements are equivalent to member variables of the class. Therefore, after the array is allocated space, each element is also initialized by the hermit according to the rules of the member variables.

In fact, the printf method receives two parameters: one is a format string and the other is an Object [] array (equivalent to Object...), where all parameters (integer
Array or basic type is automatically packaged and converted to an object ). Then scan the fmt string and match the I-th format specifier with the value of args [I.

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.