JavaSE01、如何写一个Java代码

Java

Java - 爪哇岛 盛产咖啡
Java之父 —— 高斯林

Write Once. Run anywhere.
一次运行,到处运行

环境:

JDK - Java开发者工具

IDEA - 集成开发环境


初识Java的main方法

public class HelloWorld {

	public statci void main(String[] args) { // Java中main函数的固定写法
		System.out.println("hello");
	}
}

在这里插入图片描述



用Sublime Text3编译运行

如何编译

在这里插入图片描述



程序运行原理

JDK:Java开发者工具

JRE:Java运行环境

JVM:Java虚拟机

一次编译,到处运行,只要你安装了JDK
在这里插入图片描述



程序需注意的问题

1、每次写完代码,必须保存,然后重新编译!!

2、如果这个类是public修饰的,类名需要和文件名一致
	`public class HelloWorld`

3、方法需在类里 --> 类(方法)

4、不是一个文件对应一个字节码,而是一个类对应一个字节码

​	好处:用到哪个类,加载哪个类,而不是全加载!

在这里插入图片描述

没有out文件

在这里插入图片描述



args

在这里插入图片描述


数组越界:
在这里插入图片描述



注释与打印

三种注释方法:

/**
 * 文档注释 --> 类或方法的前面
 */

public class HelloWorld {

	public static void main(String[] args) {
		for(int i = 0; i< args.length; i++) {
			// System.out.println(args[i]); 行注释
			/* 块注释
			System.out.println(args[0]);
			System.out.println(args[1]);
			System.out.println(args[2]);
			System.out.println(args[3]);
			*/
		}

		System.out.println("hello");
	}
}

运行此代码产生错误问题:

在这里插入图片描述


javac 在编译的时候,默认用GDK格式编译代码,

解决方法:javac -encoding utf-8 HelloWorld.java

在这里插入图片描述


打印方法:

public class HelloWorld {

	public static void main(String[] args) {
		for(int i = 0; i< args.length; i++) {

		System.out.print("打印不换行");
		System.out.println("打印换行");
		System.out.printf("%d\n", 10);
        // 打印不换行打印换行
		// 10

	}
}


变量与类型

类型特性

1、int 多少字节?

int 4字节,无关多少位系统 — 可移植性高

Linux Mac Win 可要有字节码文件,就能编译 — 跨平台

public class HelloWorld {

	/**
	 * int 4字节
	 * @param args [description]
	 */
	public static void main(String[] args) {
		int a = 10;
	}

}

2、有无符号?

Java 中的 int,没有所谓的无符号类型,统一是有符号的

public class HelloWorld {	
	public static void main(String[] args) {
		int a = 10;
		System.out.println(a); // 10
	}
}

3、取值范围?

  1. 最高位为有符号位,剩余31位是数值位

    这31位可表示 2^31 = 2,147,483,648 个数值

  2. int 取值范围:-2^31 -> 2^31-1

    -2147483648 — 0 — 2147483647

public class HelloWorld {	
	public static void main(String[] args) {
		int a = 10;
		System.out.println(a); // 10
        
        // Integer - 包装类 是int的plus版本
        System.out.println(Integer.MAX_VALUE); // 2147483647
		System.out.println(Integer.MIN_VALUE); // -2147483648
	}
}


变量类型

1、变量命名

小驼峰:以(数字 字母 下划线 美元符号)组成,不能以数字开头

public class HelloWorld {	
	public static void main(String[] args) {
		int maxMax = 10;
		int max_num = 10;
		int $max3num = 10;

		int ___________ = 100; // 可以,但不要用
		System.out.println(___________); // 100
        
        int= 300;
		System.out.println(); // 300
        // Unicode字符集:包含中文,固可使用
	}
}

2、变量能否不初始化?

Java 比较安全,如果不初始化就使用,编译器就会报错,不是警告!

int num; // err


3、long 长整形

8bit 64bit

long 的最大最小值:-2^63 2^63-1

取值范围:

public class HelloWorld {	
	public static void main(String[] args) {
		long a = 10L;
		System.out.println(Long.MAX_VALUE); // 9223372036854775807
		System.out.println(Long.MIN_VALUE); // -9223372036854775808
	}
}

4、双精度浮点型变量

注意问题1:

在这里插入图片描述


注意问题2:

小数本身是没有一个精准的数字的,只能精确到几位

public class HelloWorld {	
	public static void main(String[] args) {
		double num = 1.1;
		System.out.println(num*num); // 1.2100000000000002
	}
}

5、单精度浮点型变量

public class HelloWorld {	
	public static void main(String[] args) {
		float f = 10.5;
		System.out.println(f);
		// 报错: 不兼容的类型: 从double转换到float可能会有损失
        // 正确写法:float f = 10.5f;
	}
}

6、 字符类型变量

public class HelloWorld {	
	public static void main(String[] args) {
		char ch1 = 'a';
		System.out.println(ch1); // a

		char ch2 = '数';
		System.out.println(ch2); // 数
	}
}

汉字2字节,代码无问题,由此可得 char 区别于C语言是2字节
在C语言中使用ASCII 表示字符,在Java 中使用Unicode 表示字符,表示的字符种类更多,包括中文


7、 字节类型

public class HelloWorld {	
	public static void main(String[] args) {
		byte b = 10; // 1字节 相当于C中的char
        System.out.println(b1); // 10
	}
}

字节类型取值范围:

public class HelloWorld {	
	public static void main(String[] args) {
		byte b1 = 130; // err
		System.out.println(b2); // 错误: 不兼容的类型: 从int转换到byte可能会有损失
        
        byte b2 = 127; // ok
	}
}

1字节 --> 8bit 最高位符号位,7位数值位

-2^7 - 2^7-1

-128 - 127


8、短整型

public class HelloWorld {   
	public static void main(String[] args) {
		short s = 10; // 2字节
		// -2^16 - 2^16-1
	}
}

9、布尔类型

在Java中,boolean只有两种取值,true真,false假
注:在JVM的规范中,没有规定布尔类型的大小,
布尔类型变量不能±,因为不是数值

public class TestDemo {
	public static void main(String[] args) {
        boolean flag = false;
        System.out.println(flag);
    }
}

10、字符串类型-(引用类型)

public class TestDemo {
	public static void main(String[] args) {
        String str = "hello";
        System.out.println(str);
    }
}

注意问题:

public class TestDemo {
	public static void main(String[] args) {
        System.out.println("hello"+"world"); // helloworld
        System.out.println("hello"+10+20); // hello1020
        // 其他数据类型和字符串使用加号拼接,结果就是一个字符串

        System.out.println(10+20+"hello"); // 30hello
        System.out.println("hello+(10+20)"); // hello+(10+20)

        System.out.println(10+""+20+"hello"); // 1020hello

        System.out.println("a = "+10+", b = " +20); // a = 10, b = 20
		
		/*
        *                         整数   byte short int long
        *                         浮点数 float double
        *          基本数据类型    字符   char
        *                         布尔   boolean
        * 数据类型
        *          引用类型        String  数组 类 接口 枚举……
        * */

		// 转义字符
        System.out.println("\"bit\""); // "bit"
        // 水平制表符\t - Tab键
    }
}


常量

public class TestDemo {
	public static void main(String[] args) {
        // 常量:
        // 1.字面值常量
        // 2.final修饰的变量 ->常量
        // 程序编译时,就已经确定值
        // 只能初始化一次

        // 11、变量:
        // 程序运行的时候们可以改变的量

        final int a = 10;
        // a = 20; // err
    }
}


类型转换与整型提升

强制类型转换

public class TestDemo {
   public static void main(String[] args) {
        int a = 10;
        long b = a; // ok
        System.out.println(b);

        long d = 12;
        // int c = d; // err 报错(强类型语言)
        int c = (int)d; // 强制类型转换(有风险)

        // boolean不能
        boolean n = true;
        // int t = (int)n; // err

        // int->byte
        byte num1 = 1;
        byte num2 = 2;
        byte sum = (byte)(num1 + num2); // 强制类型转换

        byte num3 = 1+2; // 1和2都是常量,在程序编译时,已经被编译为3了  --byte num3 = 3;

    }
}

整型提升

public class TestDemo {
public static void main(String[] args) {
        // 整形提升:小范围提升为大范围,效率高

        // byte放int
        // byte a = 128; // err  -128  127

        byte a = 1;
        byte b = 2;
        byte c = (byte)(a + b); // 小于4 提升
        System.out.println(c);

        int num1 = 10;
        long num2 = 10;
        int sum = (int)(num1+num2);
    }
}

int 和 String 之间的相互转换

public class TestDemo {
    public static void main(String[] args) {
        int num = 10;
        String ret = String.valueOf(num); // int->String
        System.out.println(ret); // 10

        // String->int
        String str = "123";
        int ret2 = Integer.valueOf(str);
        System.out.println(ret+1); //
    }
}


运算符

1、算术运算符

public class TestDemo {
	public static void main(String[] args) {
        // %
        int a = 10;
        int b = 0;
        // System.out.println(a/b); // 抛出异常 程序结束 by zero-算术异常
        System.out.println("xxxxxxxxxxx"); // 不打印

        System.out.println(10 % 3); // 1
        System.out.println(-10 % 3); // -1
        System.out.println(10 % -3); // 1
        System.out.println(-10 % -3); // -1
    }
}

2、增量赋值运算符 += -= ……

public class TestDemo {
	public static void main(String[] args) {
        short s = 10;
        // s = (short)(s+9);
        s += 9; // +=自动强制类型转换
        System.out.println(s);
    }
}

3、自增自减 ++ –

区分前置(先++后使用),后置(先使用后++)


4、关系运算符 == != < > <= >=

关系运算符表达式结果是波尔值
if(布尔表达式)

public class TestDemo {
	public static void main(String[] args) {
        int i1 = 10;
        int i2 = 20;
        System.out.println(i1 == i2); // false
        System.out.println(i1 != i2); // true
    }
}

5、逻辑运算符

public class TestDemo {
   public static void main(String[] args) {
        // 5、逻辑运算符
        // 逻辑与 &&
        // 表达式1 && 表达式2 --> 此2表达式都是布尔表达式
        // 短路:如果表达式1为假,就不执行表达式2

        // 逻辑或 ||
        // 表达式1 || 表达式2 --> 此2表达式都是布尔表达式
        // 短路:如果表达式1为真,就不执行表达式2

        // 逻辑非 !
        int a = 10;
        int b = 20;
        System.out.println(!!(a<b)); // true

        System.out.println(10<20 && 10/0 == 0); // 短路
        System.out.println(10<20 || 10/0 == 0); // 短路
    }
}

6、位运算符

按位与&   -- 对应为都是1,结果是1
按位或|   --1就是1
按位异或^ --  相同为0,相异为1
按位取反~ -- 01,10

7、移位运算符

<< >> >>> 都是按二进制位移
左移 右边补0 相当于原数字*2N次方
右移 左边补符号位 相当于原数字/2N次方
 0000 1011>>1     0000 0101

8、条件运算符

public class TestDemo {
   public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int max = a > b ? a : b; // 表达式1为布尔表达式
    }
}


逻辑控制

选择结构,循环结构,循序结构

1、分支语句

public class TestDemo {
	public static void main(String[] args) {
        // if...else
        // 1. 判断一个数是不是奇数
        Scanner scanner = new Scanner(System.in); // 从键盘获取数据
        // import java.util.Scanner; --> Alt_回车
        int n = scanner.nextInt();
        if( n % 2 == 0) {
            System.out.println("偶数");
        } else {
            System.out.println("奇数");
        }

        // 输入字符串
        String str = scanner.nextLine();
        // String str = scanner.next(); // 遇到空格结束
        System.out.println(str);
        // 有整数和字符串输入,要先读取字符串
    }
}

判断闰年

public class TestDemo {
	public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int year = scanner.nextInt();
        // 1
        if(year % 100 == 0) {
            if(year % 400 == 0) {
                System.out.println(year+"是闰年!");
            }else {
                System.out.println(year+"不是闰年!");
            }
        }else {
            if(year % 4 == 0) {
                System.out.println(year+"是闰年!");
            }else {
                System.out.println(year+"不是闰年!");
            }
        }

        // 2
        if(year % 100 == 0 && year % 400 == 0) {
                System.out.println(year+"是闰年!");
        }else {
            if(year % 4 == 0) {
                System.out.println(year+"是闰年!");
            }else {
                System.out.println(year+"不是闰年!");
            }
        }

        // 3
        if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            System.out.println(year+"是闰年");
        }else {
            System.out.println(year+"不是闰年");
        }
    }
}

2、switch语句

不能用作switch参数的类型:long float double boolean
只能是整数,字符,字符串,枚举(JDK1.5开始,引入枚举,枚举也可作为switch参数)


3、循环结构

public class TestDemo {
	public static void main(String[] args) {
        // idea 如何调试 --> 甲壳虫/右击debug
        // debug -> 观察代码是怎么一步一步执行的
        int i = 0;
        int sum = 0;
        while(i<=5) { // 布尔表达式
            sum += i;
            i++;
        }
        System.out.println(sum);
    }
}

在这里插入图片描述


求阶乘

public class TestDemo {
	public static void main(String[] args) {
        int n = 5;
        int i = 1;
        int ret = 1;
        while(i <= n) {
            ret *= i;
            i++;
        }
        System.out.println(ret); // 120
}

求阶乘和

public class TestDemo {
	public static void main(String[] args) {
        int j = 1;
        int sum = 0;
        while(j <= 5) {
            int i = 1;
            int ret = 1;
            while (i <= j) {
                ret *= i;
                i++;
            }
            sum = sum + ret;
            j++;
        }
        System.out.println(sum);
}

continue

public class TestDemo {
	public static void main(String[] args) {
        // 既能被3整除,也能被5整除
        int i = 1;
        while(i <= 100) {
            if(i % 15 != 0) {
                i++;
                continue;
            }
            System.out.println(i);
            i++;
        }
    }
}

3、for

public class TestDemo {

    public static void main(String[] args) {
        // 快捷方式:fori回车
        for (;true;) {
            System.out.println("111"); // 死循环
        }
    }

/**
* Created with IntelliJ IDEA.
* Description:
* User: ${USER}
* Date: ${YEAR}-${MONTH}-${DAY}
* Time: ${TIME}
*/

在这里插入图片描述


jdk

JDK 8:Java SE 8

在这里插入图片描述

环境变量

1、新建系统变量 java_home
JAVA_HOME
C:\Program Files\Java\jdk1.8.0_301
在这里插入图片描述
2、Path 后
%JAVA_HOME%\bin
在这里插入图片描述
3、添加 classpath

CLASSPATH
.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
在这里插入图片描述
验证:
cmd 命令:java -version
在这里插入图片描述


idea

jetbrains

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述


2、设置

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


快捷键

Ctrl+Alt+I,将选中的代码进行自动缩进编排
快捷键:普通搜索替换:Ctrl+F和Ctrl+R; 全局搜索替换:Ctrl+Shift+F和Ctrl+Shift+R;

WIN + AIT + INS 新建文件,可搜索

Ctrl + AIT + V 最近的变量输出

AIT + V 自动生成变量

ctrl + shift + z 反撤销

选中代码, ctrl alt t可以选择一个代码块包裹选中的代码

  • 18
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 28
    评论
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三春去后诸芳尽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值