java复习笔记0
HelloWorld:
运行下面代码
public class HelloWorld { public static void main(String args[]) { System.out.print("hellojava"); } }
classpath
classpath:
运行下面代码
//没有用引号哦 C:\>set classPath=H:\work\java C:\>java HelloWorld hellojava
public class和class声明的区别
public class的文件名称必须和类名称一致
class文件名称可以与类名称不一致
一个java文件只能有一个class拥有public 属性;
在java中:只要是引用类型的变量, 默认的值都为null, int类型的变量默认值为0;
int []arr = {1,2,3,4};
ArrayDemo, 声明多维数组, 以及java的数组都是使用的大括号作为标识符{}:
运行下面代码
public class ArrayDemo { public static void main(String args[]) { int arr[] = null; int[] arr1 = null; int arr2[] = new int[3]; int arr3[][] = new int[4][4]; System.out.println(arr); System.out.println(arr1); System.out.println(arr2); System.out.println(arr3); } }
java中要调用自己的静态方法, 直接写方法名字就好了:
运行下面代码
public class ArrayDemo2 { public static void run() { char c[] ={'h','e','l','l','o'}; for(int i=0; i<c.length; i++ ) { System.out.print(c[i]); } } public static void main(String args[]) { run(); System.out.println("\n"); System.out.println("hehe"); } }
java中调用静态方法和调用实例方法的区别:
运行下面代码
public class ArrayDemo2 { public static void run() { char c[] ={'h','e','l','l','o'}; for(int i=0; i<c.length; i++ ) { System.out.print(c[i]); } } public void run2() { System.out.println("method run2"); run(); } public static void main(String args[]) { run(); System.out.println("\n"); System.out.println("hehe"); ArrayDemo2 run2 = new ArrayDemo2(); run2.run2(); } }
java中方法的重载:
运行下面代码
public class methodDemo { public static void run() { char c[] ={'h','e','l','l','o'}; for(int i=0; i<c.length; i++ ) { System.out.print(c[i]); } } public static void run(int args[]) { for(int i=0; i<args.length; i++ ) { System.out.print(args[i]); } } public static void main(String args[]) { run(); int args1[] = {1,2,3,4}; run(args1); //run({1,2,3,4}); 直接调用还不行, 必须先声明一个变量; } }
System.arraycopy
java中的System.arraycopy :
运行下面代码
public class CopyDemo { //实现了Systyem.arraycopy方法; public static int[] copy(int arr[], int start, int[] arr1 ,int start1, int length) { for(int i=0; i<length; i++) { arr1[start1] = arr[start]; start1++; start++; } return arr; } public static void print(int arr[]) { for(int i=0; i<arr.length; i++) { System.out.print(arr[i]); } } public static void main(String args[]) { int[] arr = {10,100,5,4,6,20}; int[] arr1 = {0,0,0}; copy(arr, 2, arr1 , 0, 3); print(arr1); } }
java.util.Arrays.sort
实现java.util.Arrays.sort:
运行下面代码
public class SortDemo { //实现了 java.util.Arrays.sort; public static int[] sort(int arr[]) { for(int i=1; i<arr.length; i++) { for(int j=0; j<arr.length; j++) { if(arr[i]<arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } return arr; } public static void print(int arr[]) { for(int i=0; i<arr.length; i++) { System.out.print(arr[i]); } } public static void main(String args[]) { int[] arr = {10,100,5,4,6,20}; sort(arr); print(arr); } }
java1.5新增的特性
java1.5新增的特性, 可变参 :
运行下面代码
public class NewDemo { //可变参数, 这样更加灵活了; public static void print(int ... args) { for(int i=0; i<args.length; i++) { System.out.print(args[i]); } } public static void main(String args[]) { print(1,2,3,4,5,6,7); } }
java1.5中新增的特性, foreach循环数组:
运行下面代码
public class NewDemo2{ public static void main( String args[] ) { int arr[] = {2,2,3,4,5,6,7}; //num:arr或者num:arr都可以; for(int num : arr) { System.out.println(num); } } }
java.lang.NullPointerException将会伴随你们的开发终生
面向对象: 封装, 继承, 多态(重载);
同学们,java.lang.NullPointerException将会伴随你们的开发终生;
java中执行出来的代码保存在堆内存中;
构造函数Person,java中,如果不声明构造方法, 会自动生成一个构造方法,和普通方法一样, 构造方法也是支持重载的:
运行下面代码
public class Person{ public Person() { System.out.println("new Cons"); } public Person(String str) { System.out.println("new Cons:"+ str); } public static void main(String args[]) { Person per= new Person(); Person per1= new Person("str"); } }
匿名对象
匿名对象就是, 实例化构造函数时候,马上调用该实例的方法
运行下面代码
public class any{ private String name = "++"; public String getName() { return name; } public static void main(String arg[]) { System.out.println(new any().getName()); } }
java中的字符串的比较
java中的字符串的比较,equals和"=="的区别, == 包含了类型的比较, equals只是值得比较:
运行下面代码
public class StringEquals{ public static void main (String arg[]) { String str = "hello"; String str1 = new String("hello"); String str2 = str1; System.out.println("str == str1 :" + (str == str1)); System.out.println("str == str3 :" + (str == str2)); System.out.println("str1 == str2 :" + (str1 == str2)); System.out.println("str.equals(str1) :" + (str.equals(str1))); System.out.println("str.equals(str2) :" + (str.equals(str2))); System.out.println("str1.equals(str2) :" + (str1.equals(str2))); } }
String类的方法
方法摘要 | |
---|---|
char |
charAt(int index) 返回指定索引处的 char 值。 |
int |
codePointAt(int index)
返回指定索引处的字符(Unicode 代码点)。 |
int |
codePointBefore(int index)
返回指定索引之前的字符(Unicode 代码点)。 |
int |
codePointCount(int beginIndex,
int endIndex)
返回此 String 的指定文本范围中的 Unicode 代码点数。 |
int |
compareTo(String anotherString)
按字典顺序比较两个字符串。 |
int |
compareToIgnoreCase(String str)
按字典顺序比较两个字符串,不考虑大小写。 |
String |
concat(String str)
将指定字符串连接到此字符串的结尾。 |
boolean |
contains(CharSequence s)
当且仅当此字符串包含指定的 char 值序列时,返回 true。 |
boolean |
contentEquals(CharSequence cs)
将此字符串与指定的 CharSequence 比较。 |
boolean |
contentEquals(StringBuffer sb)
将此字符串与指定的 StringBuffer 比较。 |
static String |
copyValueOf(char[] data)
返回指定数组中表示该字符序列的 String。 |
static String |
copyValueOf(char[] data,
int offset,
int count)
返回指定数组中表示该字符序列的 String。 |
boolean |
endsWith(String suffix)
测试此字符串是否以指定的后缀结束。 |
boolean |
equals(Object anObject)
将此字符串与指定的对象比较。 |
boolean |
equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 比较,不考虑大小写。 |
static String |
format(Locale l,
String format,
Object... args)
使用指定的语言环境、格式字符串和参数返回一个格式化字符串。 |
static String |
format(String format,
Object... args)
使用指定的格式字符串和参数返回一个格式化字符串。 |
byte[] |
getBytes()
使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 |
byte[] |
getBytes(Charset charset)
使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。 |
void |
getBytes(int srcBegin,
int srcEnd,
byte[] dst,
int dstBegin)
已过时。 该方法无法将字符正确转换为字节。从 JDK 1.1 起,完成该转换的首选方法是通过 getBytes() 方法,该方法使用平台的默认字符集。 |
byte[] |
getBytes(String charsetName)
使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 |
void |
getChars(int srcBegin,
int srcEnd,
char[] dst,
int dstBegin)
将字符从此字符串复制到目标字符数组。 |
int |
hashCode()
返回此字符串的哈希码。 |
int |
indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。 |
int |
indexOf(int ch,
int fromIndex)
返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。 |
int |
indexOf(String str)
返回指定子字符串在此字符串中第一次出现处的索引。 |
int |
indexOf(String str,
int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。 |
String |
intern()
返回字符串对象的规范化表示形式。 |
boolean |
isEmpty()
当且仅当 length() 为 0 时返回 true。 |
int |
lastIndexOf(int ch)
返回指定字符在此字符串中最后一次出现处的索引。 |
int |
lastIndexOf(int ch,
int fromIndex)
返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。 |
int |
lastIndexOf(String str)
返回指定子字符串在此字符串中最右边出现处的索引。 |
int |
lastIndexOf(String str,
int fromIndex)
返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。 |
int |
length()
返回此字符串的长度。 |
boolean |
matches(String regex)
告知此字符串是否匹配给定的正则表达式。 |
int |
offsetByCodePoints(int index,
int codePointOffset)
返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引。 |
boolean |
regionMatches(boolean ignoreCase,
int toffset,
String other,
int ooffset,
int len)
测试两个字符串区域是否相等。 |
boolean |
regionMatches(int toffset,
String other,
int ooffset,
int len)
测试两个字符串区域是否相等。 |
String |
replace(char oldChar,
char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 |
String |
replace(CharSequence target,
CharSequence replacement)
使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。 |
String |
replaceAll(String regex,
String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 |
String |
replaceFirst(String regex,
String replacement)
使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。 |
String[] |
split(String regex)
根据给定正则表达式的匹配拆分此字符串。 |
String[] |
split(String regex,
int limit)
根据匹配给定的正则表达式来拆分此字符串。 |
boolean |
startsWith(String prefix)
测试此字符串是否以指定的前缀开始。 |
boolean |
startsWith(String prefix,
int toffset)
测试此字符串从指定索引开始的子字符串是否以指定前缀开始。 |
CharSequence |
subSequence(int beginIndex,
int endIndex)
返回一个新的字符序列,它是此序列的一个子序列。 |
String |
substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。 |
String |
substring(int beginIndex,
int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。 |
char[] |
toCharArray()
将此字符串转换为一个新的字符数组。 |
String |
toLowerCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为小写。 |
String |
toLowerCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。 |
String |
toString()
返回此对象本身(它已经是一个字符串!)。 |
String |
toUpperCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为大写。 |
String |
toUpperCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。 |
String |
trim()
返回字符串的副本,忽略前导空白和尾部空白。 |
static String |
valueOf(boolean b)
返回 boolean 参数的字符串表示形式。 |
static String |
valueOf(char c)
返回 char 参数的字符串表示形式。 |
static String |
valueOf(char[] data)
返回 char 数组参数的字符串表示形式。 |
static String |
valueOf(char[] data,
int offset,
int count)
返回 char 数组参数的特定子数组的字符串表示形式。 |
static String |
valueOf(double d)
返回 double 参数的字符串表示形式。 |
static String |
valueOf(float f)
返回 float 参数的字符串表示形式。 |
static String |
valueOf(int i)
返回 int 参数的字符串表示形式。 |
static String |
valueOf(long l)
返回 long 参数的字符串表示形式。 |
static String |
valueOf(Object obj)
返回 Object 参数的字符串表示形式。 |
约定
在实例方法中可以设置私有的变量:
运行下面代码
class Demo { private String name; public void fun(Demo de) { de.name = "hehe"; } public String getName() { return name; } } public class RefDemo { public static void main(String arg[]) { Demo demo = new Demo(); demo.fun(demo); System.out.println( demo.getName() ); } }
this
java中的构造函数和this;
运行下面代码
public class ThisDemo{ private String name; private int age; public ThisDemo() { System.out.println("init"); } public ThisDemo(String name) { this(); this.name = name; } public ThisDemo(String name, int age) { //this.name = name; this(name); this.age = age; } public static void main(String[] args) { ThisDemo demo = new ThisDemo("name",19); } }
static
使用static声明的属性或者方法,是所有实例公用的,
他的方法调用可以是通过如下代码实现:
运行下面代码
构造函数名.方法
方法
他的属性获取通过如下代码实现:
运行下面代码
构造函数.属性
属性
static的方法只能调用static的方法或者属性;
运行下面代码
public class staticDemo{ public static String str = "aaaa"; public static void main(String args[]) { System.out.print(str); } }
比如如下的代码是有问题的,因为静态方法不能调用实例方法:
运行下面代码
class C { public static String k = "str"; public static void run(){ this.print(); } public void print() { System.out.println(k); } } public class StaticDemo { public static void main(String args[]) { C c = new C(); c.run(); } }
代码块
代码块, 分为普通代码块, 构造块, 静态代码块, 同步代码块;
单例模式
就是给外部提供一个接口, 所有的变量都是static静态的, 代码里面随便写的意思:
运行下面代码
class Singleton{ private static Singleton singleton = new Singleton(); private Singleton() { } public static Singleton getSingleton() { return singleton; } } public class SigletonDemo { public static void main(String args[]) { Singleton sig = Singleton.getSingleton(); Singleton sig1 = Singleton.getSingleton(); System.out.print(sig == sig1); } }
对象数组
对象数组:
运行下面代码
class Person { private String name; public Person(String n) { this.name = n; } public void setName(String n) { this.name = n; } public String getName() { return this.name; } } public class PersonArray{ public static void main(String args[]) { //Person pers[] = new Person[2]; Person pers[] = {new Person("lili"), new Person("dada") }; System.out.println( pers ); } }
内部类
Java中的内部类, 用的很少:
运行下面代码
class OuterClass { String name = "nono"; class Inner{ public void getName() { System.out.println( name ); } } } public class InnerClass { public static void main(String args[]) { OuterClass out = new OuterClass(); OuterClass.Inner inner = out.new Inner(); inner.getName(); } }
end
本文作者:方方和圆圆
本文链接:https://www.cnblogs.com/diligenceday/p/5215346.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2014-03-03 最近新学的小东西和单词