java学习笔记

安装

安装的时候要在甲骨文官网上,下载jdk8。是jdk不是jre哦 orcale有可能会要登录,从csdn上随便找一个大好人分享的就好了。

路径

在环境变量的path那里添加一个类似这样的东西C:\Program Files (x86)\Java\jre1.8.0_231\bin路径会有不同,但不变的是要添加jre里面的bin
添加完后,在cmd或者powershell中输入javac,出现这个
在这里插入图片描述
输入java,出现这个
在这里插入图片描述
这就说明安装完成了。

hello world

环境 vscode 大概是这个模样
在这里插入图片描述

public class hello{
    public static void main(String args[]){
        System.out.print("hello world");
    } 
}

在终端里先javac hello.java然后java hello,屏幕上就会输出hello world

public class Hello{
    //public class 类名必须跟文件名保持一致,且首字母要大写,一个*.java里面只允许有一个public class
    public static void main(String args[]){//这是一切程序的起点
        System.out.println("hello world");//println用来换行
        System.out.print("hello world");//print不换行
        System.out.println("hello world");
    } 
}
// 有几个calss就生成几个*.class文件。
/*比如在这个程序里就生成了 三个*.class文件,分别是
A.class
B.class
hello.class */
class A{}//可以有多个class 
class B{}

因为有中文注释编译无法通过的办法,javac -encoding UTF-8 Hello.java设置编码格式
数据类型分为基本数据类型和引用数据类型

  • 整数数据类型
class Zhengshu {
    public static void main(String[] args) {
        System.out.print("整数类型");
        long chang = 124;// 定义长整数
        int zheng = 11;// 定义整数
        short duan = 111;// 定义短整数
        System.out.println("");
        System.out.println("long " + chang);
        System.out.println("int " + zheng);
        System.out.println("short " + duan);
    }
}
  • 字符
class Zifu {
    public static void main(String[] args) {
        // 字符数据类型用单引号包裹
        char myna = '素';// 字符用 char 单引号,只能单个字符
        String myname = "你好"; // 字符串用String 双引号 多个字符
        System.out.print(myname);
        System.out.println(myna);
    }
}
  • 浮点数
class Xiaoshu {
    public static void main(String[] args) {
        double c = 1.2d; //后面接d或D,可以不接
        double b = 1.2;
        float cc = 1.34f;// 后面接 F 或f ,不能不接
        System.out.println((float) (c * cc));// 类型转换
    }
}
  • 整数和浮点数之间的数据转换
//(欲转换的数据类型)变量名称
flaot c=1.2;
int b = (int)c;
  • 布尔 boolean
class buer {
    public static void main(String[] args) {
        boolean flag = true;
        System.out.println(flag);
        double c = 1.2d;
        // System.out.println(c+flag);
        String s = "2";
        /*把String转化为int型。
1.int i=Integer.parsenInt(s);
2.int i=Integer.valueOf(s).intValue();
 */
    }
}

java的运算

public class Ys {// 加减乘除
    public static void main(String[] args) {
        int bb = 11;
        System.out.println(bb);
        int num = 12;
        System.out.println(bb + num);
        System.out.println(bb - num);
        System.out.println(bb * num);
        System.out.println(bb / (float) num);// /是除法
        System.out.println(bb % num);// %是取模
        bb--;
        System.out.println(bb);// 自减
        bb += 3;// 加3;
        System.out.println(bb);
        System.out.println(bb > num);// 关系运算 true
    }
}

class San {// 三目运算
    public static void main(String[] args) {
        System.out.println("三目运算");
        int sanmu = 0;
        sanmu = sanmu > 0 ? 1 : 2;// 这就是三目运算
        sanmu = 12;
        System.out.println(sanmu);
    }
}

class Luo {// 逻辑运算
    public static void main(String[] args) {
        System.out.println("逻辑运算");
        int numa = 12;
        int numb = 13;
        int l = 0;
        System.out.println((numa < numb) && ((numa - numb) < 0));// 与运算
        System.out.println(l > 0);
        // !要和boolean合用
        boolean c = true;
        System.out.println(!c);
        /*
         * &&和&的区别 其实是短不短路的区别,比如
         * false&&true,程序在得知第一个是false后会确定false&&true是false。但&是要确定完两个
         * 后才会确定false&&true是false
         */
    }
}
  • 逻辑控制
public class Luoji {// 逻辑运算,if
    public static void main(String[] args) {
        int age = 38;
        if (age > 60) {
            System.out.println("可以退休");
        } else {
            System.out.println("不可以退休");
        }
    }}

class switchh{//逻辑运算 switch

    public static void main(String[] args) {
        int age=23;
        switch (age) {
            case 18:{
                System.out.println("你18le");
                break;//必须要有break
            }
        
            default:{
                System.out.println("haha");//必须要有default
                break;
            }
        }

    }
}

class whilee{//while运算
    /* 有两种
    第一种是while(条件){语句}
    第二种是do{语句}while(条件)
    */ 
    public static void main(String[] args) {
        int i=0;
        while(i<10){//这是while(条件){语句}
            i+=2;
            if(i==9){
                i-=1;
            }
            System.out.println("i= "+i);
        }
        System.out.println("i= "+i);//i仍然是10
        do{
            i+=1;
            System.out.println("i= "+i);
        }while(i==2);//这里要有;
        System.out.println(i);
    }
}
class forr{//用for
    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.print("\n");
        }
    }
}
  • 数组
public class Ob {
    public static void main(String[] args) {
        System.out.println("面向对象,数组");
        /*
         * 数组
         *
         * */
        //第一种方法,声明并分配数组
        int lt[] = null;//声明整形数组lt
        lt = new int[3];//声明数组长度
        //第二种方法
        int[] ltt = null;//声明的另一种方式
        lt[0] = 1;
        lt[1] = 1;
        lt[2] = 1;
//        System.out.println(lt);
        for (int i = 0; i <= 2; i++) {
            lt[i] = i * 123 + 123;
            System.out.println(lt[i]);
        }
        System.out.println(lt.length);
        System.out.println(lt[lt.length-1]);
        //引用传递
        System.out.println("调用传递");
        int litt[]=new int[4];
        for(int i=0;i<litt.length;i++){
            litt[i]=i*3+123-4*67+200;
            System.out.println(litt[i]);
        }
        //定义一个新数组,和litt用同样的堆内存
        System.out.println("定义一个新数组,和litt用同样的堆内存");
        int newlitt[]=litt;
        for(int i=0;i<newlitt.length;i++){
            newlitt[i]=0;
            System.out.println("newlitt["+i+"]"+"="+newlitt[i]);
        }
        System.out.println("修改newlitt后");
        for(int i=0;i<litt.length;i++){
            System.out.println("litt["+i+"]"+"="+litt[i]);
        }
        //静态生成数组
        int jing[]=new int[]{1,2,3,4,5,6,78,};
        System.out.println(jing.length);
        jing[3]=12234456;
        System.out.println(jing[3]);
        //类 数组
        int lei[]=new int[]{1,2,24,45,45,321,3214,456,32,12,90,65,332,456};
        for(int i=0;i<lei.length;i++){
            System.out.print(lei[i]+"\t");
        }
        System.out.println("\n");
        System.out.println(">------------------------");
        System.out.println("\n");
        //使用类排序
        java.util.Arrays.sort(lei);
        for(int i=0;i<lei.length;i++){
            System.out.print(lei[i]+"\t");
        }
        int leiei[]=new int[]{1,2,3,4,4,3,3};
        System.arraycopy(lei,3,leiei,0,4);
        print(leiei);

    }
    public static void print(int temp[]){//定义一个方法去打印数组
        System.out.println("\n");
        for(int i=1;i<temp.length;i++){
            System.out.print(temp[i]+"\t");
        }
    }
}
class Ds{
    public static void main(String[] args) {
        //多维数组
        //动态
        int dd[][]=new int[4][3];
        //静态
        int dj[][]=new int[][]{{1,2,3},{1,2},{1,2,3},{3,4,5,3,4,53,3,5,54,54}};
        for (int i=0;i<dd.length;i++){
            for(int j=0;j<dd[i].length;j++){
                System.out.print("dd["+i+"]"+"["+j+"]"+"="+dd[i][j]+"\t");
            }
            System.out.println("\n");
        }
        System.out.println("静态数组");
        for (int i=0;i<dj.length;i++){
            for(int j=0;j<dj[i].length;j++){
                System.out.print("dj["+i+"]"+"["+j+"]"+"="+dj[i][j]+"\t");
            }
            System.out.println("\n");
        }

    }
}
//数组和方法
class SzAndFang{
    public static void main(String[] args) {
        System.out.println("数组和方法");
        //方法对数组的修改一定会影响到原始数据
        int dl[]=new int[]{1,2,3,4,4,5,};
        for(int i=0;i<dl.length;i++){
            System.out.print(dl[i]+"\t");
        }
        wayToob(dl);
        System.out.println("\n");
        for(int i=0;i<dl.length;i++){
            System.out.print(dl[i]+"\t");
        }
    }
    public static void wayToob(int temp[]){
        for(int i=0;i<temp.length;i++){
            temp[i]*=123;
        }
    }
}

  • 方法
  • 方法和类
  • 构造方法
class Gouzao{
    //构造方法的重载,要按参数的数量进行升序或降序排列
    public Gouzao(){//构造方法,没有返回值,方法名和类名相同
        System.out.println("定义的构造方法启用");
    }
    //构造方法可以重载
    public Gouzao(String sa){
        name=sa;
    }
    public Gouzao(String sa,int sag){
        name=sa;
        age=sag;
    }
    private String name;//类的属性,封装掉他
    //封装
    public void setName(String p){//setter方法
        name=p;
    }
    public String getName(){//getter方法
        return name;
    }
    private int age;
    public void setAge(int a){
        if(a>=0){//防止输入的年龄为小于0的数
            age=a;
        }
    }
    public int getAge(){
        return age;
    }
    public void tell(){//类的方法
        System.out.println("name: "+name+"\tage:  "+age);
    }
}
public class GzaoHanShu {//构造方法
    public static void main(String[] args) {
        //即使没有定义构造方法,也会默认产生一个构造方法
        Gouzao bk=new Gouzao();
        bk.setName("hehe");
        bk.setAge(12);
        bk.tell();
        System.out.println("构造函数有一个参数");
        System.out.println(">------------------");
        Gouzao bk1=new Gouzao("hahahj");
        bk1.tell();
        System.out.println("构造函数有二个参数");
        System.out.println(">------------------");
        Gouzao bk2=new Gouzao("lalalal",16);
        bk2.tell();
        //匿名函数
        System.out.println("匿名函数");
        System.out.println(">------------------");
        new Gouzao("qust",12).tell();
    }
}
class FangCan{//方法中的参数类型是任意的
    public static void main(String[] args) {//方法中的参数类型是任意的
        int c=fun(12,"hello12");
        System.out.println(c);

    }
    public static int fun(int x,String c){
        if(c=="hello"){
            return x;
        }else {
            return x+1;
        }
    }
}

  • 对象数组
  • String
public class StringDemo {
    public static void main(String[] args) {
        //String类
        String sta = new String("heloo");
        String stb = new String("heloo");
        System.out.println(sta == stb);//false,分析内存可得知原因
        //其他的呢?
        int ina = 1;
        int inb = 1;
        System.out.println(ina == inb);//true
        /* 1、“==”比较两个变量本身的值,即两个对象在内存中的首地址。
           (java中,对象的首地址是它在内存中存放的起始地址,它后面的地址是用来存放它所包含的各个属性的地址,
           所以内存中会用多个内存块来存放对象的各个参数,而通过这个首地址就可以找到该对象,进而可以找到该对
           象的各个属性)
           2、“equals()”比较字符串中所包含的内容是否相同。
        * */
        System.out.println(sta.equals(stb));//true,完全比较内容为true
        //为防止空指向,要把匿名对象放在前面
        System.out.println("heloo".equals(sta));//true
        /*String的两种实例化方法
        1.匿名
        String a="hhhhh"; //会开辟堆内存和栈内存
        * */
        String a = "fhhhh";
        String b = "fhhhh";
        String c = "world";
        System.out.println(a == b);//true,一个对内存
        System.out.println(b == c);//false,?
        System.out.println(a == c);//false,?
        //因为共享设计模,在一个对象池里面,若是通过构造方法的话会造成特别多的垃圾
        //字符串的内容最好不要修改,会造成较多垃圾

    }
}
class StringDe{
    public static void main(String[] args) {
        System.out.println("String的方法");
        String tochar="sadjsajskjajkjs";
        //定义方法,如果返回值是boolean,命名用is
        //toCharArray
        char c[]=tochar.toCharArray();//toCharArray
        print(c);
        System.out.println(isNum(c));
        char a[]="123243434".toCharArray();
        System.out.println(isNum(a));
        //charAt
        char cc=tochar.charAt(1);
        System.out.println(cc);
        //equalsIgnoreCase
        String eq="wert";
        String eqq="WERT";
        System.out.println(eq==eqq);//false
        System.out.println(eqq.equalsIgnoreCase(eqq));//true
        //conpareTo.比较大小
        System.out.println(eq.compareTo(eqq));//32
        //查找
        System.out.println("helloword".indexOf("l"));//2
        System.out.println("helloword".indexOf("l",3));//3
//        lastIndexOf
        System.out.println("helloword".lastIndexOf("l"));//3,从后往前查,但返回是从前往后数的
        //contains
        System.out.println("helloword".contains("l"));//boolean  true
        //判断开头结尾
        System.out.println(">-------------------------------");
        System.out.println("***@@@hello^^^".startsWith("**"));//true
        System.out.println("***@@@hello^^^".endsWith("^^^"));//true
        System.out.println("***@@@hello^^^".startsWith("@@",3));//true
        int arraryone[]=new int[]{1,2,3,4,5,5,4,3,32,34,432,12,4,5346,4,5445,};
        java.util.Arrays.sort(arraryone);
        print(arraryone);
        System.out.println("\n"+">--------------------------");
        String stringone="qweerrtryhdhrllo,hello,hello";
        System.out.println(stringone.replaceAll("hello","啥"));//qweerrtryhdhrllo,啥,啥
        System.out.println(stringone.replaceFirst("hello","啥"));//qweerrtryhdhrllo,啥,hello
        System.out.println(stringone.replace("hello","啥"));//qweerrtryhdhrllo,啥,啥
        System.out.println(">------------------------------");
        //substring 类似于切片,s小写
        System.out.println(stringone.substring(1,6));//左开右闭,不可以负数
        System.out.println(stringone.substring(4));
        System.out.println(">--------------------------------------");
        //spliit
        String stringarraryone[]=stringone.split(",");//qweerrtryhdhrllo	hello	hello
        print(stringarraryone);
        String stringarrarytwo[]=stringone.split(",",2);//qweerrtryhdhrllo	hello,hello
        print(stringarrarytwo);
        //trim
        //取消两边空格
        System.out.println("   asdlkjvirfi  dlevnrojewvo  ".trim());
        //toUpperCase,toLowerCase
        System.out.println("djnfuerghwghwihgi4oi".toUpperCase());
        System.out.println("uIFEAFHOI4HIJKDNFKJnjdnfweh".toLowerCase());
        System.out.println(">--------------------------------------");
        System.out.println(initcap("   dlaenfidvfrvrvDWF   "));
    }
    public static void print(char temp[]){
        for(int i=0;i<temp.length;i++){
            System.out.print(temp[i]+"\t");
        }
    }
    public static void print(int temp[]){
        for (int i=0;i<temp.length;i++){
            System.out.print(temp[i]+"\t");
        }
    }
    public static void print(String temp[]){
        for(int i=0;i<temp.length;i++){
            System.out.print(temp[i]+"\t");
        }
        System.out.println("\n");
        System.out.println("length:\t"+temp.length);
    }
    public static boolean isNum(char temp[]){//判断数字
        for(int i=0;i<temp.length;i++){
            if(temp[i]>'9'||temp[i]<'0'){
                return false;
            }
        }
        return true;
    }
    //定义一个方法首字母大写
    public static String initcap(String temp){
        temp=temp.trim();
        temp=temp.substring(0,1).toUpperCase()+temp.substring(1);
        return temp;
    }

}
class CeShi{
    public static void main(String[] args) {
        //toCharArray
        char chararraryone[]="qwwdwqfva".toCharArray();//变成数组
        print(chararraryone);
        System.out.println("\n");
        //charAt
        //返回所在位置
        char a="swfesgegfag".charAt(2);//f
        System.out.println(a);
        //equalsIgnoreCase
        //比较,忽略大小写
        System.out.println("qdeqwfqf".equalsIgnoreCase("QDEQWFQF"));//true
        //compareTo
//        比较
        System.out.println("qe".compareTo("QE"));//32
        //        lastIndexOf
        //查找存在的位置
        System.out.println("qqewfsacefa".indexOf("e"));//2
        //不存在会返回负一
        //contains
        //查看是否存在
        boolean isb="wjdksalvehello".contains("hello");//true
        System.out.println(isb);
        //startsWith
        boolean isc ="%%%%%lkjicfiesjfv******".startsWith("%%%%%");//true
        System.out.println(isc);
        //endswith
        //同上
        //substring
        //切片
        System.out.println("qwewqrewtewt".substring(1,4));//左开右闭//wew
        System.out.println("dsnfgrwuhgiorwgio".substring(1));//从1到最后//snfgrwuhgiorwgio
        //split
//        分割
        String stingarraryone[]="hello my wwe dcvr".split(" ");
        print(stingarraryone);
        //trim
        //去除两边空格
        System.out.println("  wqewq wqfeq wqrewt    ".trim());//wqewq wqfeq wqrewt
        //toUpperCase,toLowerCase
        //全部大小写
        System.out.println("dsadwdec".toUpperCase());//DSADWDEC
        //replaceAll
        //替换
        System.out.println("aaasdwasfdewfhello".replace("hello","啥"));//aaasdwasfdewf啥

    }
    public static void print(char temp[]){
        for(int i=0;i<temp.length;i++){
            System.out.print(temp[i]+"\t");
        }
    }
    public static void print(int temp[]){
        for (int i=0;i<temp.length;i++){
            System.out.print(temp[i]+"\t");
        }
    }
    public static void print(String temp[]){
        for(int i=0;i<temp.length;i++){
            System.out.print(temp[i]+"\t");
        }
        System.out.println("\n");
        System.out.println("length:\t"+temp.length);
    }
    public static boolean isNum(char temp[]){//判断数字
        for(int i=0;i<temp.length;i++){
            if(temp[i]>'9'||temp[i]<'0'){
                return false;
            }
        }
        return true;
    }
    //定义一个方法首字母大写
    public static String initcap(String temp){
        temp=temp.trim();
        temp=temp.substring(0,1).toUpperCase()+temp.substring(1);
        return temp;
    }
}

  • String方法
class CeShi{
    public static void main(String[] args) {
        //toCharArray
        char chararraryone[]="qwwdwqfva".toCharArray();//变成数组
        print(chararraryone);
        System.out.println("\n");
        //charAt
        //返回所在位置
        char a="swfesgegfag".charAt(2);//f
        System.out.println(a);
        //equalsIgnoreCase
        //比较,忽略大小写
        System.out.println("qdeqwfqf".equalsIgnoreCase("QDEQWFQF"));//true
        //compareTo
//        比较
        System.out.println("qe".compareTo("QE"));//32
        //        lastIndexOf
        //查找存在的位置
        System.out.println("qqewfsacefa".indexOf("e"));//2
        //不存在会返回负一
        //contains
        //查看是否存在
        boolean isb="wjdksalvehello".contains("hello");//true
        System.out.println(isb);
        //startsWith
        boolean isc ="%%%%%lkjicfiesjfv******".startsWith("%%%%%");//true
        System.out.println(isc);
        //endswith
        //同上
        //substring
        //切片
        System.out.println("qwewqrewtewt".substring(1,4));//左开右闭//wew
        System.out.println("dsnfgrwuhgiorwgio".substring(1));//从1到最后//snfgrwuhgiorwgio
        //split
//        分割
        String stingarraryone[]="hello my wwe dcvr".split(" ");
        print(stingarraryone);
        //trim
        //去除两边空格
        System.out.println("  wqewq wqfeq wqrewt    ".trim());//wqewq wqfeq wqrewt
        //toUpperCase,toLowerCase
        //全部大小写
        System.out.println("dsadwdec".toUpperCase());//DSADWDEC
        //replaceAll
        //替换
        System.out.println("aaasdwasfdewfhello".replace("hello","啥"));//aaasdwasfdewf啥

    }
    public static void print(char temp[]){
        for(int i=0;i<temp.length;i++){
            System.out.print(temp[i]+"\t");
        }
    }
    public static void print(int temp[]){
        for (int i=0;i<temp.length;i++){
            System.out.print(temp[i]+"\t");
        }
    }
    public static void print(String temp[]){
        for(int i=0;i<temp.length;i++){
            System.out.print(temp[i]+"\t");
        }
        System.out.println("\n");
        System.out.println("length:\t"+temp.length);
    }
    public static boolean isNum(char temp[]){//判断数字
        for(int i=0;i<temp.length;i++){
            if(temp[i]>'9'||temp[i]<'0'){
                return false;
            }
        }
        return true;
    }
    //定义一个方法首字母大写
    public static String initcap(String temp){
        temp=temp.trim();
        temp=temp.substring(0,1).toUpperCase()+temp.substring(1);
        return temp;
    }
  • toCharArray
  • replaceAll
  • toUpperCase,toLowerCase
  • trim
  • split
  • substring
  • endswith,startsWith
  • contains
  • lastIndexOf
  • compareTo
  • equalsIgnoreCase
  • charAt
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值