Java基础
Java基础
1.1JDK/JRE
java development kit ---java 开发工具包 javac
java runtime environment --- java运行时环境(jvm)
/**
public --- 访问修饰符,决定这段代码的访问级别
定义一个类
class [ClassName]
类名必须和文件名一致。而且类名的命名方式必须是驼峰
*/
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello world");
}
}
- 如何编译运行一个java源文件
** ** 1.javac xxx.java
2.java xxx
java源文件通过Jdk的javac命令编译成jvm可识别的.class字节码文件,再由jvm编译成计算机可以实别的二进制文件
2.1 数据类型
1.整型
| int | 4字节 | 大概20亿 |
|---|---|---|
| short | 2字节 | 3万多 |
| byte | 1字节 | -128-127 |
| long | 8字节 |
//变量声明
int i;
//变量声明与初始化
long i = 10L;
2.浮点型
| float | 4字节 | |
|---|---|---|
| double | 8字节 |
float f = 3.14f;//实际工作中不要用
double d = 3.14D;
//数值范围
Double.POSITIVE.INFINITY
Double.NEGATIVE.INFINITY
//not a number
Double.NaN
//一个整数除以0,0/0,负数的平方根非数
3.字符型
| char | 2字节 | \ |
|---|
char ch = 'a';
4.布尔型
| boolean | true /false (1/0) |
|---|
boolean b = 1;
2.2 常量
public class Constants{
public static void main(String[] args){
final double PAI = 3.14;
double r = 5;
System.out.println("Area of cricle:" + PAI * r * r);
}
}
常量:一旦被赋值就不能变化了;用finl关键字定义。习惯上来说定义常量名称是全大写
float constant ---> FLOAT_CINSTANT
2.3运算符
(+、-、*、/、%)
15%2 = 1
15/2 = 7
15.0/2 = 7.5
2.3.1数学函数
double x = 4;
//求平方根
double y = Math.sqrt(x);
//幂运算
//10^4
//double z = 10*10*10*10;
double z =- Math.pow(10,4);
2.3.2其他运算符
++,-- ,!=,>,<,>=,<=,==
public class Cal2{
public static void main(String[] args){
int i = 10;
System.out.println(i++);//10 --- 11
System.out.println(++i);//12
}
}
2.3.3位运算
&,|, ^,~,>>,<<,>>>
1 & 1 = 1 1 & 0 = 0
1 | 0 = 1 0 | 0 = 0
1 ^ 0 = 1 1 ^ 1 = 0
16 >> 2 0001 0000 ---> 0000 0100 -- 4 16/2^n
3 << 2 0000 0011 ---> 0000 1100 -- 12 3*2^n
-8 >> 2 1000 1000 ---> 1110 0010
-8 >>> 2 1000 1000 ---> 0010 0010
2.3.4短路
&& ||
public class Cal3{
public static void main(String[] args){
int i = 1;
//短路与
System.out.println((2 == 3) && (i > 0));//false
System.out.println((i == 1) || (false));//true
}
}
2.3.5三目运算
public class Cal4{
public static void main(String[] args){
int i = 1;
int res;
res = (i == 2) ? 2:50;
}
}
2.4 类型转换

i + j
如果i是double,j也会转成double --->double;
否则,一个操作数是float,另一个也是float -->float;
否则,一个是long,另一个也是long ---> long;
否则都为int;
2.4.1 强制类型转换
高精度转低精度时需要强制类型转换,会丢失精度
低精度转高精度自动完成。
double x = 3.14;
int y = (int)x; //3
2.5 字符串
public class StringDemo {
public static void main(String[] args) {
/**初始化方法*/
String str = "a";
System.out.println(str);
String str2 = new String("abc");
System.out.println(str2);
char[] chs = {'a','b','c','d','e'};
String charStr = new String(chs);
System.out.println(charStr);
String charOffset = new String(chs,2,2);
System.out.println(charOffset);
}
}
public class StringDemo2 {
public static void main(String[] args) {
String str = "Hello";
//把字符串转换成char数组
char[] chs = str.toCharArray();
for (int i = 0; i <3 ; i++) {
System.out.print(chs[i]);
}
System.out.println();
//取子串
String subStr = str.substring(0,3);
System.out.println(subStr);
String str2 = "World";
//字符串拼接
String newStr = str + str2;
System.out.println(newStr);
//插入分隔符
String cut = String.join("/","Y","C","L");
System.out.println(cut);
}
}
public class StringDemo3 {
public static void main(String[] args) {
String str = "Hello";
//Hello --> Help
// str = str.substring(0,3) + "p!";
// System.out.println(str);
String str2 = "world.";
String newStr = str + str2;
str = str + str2;
System.out.println(str);
System.out.println(newStr);
System.out.println(str.equals(newStr));
//可能是对的
System.out.println("Helloworld.".equals(str));
System.out.println("hello".equalsIgnoreCase("HELLO"));
}
}
public class StringDemo4 {
public static void main(String[] args) {
//空串和null不一样
String str = "";
System.out.println(str.length());
System.out.println(str.equals(""));
String str2 = null;
//System.out.println(str2.length());
//System.out.println(str2.equals(""));
}
}
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer str = new StringBuffer("ab");
System.out.println(str);
System.out.println(str.getClass().hashCode());
str.append("c");
System.out.println(str);
System.out.println(str.getClass().hashCode());
System.out.println();
//字符串不可变
//同一对象的hashcode一定相等
String str2 = "ab";
System.out.println(str2);
System.out.println(str2.hashCode());
str2 += "c";
System.out.println(str2);
System.out.println(str2.hashCode());
}
}
String不可变类,不可变对象,因为字符串是存在字符串常量池中的,这个和设计有关,每次字符串变更都会新创建一个对象,另外一个原因是保证线程安全。如果字符串会经常变化应该是用StringBuffer对字符串进行操作。StringBuffer线程安全,原因是有加互斥锁,StringBuilder效率会高一些但是线程不安全。不要使用
2.6循环和流程控制
public class IFDemo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("What 's your age?");
int age = in.nextInt();
//条件是布尔值
if (age >=0 && age <= 10){
System.out.println("child");
}else if (age >10 && age <18){
System.out.println("teen");
}else if (age > 18 && age < 50){
System.out.println("adult");
}else {
System.out.println("older");
}
}
}
public class ControlDemo {
public static void main(String[] args) {
int x = 9;
int y;
y= x > 10 ? 2 : 3;
System.out.println(y);
//三目运算和if-else转换
if (x >10){
y =2;
}else {
y = 3;
}
}
}
public class Retirment {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("退休能拿到多少钱?");
double salary = in.nextDouble();
System.out.println("你的年薪是是多少");
double payment = in.nextDouble();
System.out.println("增长比率是多少");
double rate = in.nextDouble();
double balance = 0;
int years = 0;
while (balance < salary) {
balance += payment;
double interest = balance * rate / 100;
balance += interest;
years++;
}
System.out.println("你还需要工作 " + years + "年");
}
}
public class Retirment2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("你的年薪是是多少");
double payment = in.nextDouble();
System.out.println("增长比率是多少");
double rate = in.nextDouble();
double balance = 0;
int years = 0;
String flag;
do {
balance += payment;
double interest = balance * rate / 100;
balance += interest;
years++;
System.out.printf("在 %d年后,我的资产是%,.2f%n",years,balance);
System.out.print("是否退休(Y/N)");
flag = in.next();
}while (flag.equals("N"));
}
}
public class ForDemo {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10 ; i++) {
if (i % 2 != 0){
continue;
}
sum += i;
}
System.out.println(sum);
}
}
public class SwitchDemo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int i = in.nextInt();
//char byte,short,int.枚举enum,字符串
//在每个case结束以后都要break
//default通常是做错误处理的
switch (i){
case 1:
System.out.println("aaaa");
//这个必须加
break;
case 2:
System.out.println("BBBB");
break;
default:
System.out.println("CCCCC");
}
}
}
循环中断:continue,跳过本次循环;break跳出整个循环体,或者是switch里面的case代码块
2.7数组
public class ArrayDemo {
public static void main(String[] args) {
int[] a = new int[100];
//初始化一个大小为5的Boolean类型数组
boolean[] b = new boolean[5];
System.out.println(a[0]);
System.out.println(b[0]);
int[] c = {1,2,3,4};
for (int i = 0; i < c.length ; i++) {
System.out.print(c[i]+" ");
}
System.out.println();
System.out.println(Arrays.toString(c));
//增强for循环
for (int elem: c) {
System.out.println(elem);
}
int[] intArray;
//匿名数组
intArray= new int[]{1,2,3,4,5,6,7};
}
}
public class ArraySort {
public static void main(String[] args) {
String[] s = {"F","A","C","Z"};
Arrays.sort(s);
System.out.println(Arrays.toString(s));
}
}
public class ArryCopy {
public static void main(String[] args) {
int[] a ={1,2,3,4};
//两个数组是同一个引用
int[] copyA = a;
copyA[3] = 6;
System.out.println(Arrays.toString(copyA));
System.out.println(Arrays.toString(a));
//这是真实的拷贝
int[] realCopy = Arrays.copyOf(a,a.length);
realCopy[0] = 9;
System.out.println(Arrays.toString(realCopy));
System.out.println(Arrays.toString(a));
}
}
public class Lottery {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("你要猜几次");
int k = in.nextInt();
System.out.println("最大的数字是多少");
int n = in.nextInt();
int[] nums = new int[n];
for (int i = 0; i < nums.length ; i++) {
nums[i] = i + 1;
}
int[] result = new int[k];
for (int i = 0; i < result.length ; i++) {
int r = (int) (Math.random()*n);
result[i] = nums[r];
nums[r] = nums[n-1];
n--;
}
Arrays.sort(result);
for (int r : result){
System.out.println(r);
}
}
}
public class TwoDimensionalDemo {
public static void main(String[] args) {
// int[][] b = new int[10][10];
int[][] a = {
{2,0,0},
{0,2,0},
{0,0,2}
};
//row
for (int i = 0; i < a.length ; i++) {
//col
for (int j = 0; j <a[i].length ; j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println();
for(int[] row : a){
for (int elem : row){
System.out.print(elem+" ");
}
System.out.println();
}
}
}
其他

标准输入
public class InputDemo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("What is your name?");
//获取屏幕输入的字符串
String name = in.nextLine();
System.out.println("Hello, " + name);
}
}

浙公网安备 33010602011771号