一 python和java字节字符串比较
1.1字符串常用方法
| String origin = "hello world 中国"; |
| |
| |
| char v1 = origin.charAt(13); |
| System.out.println(v1); |
| |
| |
| int len = origin.length(); |
| for (int i = 0; i < len; i++) { |
| char item = origin.charAt(i); |
| System.out.println(item); |
| } |
| |
| String v2 = origin.trim(); |
| System.out.println(v2); |
| |
| String v3 = origin.toLowerCase(); |
| System.out.println(v3); |
| String v4 = origin.toUpperCase(); |
| System.out.println(v4); |
| String[] v5 = origin.split(" "); |
| System.out.println(v5); |
| String v6 = origin.replace("h", "l"); |
| System.out.println(v6); |
| String v7 = origin.substring(2, 6); |
| System.out.println(v7); |
| boolean v8 = origin.equals("hello world"); |
| System.out.println(v8); |
| boolean v9 = origin.contains("中"); |
| System.out.println(v9); |
| boolean v10 = origin.startsWith("a"); |
| System.out.println(v10); |
| String v11 = origin.concat("哈哈哈"); |
| System.out.println(v11); |
1.2字符串拼接
| |
| String s="hello"+"world"+"中国"; |
| |
| |
| StringBuilder sb = new StringBuilder(); // StringBuffer线程安全 |
| //StringBuffer sb = new StringBuffer(); // StringBuffer线程安全 |
| sb.append("hello"); |
| sb.append(" "); |
| sb.append("world"); |
| sb.append(" "); |
| sb.append("中"); |
| sb.append("国"); |
| System.out.println(sb.toString()); |
1.3 java字节数组和字符串相互转换
| |
| byte[] b = v4.getBytes(); |
| System.out.println(b); |
| System.out.println(Arrays.toString(b)); |
| |
| |
| |
| |
| |
| |
| |
| byte[] b1 = v4.getBytes("GBK"); |
| System.out.println(Arrays.toString(b1)); |
| |
| |
| |
| String v8 = new String(new byte[]{-27, -67, -83, -28, -70, -114, -26, -103, -113}); |
| String v9 = new String(new byte[]{-59, -19, -45, -38, -22, -52},"GBK"); |
| System.out.println(v8); |
| System.out.println(v9); |
1.4 java 字符数组和字符串相互转换
| |
| char[] c=new char[]{'彭','于','晏'}; |
| String v1 = new String(c); |
| System.out.println(v1); |
| |
| |
| String str1 = "彭于晏"; |
| char[] c=str1.toCharArray(); |
| System.out.println(c.length); |
| System.out.println(c[0]); |
1.5 python字节和字符串
| |
| v1 = '彭于晏' |
| v2 = v1.encode('utf-8') |
| print(v2) |
| v3 = v1.encode('GBK') |
| print(v3) |
| |
| |
| v3 = [item for item in v2] |
| print(v3) |
| |
| |
| v4 = [bin(item) for item in v2] |
| print(v4) |
| |
| |
| v5 = [hex(item) for item in v2] |
| print(v5) |
1.6 java字节数组转python字符串
| |
| Python字节无符号(不含负数): 0 ~ 255 |
| 0 1 2 3 4 5 ... 127 128 129 130 ... 255 |
| |
| Java字节是有符号(含有负数):-128 ~ 127 |
| 0 1 2 3 4 5 ...127 -128 -127 - 126 -125 .... -2 -1 |
| |
| |
| [229, 189, 173, 228, 186, 142, 230, 153, 143] |
| |
| [-27, -67, -83, -28, -70, -114, -26, -103, -113] |
| |
| |
| |
| 逻辑是:数字大于0,不操作,小于0,加上256 |
| |
| v1 = [-27, -67, -83, -28, -70, -114, -26, -103, -113] |
| def java_arr_to_python_str(v1): |
| num_list = bytearray() |
| for i in v1: |
| if i < 0: |
| i = i + 256 |
| num_list.append(i) |
| return num_list.decode('utf-8') |
| |
| |
| if __name__ == '__main__': |
| print(java_arr_to_python_str(v1)) |
二 Object类
Object类是java.lang包下的核心类,Object类是所有类的父类,何一个类时候如果没有明确的继承一个父类的话,那么它就是Object的子类
以下两种类的定义的最终效果是完全相同的
class Person { }
class Person extends Object
2.1 Object类可以接受所有类的对象
| public class Demo05 { |
| public static void main(String[] args) { |
| String name="justin"; |
| int age =19; |
| Object v1=name; |
| v1=age; |
| Person p=new Person(); |
| v1=p; |
| System.out.println(v1); |
| |
| Object[] arrs=new Object[3]; |
| arrs[0]="justin"; |
| arrs[1]=19; |
| arrs[2]=p; |
| System.out.println(Arrays.toString(arrs)); |
| |
| } |
| } |
| |
| class Person{ |
| } |
2.2 Object 类的方法
Object 类属于java.lang
包,此包下的所有类在使用时无需手动导入,系统会在程序编译期间自动导入

| |
| 保护方法,实现对象的浅复制,只有实现了Cloneable接口才可以调用该方法,否则抛出CloneNotSupportedException异常。 |
| |
| |
| final方法,返回Class类型的对象,反射来获取对象。 |
| |
| |
| 该方法用得比较多,一般子类都有覆盖,来获取对象的信息。 |
| |
| |
| 该方法用于释放资源。因为无法确定该方法什么时候被调用,很少使用。 |
| |
| |
| 比较对象的内容是否相等 |
| |
| |
| 该方法用于哈希查找,重写了equals方法一般都要重写hashCode方法。这个方法在一些具有哈希功能的Collection中用到。 |
| |
| |
| wait方法就是使当前线程等待该对象的锁,当前线程必须是该对象的拥有者,也就是具有该对象的锁。wait()方法一直等待,直到获得锁或者被中断。wait(long timeout)设定一个超时间隔,如果在规定时间内没有获得锁就返回。 |
| 调用该方法后当前线程进入睡眠状态,直到以下事件发生。 |
| 其他线程调用了该对象的notify方法。 |
| 其他线程调用了该对象的notifyAll方法。 |
| 其他线程调用了interrupt中断该线程。 |
| 时间间隔到了。 |
| 此时该线程就可以被调度了,如果是被中断的话就抛出一个InterruptedException异常。 |
| |
| |
| 该方法唤醒在该对象上等待的某个线程。 |
| |
| |
| 该方法唤醒在该对象上等待的所有线程。 |
| |
| |
| |
| |
| |
| |
| |
| |
2.3 获取Object类型变量的具体类型
| |
| Object v2=10; |
| System.out.println(v2.getClass()); |
| |
| |
| Object v2=10; |
| if (v2 instanceof Integer){ |
| System.out.println("v2是Integer类型"); |
| int i=(Integer) v2; |
| System.out.println(i); |
| } |
三 容器类型之List
List是一个接口,接口下面有两个常见的类型(目的是可以存放动态的多个数据)
- ArrayList,连续的内存地址的存储(底层基于数组,内部自动扩容) -> Python列表的特点
- LinkedList,底层基于链表实现(底层基于链表) -> Python列表的特点
3.1 基本使用
| import java.util.ArrayList; |
| import java.util.LinkedList; |
| |
| public class Demo05 { |
| public static void main(String[] args) { |
| |
| ArrayList ll=new ArrayList(); |
| ll.add("justin"); |
| ll.add(19); |
| ll.add(new Person()); |
| System.out.println(ll); |
| |
| |
| LinkedList ll2=new LinkedList(); |
| ll2.add("justin"); |
| ll2.add(19); |
| ll2.add(new Person()); |
| System.out.println(ll2); |
| |
| |
| } |
| } |
| |
| class Person{ |
| } |
| |
| |
3.2 接口概念(了解)
| import java.util.ArrayList; |
| import java.util.LinkedList; |
| |
| interface Duck { |
| public void speak(); |
| |
| public void run(); |
| } |
| |
| |
| |
| |
| class TDuck implements Duck { |
| @Override |
| public void speak() { |
| |
| } |
| |
| @Override |
| public void run() { |
| |
| } |
| } |
| |
| public class Demo05 { |
| public static void main(String[] args) { |
| |
| |
| } |
| } |
| |
| |
3.3 指定类型的List及常用操作
| import java.util.ArrayList; |
| import java.util.LinkedList; |
| |
| |
| public class Demo05 { |
| public static void main(String[] args) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| LinkedList<String> names = new LinkedList<String>(); |
| names.add("彭于晏"); |
| names.add("古天乐"); |
| names.add("杨颖"); |
| |
| names.add("刘亦菲"); |
| |
| String name = names.get(1); |
| System.out.println(name); |
| |
| |
| names.set(1,"迪丽热巴"); |
| System.out.println(names); |
| |
| |
| names.remove("迪丽热巴"); |
| names.remove(0); |
| |
| |
| System.out.println(names.size()); |
| |
| System.out.println(names.contains("刘亦菲")); |
| |
| |
| for (int i = 0; i < names.size(); i++) { |
| System.out.println(names.get(i)); |
| } |
| |
| for (String item:names) { |
| System.out.println(item); |
| |
| } |
| |
| |
| names.push("xxx"); |
| |
| names.addFirst("第一"); |
| names.addLast("倒数第一"); |
| System.out.println(names); |
| } |
| } |
| |
3.4 迭代
| |
| |
| |
| LinkedList<String> names = new LinkedList<String>(); |
| names.add("彭于晏"); |
| names.add("古天乐"); |
| names.add("杨颖"); |
| names.add("刘亦菲"); |
| Iterator i =names.iterator(); |
| while (i.hasNext()){ |
| System.out.println(i.next()); |
| } |
四 容器类型之 Set
Set是一个接口,常见实现这个接口的有两个类,用于实现不重复的多元素集合。
- HashSet,去重,无序。
- TreeSet,去重,内部默认排序(ascii、unicode)【不同的数据类型,无法进行比较】。
4.1 基本使用
| import java.util.Arrays; |
| import java.util.HashSet; |
| import java.util.TreeSet; |
| |
| public class Demo04 { |
| public static void main(String[] args) throws Exception { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| TreeSet<String> h = new TreeSet<String>(); |
| |
| |
| |
| h.add("刘亦菲"); |
| h.add("刘亦菲"); |
| h.add("迪丽热巴"); |
| h.add("古天乐"); |
| h.add("彭于晏"); |
| h.add("Justin"); |
| |
| System.out.println(h); |
| |
| |
| System.out.println(h.contains("迪丽热巴")); |
| |
| |
| h.remove("刘亦菲"); |
| System.out.println(h); |
| |
| System.out.println(h.size()); |
| |
| |
| } |
| } |
4.2 交并差集
| import java.util.Arrays; |
| import java.util.HashSet; |
| import java.util.TreeSet; |
| |
| public class Demo04 { |
| public static void main(String[] args) throws Exception { |
| HashSet<String> h1 = new HashSet<String>(); |
| h1.add("古天乐"); |
| h1.add("彭于晏"); |
| h1.add("Justin"); |
| |
| HashSet<String> h2 = new HashSet<String>(); |
| h2.add("古力娜扎"); |
| h2.add("杨颖"); |
| h2.add("迪丽热巴"); |
| h2.add("刘亦菲"); |
| h2.add("Justin"); |
| |
| HashSet<String> h3 = new HashSet<String>(); |
| h3.addAll(h2); |
| |
| System.out.println(h3); |
| |
| h3.retainAll(h1); |
| System.out.println(h3); |
| |
| h2.addAll(h1); |
| System.out.println(h2); |
| |
| |
| h2.removeAll(h1); |
| System.out.println(h2); |
| |
| |
| } |
| } |
| |
4.3 循环取值和迭代
| import java.util.HashSet; |
| import java.util.Iterator; |
| |
| public class Demo04 { |
| public static void main(String[] args) throws Exception { |
| HashSet<String> h1 = new HashSet<String>(); |
| h1.add("古天乐"); |
| h1.add("彭于晏"); |
| h1.add("Justin"); |
| |
| |
| for (String item: h1) { |
| System.out.println(item); |
| } |
| |
| Iterator i=h1.iterator(); |
| while (i.hasNext()){ |
| System.out.println(i.next()); |
| } |
| } |
| } |
五 容器类型之Map
Map是一个接口,常见实现这个接口的有两个类,用于存储键值对。
-
HashMap,无序。
-
TreeMap,默认根据key排序。(常用)
5.1 基本使用
| import java.util.HashMap; |
| import java.util.HashSet; |
| import java.util.Iterator; |
| import java.util.TreeMap; |
| |
| public class Demo04 { |
| public static void main(String[] args) throws Exception { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| TreeMap<String,String> hm=new TreeMap<String,String> (); |
| hm.put("name","lqz"); |
| hm.put("age","19"); |
| hm.put("hobby","篮球"); |
| System.out.println(hm); |
| |
| } |
| } |
| |
5.2 常用操作
| import java.util.*; |
| |
| public class Demo04 { |
| public static void main(String[] args) throws Exception { |
| TreeMap<String,String> hm=new TreeMap<String,String> (); |
| hm.put("name","lqz"); |
| hm.put("age","19"); |
| hm.put("hobby","篮球"); |
| System.out.println(hm); |
| |
| |
| hm.remove("name"); |
| System.out.println(hm); |
| |
| System.out.println(hm.size()); |
| |
| |
| System.out.println(hm.get("name")); |
| |
| |
| System.out.println(hm.containsKey("name")); |
| System.out.println(hm.containsValue("篮球")); |
| |
| |
| hm.replace("age","99"); |
| System.out.println(hm); |
| |
| |
| for (String key:hm.keySet()) { |
| System.out.println(hm.get(key)); |
| } |
| |
| |
| for (Map.Entry<String,String> item:hm.entrySet()) { |
| System.out.println(item); |
| System.out.println(item.getKey()); |
| System.out.println(item.getValue()); |
| |
| } |
| |
| |
| Set s2 = hm.entrySet(); |
| Iterator it2 = s2.iterator(); |
| while (it2.hasNext()) { |
| Map.Entry entry = (Map.Entry) it2.next(); |
| String k = (String) entry.getKey(); |
| String v = (String) entry.getValue(); |
| System.out.println(k); |
| System.out.println(v); |
| } |
| |
| |
| } |
| } |
| |
六 面向对象
6.1 类与对象构造方法
| public class Demo04 { |
| public static void main(String[] args) throws Exception { |
| |
| Person p =new Person(); |
| System.out.println(p); |
| |
| Person p1=new Person("Justin",99); |
| System.out.println(p1); |
| |
| } |
| } |
| |
| class Person { |
| public String name; |
| public int age; |
| |
| |
| |
| |
| public Person() { |
| this.name = "justin"; |
| this.age = 19; |
| |
| |
| } |
| |
| |
| |
| |
| |
| |
| public Person(String name, int age) { |
| this.name = name; |
| this.age = age; |
| |
| } |
| } |
6.1.1 方法重载
| import java.util.*; |
| |
| public class Demo04 { |
| public static void main(String[] args) throws Exception { |
| |
| Person p =new Person(); |
| System.out.println(p); |
| |
| Person p1=new Person("Justin",99); |
| System.out.println(p1); |
| |
| |
| p.speak(); |
| p.speak("说呀说"); |
| } |
| } |
| |
| class Person { |
| public String name; |
| public int age; |
| |
| |
| |
| |
| public Person() { |
| this.name = "justin"; |
| this.age = 19; |
| |
| |
| } |
| |
| |
| |
| |
| |
| |
| public Person(String name, int age) { |
| this.name = name; |
| this.age = age; |
| |
| } |
| |
| |
| |
| |
| public void speak(){ |
| System.out.println("说哈"); |
| } |
| |
| |
| |
| public void speak(String s){ |
| System.out.println(s); |
| } |
| } |
| |
6.1.2 Hook重载的方法
| /* |
| adb shell |
| su |
| cd /data/local/tmp/ |
| ./frida-server-16.0.19-android-arm64 |
| |
| 端口转发 |
| adb forward tcp:27042 tcp:27042 |
| adb forward tcp:27043 tcp:27043 |
| */ |
| |
| import frida |
| import sys |
| |
| |
| rdev = frida.get_remote_device() |
| |
| session = rdev.attach("油联合伙人") |
| |
| scr = """ |
| Java.perform(function () { |
| |
| // 包.类 |
| var MessageDigest = Java.use("java.security.MessageDigest"); |
| |
| MessageDigest.update.overload('[B').implementation = function(data){ |
| var res = this.update(data); |
| return res; |
| } |
| |
| MessageDigest.update.overload('java.nio.ByteBuffer').implementation = function(data){ |
| var res = this.update(data); |
| return res; |
| } |
| |
| }); |
| """ |
| script = session.create_script(scr) |
| |
| |
| def on_message(message, data): |
| print(message, data) |
| |
| |
| script.on("message", on_message) |
| |
| script.load() |
| sys.stdin.read() |
6.1.3 Hook构造方法
| import frida |
| import sys |
| |
| |
| rdev = frida.get_remote_device() |
| |
| session = rdev.attach("油联合伙人") |
| |
| scr = """ |
| Java.perform(function () { |
| |
| // 包.类 |
| var StringBuilder = Java.use("java.lang.StringBuilder"); |
| |
| StringBuilder.$init.overload('int').implementation = function(num){ |
| console.log(num); |
| if(num==32){ |
| var res = this.$init(num); |
| } |
| return res; |
| } |
| |
| }); |
| """ |
| |
| script = session.create_script(scr) |
| |
| |
| def on_message(message, data): |
| print(message, data) |
| |
| |
| script.on("message", on_message) |
| |
| script.load() |
| sys.stdin.read() |
6.2 静态成员(方法,变量)
| public class Demo04 { |
| public static void main(String[] args) throws Exception { |
| Person p =new Person(); |
| p.school="清华大学"; |
| Person.school="北京大学"; |
| System.out.println(p.school); |
| System.out.println(Person.school); |
| |
| p.run(); |
| Person.run(); |
| } |
| } |
| |
| class Person { |
| public static String school; |
| public String name; |
| public int age; |
| |
| |
| |
| |
| public Person() { |
| this.name = "justin"; |
| this.age = 19; |
| |
| } |
| |
| public static void run(){ |
| System.out.println("静态方法,走路,属于类"); |
| } |
| } |
6.3 继承
Java中的继承,只支持单继承,不支持多继承,但支持实现多个接口
6.3.1 java只支持单继承
| class Base { |
| } |
| class Foo { |
| } |
| class Person extends Foo { |
| } |
6.3.2 实现多个接口
| interface Base{ |
| public void speak(); |
| |
| } |
| interface Foo{ |
| public void run(); |
| |
| } |
| |
| class Person implements Base,Foo{ |
| |
| @Override |
| public void speak() { |
| |
| } |
| |
| @Override |
| public void run() { |
| |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)