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类的方法
| 方法摘要 | |
|   |            返回指定索引处的  | 
|  |            返回指定索引处的字符(Unicode 代码点)。 | 
|  |            返回指定索引之前的字符(Unicode 代码点)。 | 
|  |            返回此  | 
|  |            按字典顺序比较两个字符串。 | 
|  |            按字典顺序比较两个字符串,不考虑大小写。 | 
|  |            将指定字符串连接到此字符串的结尾。 | 
|  |            当且仅当此字符串包含指定的 char 值序列时,返回 true。 | 
|  |            将此字符串与指定的  | 
|  |            将此字符串与指定的  | 
|  |            返回指定数组中表示该字符序列的 String。 | 
|  |            返回指定数组中表示该字符序列的 String。 | 
|  |            测试此字符串是否以指定的后缀结束。 | 
|  |            将此字符串与指定的对象比较。 | 
|  |            将此  | 
|  |            使用指定的语言环境、格式字符串和参数返回一个格式化字符串。 | 
|  |            使用指定的格式字符串和参数返回一个格式化字符串。 | 
|  |            使用平台的默认字符集将此  | 
|  |            使用给定的 charset 将此  | 
|  |  已过时。 该方法无法将字符正确转换为字节。从 JDK 1.1 起,完成该转换的首选方法是通过 getBytes() 方法,该方法使用平台的默认字符集。 | 
|  |            使用指定的字符集将此  | 
|  |            将字符从此字符串复制到目标字符数组。 | 
|  |            返回此字符串的哈希码。 | 
|  |            返回指定字符在此字符串中第一次出现处的索引。 | 
|  |            返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。 | 
|  |            返回指定子字符串在此字符串中第一次出现处的索引。 | 
|  |            返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。 | 
|  |            返回字符串对象的规范化表示形式。 | 
|  |            当且仅当 length() 为 0 时返回 true。 | 
|  |            返回指定字符在此字符串中最后一次出现处的索引。 | 
|  |            返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。 | 
|  |            返回指定子字符串在此字符串中最右边出现处的索引。 | 
|  |            返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。 | 
|  |            返回此字符串的长度。 | 
|  |            告知此字符串是否匹配给定的正则表达式。 | 
|  |            返回此  | 
|  |            测试两个字符串区域是否相等。 | 
|  |            测试两个字符串区域是否相等。 | 
|  |            返回一个新的字符串,它是通过用  | 
|  |            使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。 | 
|  |            使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 | 
|  |            使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。 | 
|  |            根据给定正则表达式的匹配拆分此字符串。 | 
|  |            根据匹配给定的正则表达式来拆分此字符串。 | 
|  |            测试此字符串是否以指定的前缀开始。 | 
|  |            测试此字符串从指定索引开始的子字符串是否以指定前缀开始。 | 
|  |            返回一个新的字符序列,它是此序列的一个子序列。 | 
|  |            返回一个新的字符串,它是此字符串的一个子字符串。 | 
|  |            返回一个新字符串,它是此字符串的一个子字符串。 | 
|  |            将此字符串转换为一个新的字符数组。 | 
|  |            使用默认语言环境的规则将此  | 
|  |            使用给定  | 
|  |            返回此对象本身(它已经是一个字符串!)。 | 
|  |            使用默认语言环境的规则将此  | 
|  |            使用给定  | 
|  |            返回字符串的副本,忽略前导空白和尾部空白。 | 
|  |            返回  | 
|  |            返回  | 
|  |            返回  | 
|  |            返回  | 
|  |            返回  | 
|  |            返回  | 
|  |            返回  | 
|  |            返回  | 
|  |            返回  | 
约定
在实例方法中可以设置私有的变量:
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
天道酬勤
    
    









