栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java笔记2.04

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java笔记2.04

接下来才是真正的进入学习

Java中new过以后的类和数组可以引用赋值
即两个指针(栈)指向同一块内存(堆)
this:this是finally的不允许修改,this是非静态的
因此静态方法不能直接使用(可以通过传参?)

public class b {
    public static void main(String[] args) {
        c test1 = new c();
        test1.pout();
        c test2 = new c();
        test2.name = "李四";
        test2.num = 99;
        test2.pout();

        // 引用赋值
        test2 = test1;
        test2.pout();

        // 更改test2
        test2.name = "王二";
        test2.num = 1024;
        test1.pout();

    }
}

class c {
    public int num = 10;
    public String name = "张三";

    public void pout() {
        System.out.println("this is method cout" + name + " " + num);
    }

}
String类型

构造示例

public class b {
    public static void main(String[] args) {

        String s1 = new String("this is string1");
        System.out.println(s1);

        String s2 = "this is string2";
        System.out.println(s2);

        char[] str = new char[] { 's', 't', 'r', '3' };
        String s3 = new String(str);
        System.out.println(s3);

        byte[] str2 = new byte[] { 65, 66, 67, 68, 69, 70 };
        String s4 = new String(str2);
        System.out.println(s4);
    }
}
   // 需要注意的是,以直接赋值方式创建的string 他们的指向是相同的
   // 因为”“中的内容,是常量stirng
    String str2 = "str";
    String str3 = "str";
    System.out.println(str3 == str2);

    // new则是不同的
    // 通过new则会重新分配空间,空间内容依据是常量“str”
    String str4 = new String("str");
    String str5 = new String("str");
    System.out.println(str4 == str5);

Java中的"=="号
字符串比较

Java中区分基本类型与引用类型
基本类型==基本类型:值比较
引用类型==引用类型:地址比较
 
在Java中可以,string类中的equals方法来比较string对象

        String str = new String("this is str");
        String str1 = new String("this is str");
        String str2 = new String("this is str2");
        String str3 = new String("this is str3");

        System.out.println(str.equals(str1));
        System.out.println(str2.equals(str3));

案例:用户登录案例

import java.util.Scanner;

public class b {
    public static void main(String[] args) {
        Scanner scanf = new Scanner(System.in);
        int num = 0;
        String password = "123456";
        String username = "zhangsan";
        while (num != 3) {
            System.out.println("请输入用户名");
            if (!scanf.next().equals(username)) {
                System.out.println("用户名输入错误!");
                num++;
                continue;
            }
            System.out.println("请输入密码");
            if (!scanf.next().equals(password)) {
                System.out.println("密码输入错误!");
                num++;
                continue;
            } else {
                System.out.println("登录成功!");

                num = num + 10;
                break;
            }
        }
        if (num < 10) {
            System.out.println("您已经登录失败超过三次");
        }
    }
}

案例:遍历字符串

import java.util.Scanner;

public class b {
    public static void main(String[] args) {
        String str = new String();
        Scanner scanf = new Scanner(System.in);
        str = scanf.nextLine();

        for (int i = 0; i < str.length(); i++) {
            System.out.print(str.charAt(i));
        }
    }
}

案例:统计字符出现次数

import java.util.Scanner;

import javax.swing.undo.UndoManager;

public class b {
    public static void main(String[] args) {
        String str = new String();
        Scanner scanf = new Scanner(System.in);
        str = scanf.nextLine();

        int num = 0;

        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == 'A') {
                num++;
            }
        }
        System.out.println(num);
    }
}

案例:int转string,拼接
这种拼接实际操作是每次都会创建一个新对象,对空间比较浪费

import java.util.Scanner;

import javax.swing.undo.UndoManager;

public class b {
    public static void main(String[] args) {
        int arry[] = { 1, 2, 3, 4, 5, 6 };

        String s1 = new String();

        // string类型很强大,可以直接进行拼接
        s1 = s1 + '[';
        s1 = s1 + arry[0];
        System.out.println(s1);
        for (int i = 1; i < arry.length; i++) {
            s1 = s1 + ',' + arry[i];
        }
        s1 = s1 + ']';
        System.out.println(s1);
    }
}

案例:字符串翻转

import java.util.Scanner;
import javax.swing.undo.UndoManager;

public class b {
    public static void main(String[] args) {
        Scanner scanf = new Scanner(System.in);
        String s1 = scanf.nextLine();
        String s2 = new String();

        for (int i = s1.length() - 1, j = 0; i >= 0; i--, j++) {
            System.out.print(s1.charAt(i));
            s2 = s2 + s1.charAt(i);
        }
        System.out.println();
        System.out.println(s2);
    }
}

string.endwith()

import java.util.Scanner;

import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction;
import javax.swing.undo.UndoManager;

public class b {
    public static void main(String[] args) {
        Scanner scanf = new Scanner(System.in);
        String str = new String();
        while (true) {
            str = scanf.nextLine();
            // str的结尾部分是否以"end"结尾
            System.out.println(str.endsWith("end"));
        }
    }
}

StringBuilder类
append方法
reverse方法示例

import java.util.Scanner;

import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction;
import javax.swing.undo.UndoManager;

public class b {
    public static void main(String[] args) {
        Scanner scanf = new Scanner(System.in);
        String str0 = new String("this");
        str0 = str0 + " is test";
        // 实际上this ,is test,this is test,是三个string类、
        // 所以非常耗费空间,因此发明了stringbuilder类
        System.out.println(str0);

        StringBuilder str1 = new StringBuilder();
        // append方法非常强大,可以使用各种基本类型
        str1.append("this is str1 ").append("可以链式继续添加");
        str1.append(10);

        // 将字符串翻转
        str1.reverse();
        System.out.println(str1);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/953344.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号