Java:Java快速入门(java入门篇)

createh52个月前 (02-01)技术教程11

你好,世界!


源代码组织方式

Java程序由package+class组成,package对应目录的相对路径,class对应文件,如

E:\Workspaces\MyEclipse 10\JavaStudy\src\com\happyframework\javastudy\hello\Hello.java

package com.happyframework.javastudy.hello;
public final class Hello {
    public static void hello(){
        System.out.println("hello!");
    }
}

关于class有如下几点规则:

  1. 文件的名字必须和class的名字一致(public级别的class名字)。
  2. 文件必须只包含一个public访问基本的class(可以包含多个非public级别的class)。
  3. package名字必须和目录一致。

入口方法

App.java

 public class App {
    public static void main(String[] args) {
         com.happyframework.javastudy.hello.Hello.hello();
    }
}

最终的项目结构

数据类型8种原子类型

  1. 整数类型:byte、short、int和long。
  2. 小数类型:float和double。
  3. 字符类型:char。
  4. 布尔类型:bool。

除此之外的是interface、class和array。

小数类型的常量默认是double类型,声明float类型的常量需要使用F作为后缀。

public class Program {
    /**
     * @param args
     */
    public static void main(String[] args) {
            float age = 28.0F;
            System.out.println(age);
    }
}

运算符

  1. 算术运算符:+、-、*、/ 和 %,两个整数相除,结果还是整数。
  2. 赋值运算符:=、+=、-=、*=、/=、%=、&=、|=、~=、^=、<<=、>>= 、 >>>=、++ 和 --。
  3. 比较运算符:==、!=、<、<=、> 和 >=。
  4. 逻辑运算符:&&、|| 和 !。
  5. 位运算符:&、|、~、^、<<、>> 和 >>>。

字符串

String是拥有“值语义”的引用类型,字符串常量实现了“享元模式”,equals会按照内容进行比较,==按照地址比较。

public class Program {
    /**
     * @param args
     */
    public static void main(String[] args) {
        String x = "段光伟";
        String y = new String("段光伟");
        System.out.println(x.equals(y)); // true
        System.out.println(x == y); // false
    }
}

为了高效地修改字符串Java引入了StringBuffer。

{
            StringBuffer sb =
                    new StringBuffer()
                    .append("段")
                    .append("光")
                    .append("伟");
            System.out.println(sb.toString());
        }

数组

声明语法

DataType[] name 或 DataType name[]。

初始化语法

DataType[] name = new DataType[length]。

DataType[] name = new DataType[] { element1, element2, ...elementn }。

DataType[] name = { element1, element2, ...elementn }。

public class Program {
    /**
     * @param args
     */
    public static void main(String[] args) {
        {
            String[] strs = { "段", "光", "伟" };
            for (String item : strs) {
                System.out.print(item);
            }
        }
    }
}

多维数组

只有不等长多维数组DataType[][],没有DataType[xxx, xxx]。

控制结构

  1. 条件:if-else if-else、switch-case-default和三元运算符(?:)。
  2. 循环:while、do-while、for和foreach。
  3. Labeled block。
public class Program {
    /**
     * @param args
     */
    public static void main(String[] args) {
        task: {
            int age = 25;
            System.out.println("start");
            if (age < 30) {
                break task;
            }
            System.out.println("end");
        }
    }
}

最近觉得label是个不错的东西,最起码多了一种选择。

方法

Java中所有的赋值和方法调用都是“按值“处理的,引用类型的值是对象的地址,原始类型的值是其自身。

Java支持变长方法参数。

public class Program {
    /**
     * @param args
     */
    public static void main(String[] args) {
        print("段光伟", "段光宇");
        print(new String[] { "段光伟", "段光宇" });
    }
    private static void print(String... args) {
        for (String item : args) {
            System.out.println(item);
        }
    }
}

public class Program {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Point point = new Point(100);
        System.out.print(point);
    }
}
class Point {
    private int x = 0;
    private int y = 0;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public Point(int x) {
        this(x, x);
    }
    public String toString() {
        return "(x:" + this.x + ",y:" + this.y + ")";
    }
}

注意:调用自身的构造方法是用this(xxx,xxx,...)来完成,且必须位于第一行。

静态成员

Java中类似静态构造方法的结构,称之为:静态初始化代码块,与之对应的是实例初始化代码块,见下例:

public class Program {
    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(Point.getValue());
        System.out.println(new Point());
    }
}
class Point {
    private static int value = 0;
    public static int getValue() {
        return value;
    }
    static {
        value++;
    }
    static {
        value++;
    }
    private int x = 0;
    private int y = 0;
    {
        this.x = 10;
    }
    {
        this.y = 10;
    }
    public String toString() {
        return "(x:" + this.x + ",y:" + this.y + ")";
    }
}

继承

继承使用 extends,抽象类和抽象方法使用abstract声明,向下转型使用 (ChildType)instance,判断是否是某个类型使用 instanceof,见下例:

public class Program {
    /**
     * @param args
     */
    public static void main(String[] args) {
        printAnimal(new Animal());
        printAnimal(new Dog());
    }
    private static void printAnimal(Animal animal) {
        if(animal instanceof Dog){
            System.out.println("I am a " + (Dog) animal);
        }
        else
        {
            System.out.println("I am an " + animal);
        }
    }
}
class Animal {
    public String toString() {
        return "Animal";
    }
}
class Dog extends Animal {
    public String toString() {
        return "Dog";
    }
}

重写

Java中的重写规则比较灵活,具体如下:

  1. 除了 private 修饰之外的所有实例方法都可以重写,不需要显式的声明。
  2. 重写的方法为了显式的表达重写这一概念,使用 @Override进行注解。
  3. 重写的方法可以修改访问修饰符和返回类型,只要和父类的方法兼容(访问级别更高,返回类型更具体)。
  4. 可以使用final将某个方法标记为不可重写。
  5. 在构造方法中使用 super(xxx, xxx)调用父类构造方法,在常规实例方法中使用 super.method(xxx, xxx)调用父类方法。
  6. Java不支持覆盖(new)。
public class Program {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Animal animal = new Animal();
        Animal dog = new Dog();
        animal.say();
        dog.say();
        animal.eat(animal);
        dog.eat(dog);
        System.out.println(animal.info());
        System.out.println(dog.info());
    }
}
class Animal {
    private String name = "Animal";
    protected void say() {
        System.out.println("Animal" + " " + this.name);
    }
    public void eat(Animal food) {
        System.out.println("Animal eat " + food);
    }
    public Object info() {
        return "Animal";
    }
    @Override
    public String toString() {
        return "Animal";
    }
}
class Dog extends Animal {
    private String name = "Dog";
    @Override
    public final void say() {
        System.out.println("Dog" + " " + this.name);
    }
    @Override
    public final void eat(Animal food) {
        super.eat(food);
        System.out.println("Dog eated");
    }
    @Override
    public final String info() {
        return "Dog";
    }
    @Override
    public final String toString() {
        return "Dog";
    }
}

包的名字和项目路径下的目录路径相对应,比如:项目路径为:C:\Study,有一个Java源文件位于:C:\Study\com\happyframework\study\App.java,那么App.java的包名字必须为:com.happyframework.study,且 App.java 的第一行语句必须为:package com.happyframework.study。

Java支持三种导入语法:
导入类型:import xxx.xxx.xxxClass。
导入包:import xxx.xxx.xxx.*。
导入静态成员:import static xxx.xxx.*。
import static util.Helper.*;
public class Program {
    /**
     * @param args
     */
    public static void main(String[] args) {
        puts("段光伟");
    }
}

访问级别

Java支持四种访问级别:public、private、protected 和 default(默认),类型和接口只能使用public 和 default,成员和嵌套类型可以使用所有,下面简单地解释一下 protected 和 default。

  • protected 修饰过的成员只能被自己、子类和同一个包里的(不包括子包)其他类型访问。
  • default 修改过的类型或成员只能被自己和同一个包里的(不包括子包)其他类型访问。

嵌套类

Java支持如下几种嵌套类:

  1. nested class,定义在类型内部的类型。
    1. static nested class,使用 static 声明的 nested class,static nested class 可以访问所有外部类的静态成员。
    2. inner class,没有使用 static 声明的 nested class,inner class 可以访问所有外部类的实例成员,inner class 不能定义静态成员。

代码示例

public class Program {
    /**
     * @param args
     */
    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();
        OuterClass.InnerClass.InnerInnerClass innerInner = inner.new InnerInnerClass();
        outer.show();
        inner.show();
        innerInner.show();
        OuterClass.StaticNestedClass staticNested=new OuterClass.StaticNestedClass();
        OuterClass.StaticNestedClass.StaticNestedNestedClass staticNestedNested=new OuterClass.StaticNestedClass.StaticNestedNestedClass();
        staticNested.show();
        staticNestedNested.show();
    }
}
class OuterClass {
    int x = 1;
    static int i = 1;
    void show() {
        System.out.println(x);
        System.out.println(i);
    }
    class InnerClass {
        int y = 2;
        void show() {
            System.out.println(x);
            System.out.println(y);
        }
        class InnerInnerClass {
            int z = 3;
            void show() {
                System.out.println(OuterClass.this.x);
                System.out.println(y);
                System.out.println(z);
            }
        }
    }
    static class StaticNestedClass {
        static int j = 2;
        void show() {
            System.out.println(i);
            System.out.println(j);
        }
        static class StaticNestedNestedClass {
            static int k = 3;
            void show() {
                System.out.println(i);
                System.out.println(j);
                System.out.println(k);
            }
        }
    }
}

特殊的inner class:local class

public class LocalClassExample {
    static String staticValue = "static value";
    String instanceValue = "instance value";
    public void test() {
        final String finalLocalValue = "final local value";
        class LocalClass {
            void test() {
                System.out.println(staticValue);
                System.out.println(instanceValue);
                System.out.println(finalLocalValue);
            }
        }
        LocalClass local = new LocalClass();
        local.test();
    }
}

除了inner class的规则之外,local class可以访问局部final变量,在Java8中有更多的改进。

特殊的local class:anonymous class

public class Program {
    /**
     * @param args
     */
    public static void main(String[] args) {
        execute(new Action() {
            @Override
            public void execute() {
                System.out.println("执行业务逻辑");
            }
        });
    }
    static void execute(Action action) {
        System.out.println("事物开始");
        action.execute();
        System.out.println("事物结束");
    }
}
interface Action {
    void execute();
}

常量

不废话了,直接看代码:

public final class Program {
    static final String STATIC_CONSTANTS = "STATIC_CONSTANTS";
    final String INSTANCE_CONSTANTS = "INSTANCE_CONSTANTS";
    public static void main(String[] args) {
        final String LOCAL_CONSTANTS = "LOCAL_CONSTANTS";
        System.out.println(STATIC_CONSTANTS);
        System.out.println(new Program().INSTANCE_CONSTANTS);
        System.out.println(LOCAL_CONSTANTS);
        new Program().test("PARAMETER_CONSTANTS");
    }
    public final void test(final String msg) {
        System.out.println(msg);
    }
}

有一点需要注意的是:只有一种情况Java的常量是编译时常量(编译器会帮你替换),其它情况都是运行时常量,这种情况是:静态类型常量且常量的值可以编译时确定。

接口

Java的接口可以包含方法签名、常量和嵌套类,见下例:

public final class Program {
    public static void main(String[] args) {
        Playable.EMPTY.play();
        new Dog().play();
    }
}
interface Playable {
    Playable EMPTY = new EmptyPlayable();
    void play();
    class EmptyPlayable implements Playable {
        @Override
        public void play() {
            System.out.println("无所事事");
        }
    }
}
class Dog implements Playable {
    @Override
    public void play() {
        System.out.println("啃骨头");
    }
}

枚举

Java枚举是class,继承自java.lang.Enum,枚举中可以定义任何类型可以定义的内容,构造方法只能是private或package private,枚举成员会被编译器动态翻译为枚举实例常量,见下例:

public final class Program {
    public static void main(String[] args) {
        System.out.println(State.ON);
        System.out.println(State.OFF);
        for (State item : State.values()) {
            System.out.println(item);
            System.out.println(State.valueOf(item.name()));
        }
    }
}
enum State {
    ON(1), OFF(0);
    int value = 1;
    State(int value) {
        this.value = value;
    }
}

调用枚举的构造方法格式是:常量名字(xxx, xxx),如果构造方法没有参数只需要:常量名子,如:

1 enum State {

异常

Java中的异常分为checked和unchecked,checked异常必须声明在方法中或被捕获,这点我觉得比较好,必定:异常也是API的一部分,见下例:

public final class Program {
    public static void main(String[] args) {
        try {
            test();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    public static void test() throws Exception {
        throw new Exception("I am wrong!");
    }
}

所有继承Exception的异常(除了RuntimeException和它的后代之外)都是checked异常。

装箱和拆箱

Java提供了原始类型对应的引用类型,在1.5之后的版本还提供了自动装箱和自动拆箱,结合最新版本的泛型,几乎可以忽略这块。

import java.util.*;
public final class Program {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(1);
        int item1 = (Integer) list.get(0);
        System.out.println(item1);
    }
}

注意:自动装箱和自动拆箱是Java提供的语法糖。

泛型

Java的泛型是编译器提供的语法糖,官方称之为:类型参数抹除,先看一下语法,然后总结一点规律:

泛型方法

测试代码

static  void puts(T msg) {
        println(msg);
    }
    static void println(Object msg) {
        System.out.println("Object:" + msg);
    }
    static void println(String msg) {
        System.out.println("String:" + msg);
    }

调用泛型方法

System.out.println("generic method test");
         puts("hello");
         Program. puts("hello");

输出的结果是

 generic method test
 Object:hello
Object:hello

泛型类

测试代码

class TestGenericClass {
    T value;
    void setValue(T value) {
        this.value = value;
    }
}

调用代码

1         System.out.println("generic class test");
2         System.out.println(t.value);

输出结果

1 generic class test
2 1

泛型接口

测试代码

interface TestInterface {
    void test(T item);
}
class TestInterfaceImp1 implements TestInterface {
    @Override
    public void test(String item) {
        System.out.println(item);
    }
}
class TestInterfaceImp2 implements TestInterface {
    @Override
    public void test(T item) {
        System.out.println(item);
    }
}

调用代码

System.out.println("generic interface test");
        TestInterface testInterface1 = new TestInterfaceImp1();
        testInterface1.test("hi");
        for (Method item : testInterface1.getClass().getMethods()) {
            if (item.getName() == "test") {
                System.out.println(item.getParameterTypes()[0].getName());
            }
        }
        TestInterface testInterface2 = new TestInterfaceImp2<>();
        testInterface2.test("hi");
        for (Method item : testInterface2.getClass().getMethods()) {
            if (item.getName() == "test") {
                System.out.println(item.getParameterTypes()[0].getName());
            }
        }

输出结果

generic interface test
hi
java.lang.String
java.lang.Object
hi
java.lang.Object

类型参数约束

测试代码

class Animal {
}
class Dog extends Animal {
}
class Base {
    public void test(T item) {
        System.out.println("Base:" + item);
    }
}
class Child extends Base {
    @Override
    public void test(Dog item) {
        System.out.println("Child:" + item);
    }
}

调用代码

System.out.println("bounded type parameters test");
        Base base = new Child();
        base.test(new Dog());
        for (Method item : base.getClass().getMethods()) {
            if (item.getName() == "test") {
                System.out.println(item.getParameterTypes()[0].getName());
            }
        }

输出结果

bounded type parameters test
Child:Dog@533c2ac3
Dog
 Animal

类型搽除过程

  1. 将泛型定义中的类型参数去掉。
class Base {
    public void test(T item) {
        System.out.println("Base:" + item);
    }
}

2.将T换成extends指定的约束类型,默认是Object。

class Base {
     public void test(Animal item) {
         System.out.println("Base:" + item);
     }
 }

3.如果有非泛型类型继承或实现了泛型基类或接口,而且进行了重写,根据情况,编译器会自动生成一些方法

class Child extends Base {
    @Override
    public void test(Animal item) {
        this.test((Dog)item);
    }
    public void test(Dog item) {
        System.out.println("Child:" + item);
    }
}

4.根据泛型参数的实际参数搽除调用代码。

System.out.println("bounded type parameters test");
        Base base = new Child();
        base.test(new Dog());
        for (Method item : base.getClass().getMethods()) {
            if (item.getName() == "test") {
                System.out.println(item.getParameterTypes()[0].getName());
            }
        }

这里说的不一定正确,特别是Java泛型的约束支持&(如:可以约束实行多个接口),不过过程估计差别不大,我没有看Java语言规范,这里只是大概的猜测。

相关文章

Java开发中的加密、解密、签名、验签,密钥,证书,这篇就够了

先说一下两个重要的工具OpenSSL:OpenSSL整个软件包大概可以分成三个主要的功能部分:SSL协议库libssl、应用程序命令工具以及密码算法库libcrypto。它使用标准的文件格式(PEM/...

实现Java与前端之间国密算法加解密与签名

问题开发中发现,我们采用Java或者NodeJS的国密SM2加密、解密、签名单边操作等正常,但是通过Nodejs加密或者生成的签名在Java下就不能解密或者不能验签。整个百度都看了,还是没有搞定通过国...

源码分享:在pdf上加盖电子签章(pdf加盖电子签名)

在pdf上加盖电子签章,并不是只是加个印章图片,。而是要使用一对密钥中的私钥对文件进行签字。为啥要用私钥呢?很简单,因为公钥是公开的,其他人才可以用公钥为你证明,这个文件是你签的。这就是我们常说的:私...

Java程序员学习Typescript初级篇(java程序猿)

引言对于 Java 程序员来说,TypeScript(简称 TS)是一门非常值得学习的语言。TS 是 JavaScript 的一个超集,它在 JavaScript 的基础上添加了静态类型系统,这与 J...

软件申请代码签名证书(代码签名证书时出错30005)

使用代码签名证书为您的可执行文件添加数字签名并消除安全警告。当最终用户下载您的软件时,他们可以确信您的代码是安全的,并且自签名以来从未被篡改过。代码签名证书及其私钥必须存储在符合FIPS标准的硬件安全...

java代码规范,你了解多少?(一段java代码的详细解说)

一、为啥要有代码规范?1.代码规范可以加快团队间的协作对于每个项目的开发,大多数是由一个团队来完成的,团队内部的人来自四面八方,每个人的代码风格也大不相同,如果没有统一的代码规范,那么代码的可读性会大...