IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Java学习第一天,有C基础入门有多快,来手撕代码呀(●‘?‘●) -> 正文阅读

[Java知识库]Java学习第一天,有C基础入门有多快,来手撕代码呀(●‘?‘●)

main方法快捷键:psvm
输出快捷键:sout

Hello World! 仪式

public class demo01 {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        System.out.println("你好,世界!");
    }
}

在这里插入图片描述


Java的main方法

这里学习了命名规范,以及Java中main方法的书写

public class demo01 {
    public static void main(String[] args) {
/*
        //大小写敏感
        String a ="zhouql";
        String A ="zhouql";
        //规范
        String 1a ="zhouql";
        String Av ="zhouql";
        String $ ="zhouql";
        String $1 ="zhouql";
        String _ ="zhouql";
        String _1 ="zhouql";
        String 1_ ="zhouql";
        String -asd ="zhouql";
        //不能使用关键字作为变量名或方法名
        String clas ="zhouql";
        String class ="zhouql";
*/

    }
}

基本数据类型

package base;

public class demo02 {
    public static void main(String[] args) {
        //八大基本数据类型
        //整型
        int num1 = 10;  //最常用
        //范围在100多
//        byte num2 = 200;
        //short
        short num3 = 20;
        //long
        long num4 = 40L;    //Long类型在数字后面加个L表示是long类型


        //float 浮点数:小数
        float num5 = 12.3F; //float类型加F,否则就报错
        double num6 = 3.14159;


        //字符
        char name1 = 'a';
//        char name2 = 'as';  //字符是一个

        //字符串
        //String 不是关键字,是一个类
        String num7 = "asd";

        //布尔值
        boolean flag = true;    //真
        boolean fla = false;    //假

    }
}

强制转换以及转义

public class demo03 {
    public static void main(String[] args) {
        int i=10;
        int i1=010;//ba jin zhi
        int i2=0x10;//shi liu jin zhi

        System.out.println(i);
        System.out.println(i1);
        System.out.println(i2);
        System.out.println("===================================================");
        float f=0.1f;
        double d=1.0/10;
        System.out.println(f==d);
        System.out.println(f);
        System.out.println(d);
        System.out.println("===================================================");
        float a=231313123123123189f;
        float b=a+1;
        System.out.println(a==b);
        System.out.println("====================================================");
        char c1='a';
        System.out.println("强制转换");
        System.out.println((int)c1);
        System.out.println((int)'A');
        System.out.println("====================================================");
        System.out.println("转义字符");
        System.out.println("Hello\tWorld!");
        System.out.println("Hello\nWorld!");
    }
}

在这里插入图片描述


新特性

public class demo06 {
    public static void main(String[] args) {
        //JDK新特性,方便看
        int money = 10_0000_0000;
        System.out.println(money);
        int years = 20;
        System.out.println(money*years);

        System.out.println("-----------------------------");
        long sum = money*((long)years);
        System.out.println(sum);

    }
}

命名再认识

public class demo07 {
    //static 是修饰符,不区分前后
    //final 只能读不能修改,不能改变的就定义成常量
    static final  double PI = 3.1415926;

    public static void main(String[] args) {
        System.out.println(PI);
    }
}
//命名规范
/*
* 1,见名之意
* 2,类成员变量驼峰原则
* 3,局部变量首字母小写和驼峰原则
* 4,常量大写和下划线
* 5,类名首字母大写和驼峰
* 6,方法名首字母小写和驼峰
* */

类变量,局部变量,实例变量

package base;

public class demo08 {
    //类变量  static
    static  double salary = 20000;


    //实例变量:从属于对象
    //实例变量,不初始化,默认值,除了基本类型,其他都是null,布尔值默认是false
    String name;
    int age;

    //main方法
    public static void main(String[] args) {
        //局部变量;必须声明和初始化值
        int i = 10;
        System.out.println(i);
        //变量类型 变量名  变量值
        demo08 demo08 = new demo08();
        System.out.println( demo08.age);
        System.out.println( demo08.name);
        //类变量  static
        System.out.println(salary);
    }

    //其他方法
    public  void  add(){
        System.out.println();
    }
}

运算符部分

加减乘除

public class Demo01 {
    public static void main(String[] args) {
        //二元运算符
        int a = 10;
        int b = 20;
        int c = 30;
        int d = 10;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);
    }
}

public class Demo02 {
    public static void main(String[] args) {
        long a = 12345678L;
        int b = 123;
        short c = 10;
        byte d = 2;

        System.out.println(a+b+c+d);//long
        System.out.println(b+c+d);//int
        System.out.println(c+d);//int
    }
}

模运算(重点体会)

public class Demo03 {
    public static void main(String[] args) {
        //关系
        int a = 10;
        int b = 20;
        int c = 21;
        //取余shu,模运算
        System.out.println(c/b);
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
    }

}

自增自减

public class Demo04 {
    public static void main(String[] args) {
        //一元运算符  ++  自增  -- 自减
        int a = 3;
        int b =a++;//执行完这行代码后,先给b值,是3,然后在自增a的值,此时a是4
        System.out.println(a);
        int c = ++a;//执行完这行代码前,先自增a的值,此时a是5,然后在赋值
        //简单说a++先赋值后递增    ++a先递增后赋值

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        //幂运算
        double pow  = Math.pow(3,4);
        System.out.println(pow);
    }
}

逻辑运算

public class Demo05 {
    public static void main(String[] args) {
        //与(and)  或(or)  非(no)
        boolean a = true;
        boolean b = false;

        System.out.println(a && b);
        System.out.println(a || b);
        System.out.println(!(a && b));

        //短路运算
        System.out.println("===================");
        int c = 5;
        boolean d = (c<4)  && (c++<4);
        System.out.println(d);
        System.out.println(c);
    }
}

位运算(目前了解即可)

public class Demo06 {
    public static void main(String[] args) {
        /*
        * A = 0011 1100
        * B = 0000 1101
        *
        * A&B = 0000 1100       两个都是1才为1,否则就是0
        * A|B = 0011 1101         都是0,才为0,有一个1,直接为1
        * A^B = 0011 0001       相同为0,不相同为1
        * ~B = 1111 0010         取反
        *
        *
        * >>右移 *2  <<左移  /2
        * 效率极高
        * 0000 0000     0
        * 0000 0001     1
        * 0000 0010     2
        * 0000 0011     3
        * 0000 0100     4
        *
        * 0001 0000     16
        * ……
        * */
    }
}

小拓展

public class Demo07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        a+=b;
        a-=b;
        System.out.println(a);
        System.out.println(a+b);
        //字符串连接  String
        System.out.println(""+a+b);
        System.out.println(a+b+"");
    }
}

获取数据输入,简单交互,类似C语言scanf

注意一点,IO流使用完记得释放

import java.util.Scanner;

import static java.lang.System.in;

public class Demo01 {
    public static void main(String[] args) {
        //创建一个扫描器对象。用来接受键盘数据
        Scanner scanner = new Scanner(System.in);

        System.out.println("使用next方式接收");


        if(scanner.hasNext()){
            String  str  = scanner.next();
            System.out.println("输入的内容为"+str);
        }
import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("输入,nextline");

        if(scanner.hasNextLine()){
            String s = scanner.nextLine();
            System.out.println(":"+s);
        }
        scanner.close();
    }
}

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入");
        String str = scanner.nextLine();
        System.out.println("输入的是:"+str);
        scanner.close();
    }

}

mport java.util.Scanner;

public class demo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int i = 0;
        float f = 0.0f;
        System.out.println("输入");
        if(scanner.hasNextInt()){
            i = scanner.nextInt();
            System.out.println(":"+i);
        }else{
            System.out.println("输入的不是整数");
        }
        scanner.close();
    }
}
import java.util.Scanner;

public class Demo05 {
    public static void main(String[] args) {
        int score = 0 ;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩,童鞋");
        int s = scanner.nextInt();
        if(s>=60 && s<=100){
            System.out.println("恭喜,没挂科");
        }else if(s>=0 && s<60){
            System.out.println("额,你懂");
        }else{
            System.out.println("不是整数");
        }
        scanner.close();
    }
}

public class Demo06 {
    public static void main(String[] args) {
        char grade = 's';

        switch (grade){
            case 'a':
                System.out.println("优秀");
                break;
            case 'b':
                System.out.println("良好");
                break;
            case 'c':
                System.out.println("及格");
                break;
            case 'd':
                System.out.println("再接再厉");
                break;
            default:
                System.out.println("未知等级");
        }
    }
}
public class Demo07 {
    //JDK7开始支持字符,字符的本质还是数字
    //反编译  java---class(字节码文件)-----反编译(IDEA)


    public static void main(String[] args) {
        String name = "小张";

        switch (name){
            case "刘森":
                System.out.println("刘森");
                break;
            case  "小张":
                System.out.println("小张");
                break;
            default:
                System.out.println("弄啥嘞");
        }
    }
}

循环

public class Demo01 {
    public static void main(String[] args) {
        int i = 0;
        while(i<100){
            System.out.println(i);
            i++;
        }
    }
}
public class Demo02 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        while (i<=100){
            sum+=i;
            i++;
        }
        System.out.println(sum);
    }
}

public class Demo03 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do {
            sum+=i;
            i++;
        }while (i<=100);
        //注意分号
        System.out.println(sum);
    }
}

public class Demo04 {
    public static void main(String[] args) {
        int a = 0;
        while (a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("======================");
        do{
            System.out.println(a);
            a++;
        }while (a<0);
    }
}

public class Demo05 {
    public static void main(String[] args) {
        int sum  = 0;
        for (int i = 1;i<=100;i++){
            sum+=i;
            System.out.println(i);
        }
        System.out.println(sum);
    }
}
//死循环
//for(;;)
public class Demo06 {
    public static void main(String[] args) {
        int a = 0;
        int b = 0;
        for (int i = 0; i < 100; i++) {
            if(i%2==0){
                a+=i;
            }else{
                b+=i;
            }

        }
        System.out.println("偶数和是:"+a);
        System.out.println("ji数和是:"+b);
    }

public class Demo07 {
    public static void main(String[] args) {
        int i = 0;
        while (i<=1000){
            i++;
            if(i%5==0){
                System.out.print(i+"\t");
            }
            if(i%(15)==0){
                System.out.println();
            }
        }
    }
}

//输出完会换行    println
//输出完不会换行    print

99乘法表

public class Demo08 {
    public static void main(String[] args) {
        for (int i = 1;i<=9;i++){
            for (int j = 1;j<=i;j++){
                System.out.print(i+"*"+j+"="+(i*j)+'\t');
                }
                System.out.println();
            }

        }
    }



package 循环;
//JDK5,重点循环数组和集合
public class Demo09 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50,60};
        //便利数组的元素
        for (int x:numbers){
            System.out.println(x);
        }
    }
}

//continue;跳过某次循环(妹子)
//break;强制推出循环(凶)
public class Demo10 {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            if(i>50&&i<60){
                continue;
            }
            System.out.println(i);
        }
    }
}

//打印101~150的质数
//质数大于1的自然数中,除了1和它本身以外不在有其他因数的自然数
public class Demo11 {
    public static void main(String[] args) {
        int i = 0;
        outer:for (int a = 101;a<150;a++){
            for(int b = 2;b<a/2;b++){
                if(a%b ==0){
                    continue outer;
                }
            }
            System.out.print(a+"  ");
        }
    }
}

public class Demo12 {
    public static void main(String[] args) {
        for (int i = 0; i <=5; i++) {
            for (int j = 5; j >=i ; j--) {
                System.out.print(" ");
            }
            for (int j = 0; j <=i ; j++) {
                System.out.print("*");
            }
            for (int j = 0; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

方法

个人理解Java的方法可以与C的核心函数理解一致
在main外写方法,叫着不舒服,可以暂时叫函数,保证main函数的整洁,其次就是在main方法中的调用,后来你会发现,它不仅仅用在main中……

public class Demo01 {
    //main方法
    public static void main(String[] args) {
        int add = add(1,5);
        System.out.println(add);
        print();
        System.out.println(max(3,3));
    }

    //假发
    public static int add (int a,int b){
        return  a+b;
    }
    //void
    public static void print(){
        System.out.println("Hello World!");
    }
    //最大值
    public static int max(int a,int b){
        int c = a>b?a:b;
        return c;
        //1.返回值
        //2.种植方法
    }
}

方法重构,就是一个方法,假设我比较大小的方法,小明需要比较两个数的大小,小红需要比较三个人的方法,这是只需要重新写一个名字一样,参数不一样,实现过程不一样就实现了函数的重构

public class Demo02 {
    public static void main(String[] args) {
        System.out.println(max(2.6,4.8,3.6));
    }
    public static int max(int a,int b){
        int c = a>b?a:b;
        return c;
    }
    public static double max(double a,double b){
        double c = a>b?a:b;
        return c;
    }
    public static double max(double a,double b,double c){
        if(a>b){
            return a;
        }else if(b>c){
            return b;
        }else{
            return c;
        }
    }
}

可变长参数

public class Demo03 {
    public static void main(String[] args) {
        Demo03 demo03 = new Demo03();
        demo03.test(1,234,45,4,6);
    }
    public void test(int... i){
        System.out.println(i[0]);
        System.out.println(i[1]);
        System.out.println(i[2]);
        System.out.println(i[3]);
        System.out.println(i[4]);
    }
}

public class Demo04 {
    public static void main(String[] args) {
        printMax(34,56,34,78,89);
        printMax(new double[]{1,2,34});
    }
    public static void printMax(double... numbers){
        if(numbers.length == 0){
            System.out.println("没有输入");
            return;
        }
        double result = numbers[0];

        for(int i = 1; i<numbers.length;i++){
            if(numbers[i]>result){
                result = numbers[i];
            }
        }
        System.out.println("the max value is "+result);
    }
}

递归,简单说就是传递乌龟,哈哈
就是自己调用自己,自己返回给自己

//递归头
//递归体
public class Demo05 {
    public static void main(String[] args) {
        Demo05 demo05 = new Demo05();
        demo05.test();
    }
    public void test(){
        test();
    }
}

实例,计算n地阶乘


public class Demo06 {
    public static void main(String[] args) {
        int c = f(4);
        System.out.println(c);
    }
    public static int f(int n){
        if(n == 1){
            return  1;
        }else {
            return n*f(n-1);
        }
    }
}

值得注意地是,Java语言用地栈机制,而递归操作,需要大量的计算机资源,往往面对稍大地需求,Java预置的空间根本不够递归完,所以谨慎使用,不过要理解递归,感受递归之美!!!快来手撕代码吧


在这里插入图片描述

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-08-09 10:06:43  更:2021-08-09 10:06:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/10 4:20:27-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码