public class Process {
public static void main(String[] args){
//Java的三种输入方式
/*
* System.out.println("Hello World!");
* System.out.print("Hello World!");
* String str = "Hello World";
*
* System.out.printf("%s",str);
*/
int a = 230000;
System.out.println(a);
double b = 2.3434;
System.out.printf("%d\n",a);
System.out.printf("%.2f",b);
/*
* %d 格式化输出整数
* %x 格式化输出十六进制整数
* %f 格式化输出浮点数
* %e 格式化输出科学计数法表示的浮点数
* %s 格式化字符串
* 和c语言一样了
* */
String str1 = "hello";
String str2 = "hello";
if(str1==str2){
System.out.println("相等");
}else{
System.out.println("不相等");
}
// 那么str1==str2将返回true。因为这种定义的字符串储存于常量池中,常量池中所有相同的字符串常量被合并,只占用一个空间。
//
// 但如果通过下面new新建的字符串则为false
/*
* */
String s1 = new String("hello");
String s2 = new String("hello");
if(s1==s2){
System.out.println("相等");
}else{
System.out.println("不相等");
}
/*
这里的不相等是因为s1和s2是两个对象,存在于堆区比较的存在栈区的地址所以不相等
*
* */
if( s1!=null && s1.equals(s2)){
System.out.println("相等");
}else{
System.out.println("不相等");
}
//这里是相等,所以平常对字符串的比较尽量使用equals来进行比较可以减少出现bug,当然不仅字符串,只要任两个
//引用类型都可以用equal
//当然使用equals的时候首先要判断s1是否为空,不然会直接报错可以利用短路运算 s1!=null &&s1.equals(s2);
/* if(){}
if(){}else{}
if(){}else if{}else{}
if里面判断值类型用==,但是如果要是判断引用类型的话就是上述方法
*/
/*
* */
switch(s1){
case "hello":System.out.println("输出"+s1);break;
case "World":System.out.println("输出world");break;
default:System.out.println("没找到");break;
}
/*while(){}
* do{}while()//先执行条件在判断循环,至少执行一次;
*
* */
/*
* for(){}
*
* */
int [] arra = {23,34,23,54,12};
for (var ar: arra )
{
System.out.println(ar);
}
//这里的for each只是把js中的in替换成了:其他都一样var 是java10中新增加的,主要用于带有构造器的局部变量声明和for循环中
//for each无法指定遍历的顺序,也无法获取数据的索引
//小练习,利用循环计算圆周率
double pai = 0 ;
for (int i = 1; i <1000000 ; i+=4) {
pai += (1.0/i-1.0/(i+2.0));
//这里带0是为了让编译器知道这是浮点型
// System.out.printf("%f.4\n",pai);
//因为这里面有个乘以4所以会出现一点偏差
}
pai*=4.0;
System.out.printf("%.16f",pai);
/*
* break和continue
* break一般配合if使用或者switch,跳出最近的一层循环,
* continue跳出当前循环然后执行下次循环
* */
}
}