Java 的基本程序设计结构
1. Java代码的风格
a. Main + line , example
Public class ClassName
{
public static void main(String[]args)
{
//program statement, each line is end with ;
System.out.println(“it is a program statement!!!”);
}
}
b. annotation
1. // using for annotating one line
2./*.........*/ using for annotating a few lines
3./**.........*/ not only annotatating a few lines ,but also it can be using for create document atomatically
2. Type of Data
int: 4bytes; short: 2 bytes;long: 8bytes; byte: 1byte;
float: 4bytes; double: 8 bytes
constant : final basType = value;
attention:
1. please pay attention to the range of data in each type. try not to do data conversion from long lenth to short one
2. do not do comparesion between float and double , some times, float 2.1f > double 2.1 or float 1/18f =double 1/18f and float 2.1f < double 2.1f, this is related to computer data mechanism
such as snapshot A.the next scrpts, a is always greater than b
snapshot B. a==b ,but the value is not equal.
so , please pay more attention to use this type of data
3. Operator
+,- ,*,/
a. coercion
base type:int a = 1; float fa = (float) a ;
basetostring: 1. String.valueOf(val)
2. baseType.toString(val) (baseType is the data type of val)
stringtobaseType: int: Integer.parseInt(str); float: Float.parseFloat(str); ...
b.logical Operators
!=; >; <; >=;<=;&& ; ||;
&& is in short-circuit operation, like expresion1 &&expression2 , when using it , if expression1 is true, do expression2, if expression1 is False, expression2 is not excuted ,
|| is also the same style, expression1 || expression2, if expression1 is true, expression2 is not excuted,result is true, if expression1 is false, do expression2
c. bit operations
&(and), |(or),^(xor),~(not),<<(left shift);>>(right shift ,hight part is filled with symbol bit);>>>(right shift,hight part is filled with zero)
4. String
String a = "HelloWorld!!!" ; String str = "H"; String b = "Demo"
frequently method:
a.Substring(int beginIndex, Int endIndex): return to a substring from beginIndex to endIndex-1
b. a.length():return to the length of string a
c. a.indexof(str,int beginIndex) : return to the index of first string str searching from beginIndex
d. a.trim(): return to a new string without trim space at head and top postion
e. String.join(delimiter:";",a,b): return to a new string which is joined by a and b with ;
f:a.equals(b): return to the result of weather a is equal to b
attention: if the string is offen changed , please use a dynamic class StringBuilder,which is more efficient than String, because String is a static length class , whereas StringBuilder is a dynamic one , considering static string , there need more time to create a new string when changing the string. we can ref to this site: https://blog.csdn.net/qq_24025219/article/details/99692955
5. control
a. if
if (condition){
statement;
} else if (condition){
statement;
}
else{
statement;
}
b. while
while (condition) {
statement;
}
c. do while
do{
statement;
} while (condition);
d. for
for(condition){
statement;
}
e. switch
switch(choice){
case choicevalue:
...
break;
case choicevalue2:
...
break;
default:
...
break;
}
example:
网友评论