java学习 day1

Java study

1. 内存溢出加粗样式,类型转换

int money = 10_0000_0000;
int years = 20;
int total = money * years; //-1474836480 , 计算溢出了
long total2 = money * years; //默认是int,转换之前已经存在问题了

long total3 = money * ((long)years); // 先把一个数转换为Long
System.out.println(total3);

2. 变量作用域

public class study {
	
	// 类变量 static
	static double salary = 2500;

	// 实例变量:从属于对象;如果不自行初始化,这个类型的默认值 0 0.0
	// 布尔值:默认false
	// 除了基本类型,其余默认值都是null
	String name;
	int age;
	
	public static void main(String[] args) {
		// 局部变量;必须声明和初始化
		int i = 10;
		System.out.println(i);
		
		//变量类型 变量名字 = new study();
		Study study = new Study();
		System.out.println(study.age());
		System.out.println(study.age());
		
		//类变量  static
		System.out.println(salary);
	}

	// 其他方法
	public void add() {


	}
}

3. 变量作用域
常量的使用

public class Demo09 {
	
	// 修饰符,不存在先后顺序,常量
	static final double PI = 3.14;
	
	public static void main(String[] args) {
		System.out.println(PI);
	}
	
}

4. 运算符
idea里面Ctrl+D可以直接复制当前行到下一行

public class Demo2 {
	public static void main(String[] args) {
		long a = 121123231L;
		int b = 123;
		short c = 10;
		byte d = 8;
	
		//所有不是long型,最后都会转为int;有long结果就是long;有double结果就是double
		System.out.println(a+b+c+d);
		System.out.println(a+b+c+d);
		System.out.println(a+b+c+d);

		// 短路运算
		int c = 5;
		boolean d  = (c<4)&&(c++<4);
		System.out.println(d); // 这里由于c<4是false,所以后面的c++也不会执行
		System.out.println(c); // 上面没执行c++,这里c还是5

		int a = 10;
		int b = 20;
		Syetem.out.println(""+a+b); // 由于""字符串在前面,所以后面的局部变量会直接连起来
		Syetem.out.println(a+b+""); // 由于""字符串在后面,所以前面的局部变量会运算

		// 三元运算符
		int score = 80;
		String type = score < 60 ? "不及格" : "及格"
		System.out.println(type);
	}
}

5. JavaDoc生成文档
idea里面

/**
 * @author Zhai
 * version 1.0
 * since 1.8
 */
public class Doc {
	String name;
	/*
	 * @author Zhai
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public String test(String name) throws Exception {
		return name;
	}
}

命令行输入以下代码生成JavaDoc文件

javadoc -encoding UTF-8 -charset UTF-8 Doc.java

6. 用户交互Scanner

public class Demo01 {
	public static void main(String[] args) {
		Scanner scammer = new Scanner(System.in);
		System.out.println("使用next方式接收:");

		int i = 0;
		float f = 0.0f;
		
		//判断用户有没有输入字符串
		if (sacnner.hasNext()) { // 前面的空格省略,有效字符开始,空格结束
			String str = scanner.next();
			//String str = scanner.nextLine();   以Enter为结束符,可以获得空白
			System.out.println("输出的内容:"+str);
		}
		
		System.out.println("请输入整数: ")
		if(scanner.hasNextInt()) {
			i = scanner.nextInt();
			System.out.println("整数数据:" + i);
		}else {
			System.out.println("输入的不是整数数据!");

		System.out.println("请输入小数: ")
		if(scanner.hasNextFloat()) {
			f = scanner.nextFloat();
			System.out.println("小数数据:" + i);
		}else {
			System.out.println("输入的不是小数数据!");
		}
		
		double sum = 0;
		int m = 0;
		while(scanner.hasNextDouble()) {
			double x = scanner.nextDouble();
			m++;
			sum += x;
			System.out.println("你输入了第" + m + "个数据,然后当前结果是sum=" + sum);
		}
		System.out.println(m + "个数的和为" + sum);
		System.out.println(m + "个数的平均值为" + (sum / m));

		//凡是IO流的类如果不关闭会一直占用资源
		scanner.close();

	}

}

7. 打印九九乘法表

public class ForDemo04 {
	public static void main(String[] args) {
		for(int i = 1; j <= 9; j++) {
			for(int i = 1; i <= j; i++) {
				System.out.print(j + "*" + i + "=" + (j * i) + "\t");
			}
			System.out.println();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值