Java learning a

1. The transition between the numeric types

 

 

 Five solid line represents conversion information is lost, there may be a broken line represents a conversion loss of accuracy.

 

2. cast

For example cast conversion between double and int

double x = 9.997;

int nx = (int)x;

Such values ​​nx 9

If you want to variables rounding

double x=9.9997;

int nx = (int)Math.round(x);

 

3. String

 

String greeting = "Hello";

String s = greeting.substring(0,3)

The second parameter is the first position do not want to copy

 

4. immutable string

 

String string does not provide a method for modifying the string. If you want to "Hello" changed to "Help"

String substring may be used alternatively

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

Detecting whether strings quite

“Hello”.equals(greeting)

 

To detect whether two strings are equal, ignoring case strings

“Hello”.equalsIgnoreCase("Hello")

To check a string is neither null nor empty string, in this case the use of:

if(str!=null && str.length()!=0)

 

5. Some of the common string variable

 

char charAt (int index): returns the given code means

length (): returns the length of the string

substirng(int beginIndex)

substring (int beginIndex, int endIndex) Returns a new string

toLowerCase()

toUpperCase()

trim () spaces removed the head and tail of the original string

 

 

6. The file input and output

Scanner in = new Scanner(Path.get("myfile.txt"));

If the file name contains a backslash and other symbols,

“ c:\\mydirectory\\myfile.txt ”

To write to a file,

PrintWriter out = new PrintWriter("myfile.txt");

String name = in.nextLine();

To enter an integer, which use in.nextInt ()

 

 

Guess you like

Origin www.cnblogs.com/zhichun/p/11622091.html