java core technology learning the first lesson

Basics

type of data

java is a strongly typed language, which means that for every variable life type, java in a total of eight basic data types. Four of the integer two float, a method for representing Unicode characters for the type and representing a Boolean true or false
Eight kinds of specific data types
Integer in java machine type has nothing to do with the running of java code, which would address the software programmers brought from one platform to another platform with a transplant, or between different systems on the same platform migration problems. Since the java program must ensure that all machines running the same result can be obtained, the value of each data type is fixed. (C and C ++ is different byte length thereof substantially because of the different data types may be used in the machine are not the same)

Suffix have a long integer value L (eg 4000000000L). A prefix 0x hexadecimal, octal number 10, (corresponding to 8 such as 010 in octal). Obviously octal representation confusing, so it would be best not to use octal Changshu.

Floating-point type

This data type double precision float double precision. The vast majority of applications use double data type.
The default value with the suffix float F, does not represent a double type.
double suffixes can no suffix, can also carry a suffix of D

Indicates an overflow error and three special floating point values:
positive infinity, negative infinity, NaN (Not a Number)
, for example, divided by a positive integer 0 result is positive infinity. Calculating the square root of 0/0 or negative, the result is NaN.
Floating-point value does not apply to financial banned rounding error in the calculationSuch as: System.out.println (2.0-1.1) will print .8999999999999999, rather than people think 0.9. The main reason is a floating-point values, binary representation, but in a binary system can not accurately represent fractions 1/10, which seems not accurately represent 1/3 as a decimal, if you need does not contain any error in the calculation, you should use BigDecimal class.

char type describes a unit of code with UTF-16 code. Unicode is a universal coding to unify various countries, is the international code.
Char type are strongly discouraged in the program, unless absolutely necessary to UTF-16 code units is operated, the string is preferably represented by the need to deal abstract data

variable

Can int a, b in a row a plurality of variable declarations;But do not advocate this way, one by one to generate variables improve the readability
Variable names are case sensitive
Variables must be initialized with a variable after the assignment statementDeclare and initialize variables can be put together, it can also be separated. Variable has not been initialized can not be used.

constant

final keyword to declare constants, Final keyword indicates that the variable can only be assigned once, once the assignment can not be changed, it is customary, constant name in uppercase.
In java, often want a constant multiple methods can be used in a class, these constants are usually lives for class constants, you can use the keyword static final to set up a class constants (class constants declared as a global variable)If a class constants are declared as public, then the methods of other classes can also use this constant

Operators

15/2=7 15%2=1 15.0/2=7.5
Is an integer of 0 will raise an exception, and the floating-point addition result of addition is 0 will be an infinite or a NaN
Ternary operator, such as x <y x: y x and y values that returns the smaller of?
Java seek using the square root of a number sqrt, such as: Double the Math.sqrt = x (y);
exponentiation of java with pow, such as a power of x - Double Math.pow = B (x, a);
In Math class in order to achieve the fastest performance, all the methods use a computer floating point unit routine, get results if a totally predictable important than speed, then you should use the class StrictMath

Casts, Double X = 9.78; y = int (int) X; the value of y becomes such 9. cast by truncating the fractional part of the floating point value to an integer. If you want to rounding floating-point calculations, in order to obtain the nearest integer, the method requires the use of Math.round. Such as:
int y current value of y = (int) Math.round (x) becomes 10
Note that the above were still using round were cast int when rounding, because the round method returns a long type, you pick up the return value int, so still need to be cast.

Operator Precedence
priority

String

java string is a sequence of Unicode characters
Substring: String a="Hello"; String b=a.substring(0,3); Hel result b is taken as the length of the subscript 0-2
String concatenation
A string and a string of non-spliced, non-string is automatically converted into a string.
String immutable
As above, like a string, this unit is always H ello sequence, if you really want to modify the string. Can also be used one way is to first extract the desired character, and then spliced on.
Such as: The Hello modified Help: String a = a.substring (0,3 ) + "p"; this embodiment is also a modification of the point, but no real Hello modified string, the string memory space still exists of.
It can not be modified because of the java string of characters, so the object of the java String class in the document referred to as immutable string. Like always the same number three number three. But can be modified to a string variable, you can point to a new string.
Magical place java compiler is to allow sharing of the string, as if to create a public space for the string variable points to the location of the corresponding storage pool. Share efficiency is much higher than the efficiency of extraction and string concatenation
equals
SUMMARY detection equals two strings are equal, check for equality with the address pointing ==
return true equal, unequal returns false.
EqualseIgnoreCase method using case-insensitive string.
When comparing equals, string comparison may also be used variable is compared, and the variable strings may be mixed. Such as: a.equalse ( "Hello")

一定不能使用==检测两个字符串是否相等,==这个运算符只能确定两个字符串是否在同一位置上,	当然如果字符串放在同一位置上,它们必然相等,但是完全有可能将内容相同的多个字符串的拷贝放在不同的位置上。

String API (common)

cahr charAt(index)
返回给定位置的代码单元,除非对底层感兴趣,否则不用调用这个代码。
int compareTo(String other)
按照字典顺序,如果字符串位于other之前,返回一个负数,如果字符串位于other之后,返回一个正数,如果两个字符串相等返回0
boolean endsWith(String suffix)
如果字符串以suffix结尾,返回true
boolean equals(Object other)
如果字符串与other相等返回true
boolean equalsIgnoreCase(Object other)
如果字符串与other相等返回ture(忽略字符串大小写来进行比较)
int indexOf(String str)
int indexOf(String str,int fromIndex)
int indexOf(int cp)
int indexOf(int cp,int fromIndex)
返回字符串str或者代码点匹配的第一个子串开始的位置。这个位置从索引0或者fromIndex开始计算,如果在原始串中不存在str,返回-1。
int lastIndexOf(String str)
int lastIndexOf(String str,int fromIndex)
int lastIndexOf(int cp)
int lastIndexOf(int cp,int fromIndex)
返回与字符串str或代码点cp匹配的最后一个字符串开始的位置,这个位置从原始串尾端或从fromIndex开始计算。
int length()
返回字符串长度
String replace(CharSequence oldString,CharSquence newString)
返回一个新字符串,这个字符串用newString代替原始字符串中所有的oldString。可以用String或者StringBuilder对象作为CharSquence参数。
String toLowerCase()
返回一个新字符串,这个字符串将原始的字符串中的所有大写字母改成小写字母。
String toUpperCase()
返回一个新字符串,这个字符串将原始的字符串所有的小写字母改成大写字母。
String trim()
返回一个新的字符串,这个字符串删除原始字符串头部和尾部的空格。

Construction of a string
Sometimes the need to build the string, the string is connected by way of this to achieve the purpose of low efficiency. Every time a connection string, will build a new String object, time-consuming and a waste of space. Use StringBuilder class can avoid the occurrence of this problem.
If you need to build a string of many small pieces of string, the following steps should follow. First, a configuration of an empty string constructor.
StringBuilder builder = new StringBuilder ();
each time when content is added to use a portion of the append method:
builder.append ( "CUI");
builder.append ( "CG");

on the calls when it needs to build a string toString method.
String result = builder.toString ();
StringBuffer is StringBuilder's predecessor, StringBuffer support multithreading, but inefficient.
StringBuilder does not support multi-threading, but high efficiency. This is also the reason why the StringBuilder later introduced (to improve efficiency)

**StringBuilder API**
int length()
返回构建器或者缓冲器中的代码单元数量。
StringBuilder append(Stringc)
追加一个代码单元并返回this
StringBuilder append(char c)
追加一个代码单元并返回this
void setCharAt(int i,char c)
将第i个代码单元设置为c
StringBuilder insert(int offset,String str)
在offset处插一个字符串并返回this。
StringBuilder insert(int offset	,Char C)
在offset处插一个代码单元并返回this。
StringBuilder delete(int startIndex,int endIndex)
删除偏移量从startIndex到endIndex-1的代码单元并返回this
String toString()
返回一个与构建器或缓冲器内容相同的字符串。

Guess you like

Origin blog.csdn.net/cui929434/article/details/94718690