javase快速入门

public class Demo02 {
    public static void main(String[] args){
        //八大基本数据类型
        int num1 =10;//最常用
        byte num2=20;
        short num3=30;
        long num4=30L;//long类型要在数字后面加个L
        //小数:浮点数
        float num5=50.1F;
        double num6=3.1414;
        //字符
        char name='a';
        //字符串,string不是关键字,是类
        //String namea="qingjian";

        //布尔值
        boolean flag=true;
        //boolean flag=false;


    }
}
public class Dome03 {
    public static void main(String[] args){
        //整数拓展, 进制
        int i =10;
        int i2=010;
        int i3=0x10;
        System.out.println(i);
        System.out.println(i2);

        ///______________
        //浮点数扩展,银行业务怎么表示?钱?


        //BigDecimal 数学工具类
        //float  有线 离散 大约
        //double
        //最好完全不使用浮点数比较!!!!!!!!
        //最好完全不使用浮点数比较!!!!!!!!
        //最好完全不使用浮点数比较!!!!!!!!
        //最好完全不使用浮点数比较!!!!!!!!

        float f= 0.1f;//0.1
        double d = 1.0/10;//0.1
        System.out.println(f==d );//false

        float d1=23232232323232323f;
        float d2 =d1+1;
        System.out.println(d1==d2);



        //_______________________________________
        //字符拓展
        char c1= 'a';
        char c2= '中';
        System.out.println(c1);
        System.out.println((int)c1);//强制转换
        System.out.println(c2);
        System.out.println((int)c2);//强制转换
        //所有点字符本质还是数字
        //编码 Unicoda 2字节          Excel 65535

        char c3 ='\u0061';
        System.out.println(c3);
        //转义字符
        //   \t
        //  \n 换行
        System.out.println("hello\tworld");

        //对象,从内存分析

        //布尔值扩展
        boolean flag=true;
        if(flag==true) {}//新手
        if (flag){}//老手
        //less is more 代码要精简易读

    }

}

public class Dome05 {
    public static void main(String[] args ){
        int i =128;
        byte b =(byte)i;//内存溢出
        //强制转换   (类型)变量名 高到低
        //自动转换    低到高
        System.out.println(i);
        System.out.println(b);


        /*注意点
        * 1不能对布尔值进行转换
        * 不能吧对象类型转换为不相干的类型
        * 在把高容量转换到低容量的时候,强制转换
        * 转换的时候可能存在内存溢出,或者精度问题
        *
        * */
        System.out.println((int)23.7); //23
        System.out.println((int)-45.89f );//-45

        System.out.println("=======================");
        char c ='a';
        int d =c+1;
        System.out.println(d);
        System.out.println((char)d);




    }

}

import jdk.nashorn.internal.parser.JSONParser;

import java.util.Arrays;

public class Dome06 {
    public static void main(String[] args ){
        //操作比较大的数的时候会,注意溢出问题
        //jdk7新特性,数字之间可以用下划线分割
        int money = 10_0000_0000;
        System.out.println(money);
        int years=20;
        int total =money*years;
        System.out.println(total);

        long total3=money*((long)years);
        System.out.println(total);
        
    }
}

/

public class Dome07 {
    public static void main(String[] args){
        //int a,b,c;
        int a=1,b=2,c=3;//程序的可读性
        String name="qiangjiang";
        char x='x';
        double pi =3.14;



    }
}
public class Demo08 {
    //类变量 static
    static double salary=2500;


    //属性:变量

   //实列变量,从属于对象,如果不执行初始化,这个类型的默认值 0 0.0
    //布尔值默认是false
    //除了基本类型,其他默认都是null
    String name;
    int age;


    //main方法
    public static  void main(String[] args){
        //局部变量,必须声明和初始化值
        int i=10;
        System.out.println(i);
        //变量类型 变量名字=new Demo08();
        Demo08 demo08=new Demo08();

        System.out.println(salary);
    }


    //其他方法
    public void add(){

    }
}

package operator;

import static java.lang.Math.pow;

public class Demo04 {
    public static void main(String[] args) {
        // ++ --自增 自减 一元运算符
        int a= 3;
        int b = a++;//执行完这行代码后,先给b赋值,再自增
        //a++ a=a+1
        System.out.println(a);

        int c =++a;//++a    a=a+1
        //执行这行代码前,先自增,在赋值
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        //幂运算 我们会用一些工具类来操作
        double pow= Math.pow(2,3);

        System.out.println(pow);
        


    }
}

package operator;

public class Demo05 {
    public static void main(String[] args) {
        //与 (and) 或(or) 非(取反)
        boolean a =true;
        boolean b=false;

        System.out.println("a && b"+(a&&b));//
        System.out.println("a || b");
        System.out.println("!(a&&b)");

        //短路运算,
        int c =5;
        //boolean d = (c<<4)&&(c++<4);
        //System.out.println(d);
        System.out.println(c);
package operator;

public class Dempo06 {
    public static void main(String[] args) {
        //位运算
        /*
        * A=0011 1100
        * B=0000 1101
        * __________
        * A&B
        * A|b
        *A^B
        * */

        /*
        * _____________________________________________
        * 面试题。
        * 2*8=16
        * 2*2*2*2
        * <<  左移   *2
        * >> 右移    /2
        * 0000 0000    0
        * 0000 0001    1
        * 0000 0010    2
        * 0000 0011    3
        * 0000 0010    4
        * 0000 0100    8
        *
        *
        *效率及其高
        * */
package operator;

public class Demo07 {
    public static void main(String[] args) {
        int a= 10;
        int b=20;
        a+=b;//a=a+b
        System.out.println(a);

        //字符串连接符  + ,String

        System.out.println(""+a+b);
        System.out.println(a+b+"");
    }
}
package operator;

public class Demo08 {
    public static void main(String[] args) {
        // x?y:z
        //如果 x==true 则结果为y,否则为z

        int score=50;
        String type =score <60 ?"不及格":"及格";
        //代码更精简,开发常用,必须会
        //if
        System.out.println(type);


    }
}
package com.kuang.scanner;

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘数据
        Scanner scanner=new Scanner(System.in);
        System.out.println("使用next方法接受");
        //判断用户有没有输入字符串
        if (scanner.hasNext()){
            //使用next方式接收
            String str=scanner.next();
            System.out.println("输出的内容为"+str);

        }//凡是属于io流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
            //io输入输出流,和电脑打交道的
        scanner.close();
    }
}


package com.kuang.struct;

import java.util.Scanner;

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

        Scanner scanner=new Scanner(System.in);

        System.out.print("请输入");
        String s= scanner.nextLine();

        //equals 判断字符窜是否相等
        if (s.equals("hello" )){
            System.out.println(s);
        }else{
            System.out.println("end");
        }




        scanner.close();
    }
}

package com.kuang.struct;

public class SwitchDemo02 {
    public static void main(String[] args) {
        String name="狂神";
    //jdk7新特性,表达式结果可以说字符串
        //字符的本质还是数字
        //反编译 java---class(字节码文件)---反编译(idea)
        //每一个对象都有自己的hashcode,通过算法生成
        switch (name){
            case "秦疆":
                System.out.println("秦疆");
                break;
            case "狂神":
                System.out.println("狂神");
                break;
            default:
                System.out.println("");

        }

    }
package com.kuang.struct;

public class ForDemo04 {
    public static void main(String[] args) {
        for (int i = 1; i <=9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j+"*"+i+"="+(j*i)+"\t");

            }

            System.out.print("\n");
        }
    }
}


//dig
package com.kuang.method;

public class Demo06 {
    public static void main(String[] args) {
        System.out.println(f(5));
    }
    //1!
    //5! 5*4*3*2*1
    public static int f(int n){
        if(n==1){
            return 1;

        }else{
            return n*f(n-1);
        }
    }
}

package com.kuang.array;

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


        /**
         * 冒泡排序
         * 比较数组中,两个相邻的元素,如果第一个数比第二个数打,我们就交换他们都位置
         * 每一次比较,都会产生出一个最大,或者最小的数字
         * 下一轮则可以少一次排序
         * 依次循环,直到结束
         *
         */
        int []a={1,12,13,14,15};
        int[] sort=sort(a);//调用完自己写的排序方法以后,返回一个排序后的数组
        //System.out.println(Arrays.toString(sort));
    }
    public static int[] sort(int[]array){
        //临时变量,第三方变量
        int temp=0;
        //外层循环,判断我们这个要走多少次
        for (int i=0 ;i<=array.length;i++){
            boolean flag=false;//通过flag标识位减少没有意义的比较
            //内层循环,比较判断两个数,如果第一个数比第二个数大,则交换位置
            for (int j =0;j<=array.length-1-i;j++){
                if (array[j+1]>array[j]){
                    temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                    flag=true;
                }
            }
            if (flag==false){
                break;
            }

        }


        return array;
    }
}
package com.kuang.oop;

public class Demo02 {
    //静态方法static
    //非静态方法
    //实例化类new
    //对象类型,对象值=对象值

    Student student=new Student();
    //student.say();


    /**
     * static和类一起加载
     * 无static,不和类一起加载,无法直接调用
     */

package com.kuang.oop.demo01;

//值传递

public class Demo04 {
    public static void main(String[] args) {
        int a=1;
        System.out.println(a);//1
        Demo04.change(a);
        System.out.println(a);//1



    }
    public static void change(int a){
        a=10;
    }

}

package com.kuang.oop.demo01;

//引用传递:对象,本质还是值传递
public class Demo05 {
    public static void main(String[] args) {
        Perosn perosn=new Perosn();
        System.out.println(perosn.name);//null
        Demo05.change(perosn);
        System.out.println(perosn.name);//qingjaing
    }
    public static void change(Perosn perosn){
        perosn.name="qingjiang";
    }

}
//定义一个Person类,有一个属性name
class Perosn{
    String name ;//null
}

package com.kuang.oop.demo02;
//java------>class
public class Person {
    String name;

    //实例化初始值


    //构造器初始值

    /**
     * 使用new关键字,必须要有构造器,本质是在调用构造器
     * 用来初始化值
     */
    public Person(){
        this.name="qingjiang";


    }
    //有参构造.一但定义了有参数构造,无参构造就无法显示的定义
    public Person(String name){
        this.name= name;
    }


}


//一个类,即使什么都不写。它也会存在一个方法


/**
 *
 * 构造器
 * 和类名相同
 * 没有返回值
 *
 * new本质在调用构造方法
 * 初始化对象的值
 * 注意点
 * 定义有参构造之后,如果想使用无参构造,显示的定义一个无参的构造
 * Alt+Insert
 * 
 */

/*类与对象
类是一个模板,抽象,对象是一个具体的实例
方法
定义,调用

对应的引用
引用类型:基本类型(8)
对象是通过引用来操作的:栈————》堆

属性:字段Field成员变量
默认初始化
	数字 :0  0.0
	char : u00000
	boolean: false
	引用:  null
	
修饰符  属性类型 属性名=属性值

方法

对象的创建和使用

必须用new关键字创造对象,构造器 Person kuangsheng =new Person();

对象的属性kuangshen.name
对象的方法kuangshen.sleep()

类
静态的属性 属性
动态的行为 方法


封装,继承,多态


*/
package com.kuang.oop.demo05;

public class Student extends Person{
    public static void main(String[] args) {
        Student student=new Student();
        student.say();
        //子类继承了父类,就会拥有父类的全部方法
        //ctrl+h继承树
        // 在java中,所有点类,都默认继承object

    }
}

package com.kuang.oop.demo05;

public class Student extends Person{
    public static void main(String[] args) {
        Student student=new Student();
        student.say();
        //子类继承了父类,就会拥有父类的全部方法
        //ctrl+h继承树
        // 在java中,所有点类,都默认继承object
        //父类私有的,无法被继承


        //隐藏代码,调用了父类的无参构造
        //调用父类的构造器,必须写在子类构造器第一行
        /**
         *
         * super注意点
         * super调用父类的构造方法,必须在构造方法的第一个
         * super必须只能出现在子类的方法或者构造方法中
         * super和this不能同时调用构造方法
         *
         *
         * vs this
         * 代表的对象不同
         *this:本身调用者这个对象
         * super;代表父类对象的应用
         * 前提
         * this 没有继承也可以使用
         * super:只有在继承条件才可以用
         *构造方法
         * this()本类的构造
         *super()父亲的构造
         */


    }
}

package com.kuang.oop.demo05;

public class A extends B{
    //Override 重写

    @Override
    public void test() {
        super.test();
    }

    public static void main(String[] args) {
        //静态方法和非静态方法区别很大
        //静态方法。//方法的调用只和左边,定义的数据类型有关

        //静态方法对于类。
        //非静态方法对于对象
        A a=new A();
        a.test();//A

        //父类的引用指向子类
        B b =new A();//子类重写的父类的方法
        b.test();


    }
}
/**
 * 重写:需要有继承关关系,子类重写父类的方法
 * 1方法名必须相同
 * 2参数列表必须相同
 * 3修饰符:范围可以扩大,但是不能缩小。public>protected>default>pribate
 * 4抛出异常:范围,可以被缩小,但是 不能扩大 ClassNotFoundExcetpion-->Exctption
 *
 * 重写。子类的方法和父类的必须一致,方法体不同
 *
 * 为什么需要重写
 * 1 父类的功能,子类不一定需要,或者不一定满足
 * alt+insert :overide
 */


package com.kuang.oop.demo06;

public class Student extends Person{


    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //new Student();
        //new Person();


        //可以指向的引用类型就不确定了:父类的引用指向子类
        //Student 能调用的方法都是自己的或者是继承父类的

        Student s1=new Student();
        //person父类型,可以指向子类,但是不能调用子类独有的方法
        Person s2=new Student();
        Object s3 =new Student();
        //对象能执行那些方法,主要看对象左边的类型,和右边关系不大
        ((Student)s2).eat();//子类重写了父类的方法,执行子类的方法
        s2.run();

    }

    /**
     * 多态注意事项
     * 1多态是方法的多态,属性没有多态
     * 2父类和子类,有联系 类型转换异常 ClassCastException
     * 3存在条件,继承关系,方法需要重写,父类引用指向子类对象father f1 =new Son();
     *不能被重写
     * 1 static方法,属于类,它不属于实列
     * 2 final 常量
     * 3 private 方法
     */
}

package com.kuang.oop.demo08;

public abstract class Action {
    //约束 有人帮我们实现
    //abstract 抽象方法,只有方法名字,没有方法的实现
    public abstract void doSomething();
}
//不能new这个抽象列,只能靠子类去实现它,约束
//抽象类的所有方法,继承了它的子类,都必须要实现它的方法,除非子类也是抽象类
//抽象类中可以写普通的方法
//抽象方法必须在抽象类中

package com.kuang.oop.demo09;
//interface 定义关键字 接口都需要有实现类

//抽象的思维,难点,java架构师

public interface UserService {
    //接口中所有定义其实都是抽象的public abstract

    //常量-
    void run();
     public abstract void run1(String name);
     //public static final int AGE=99;
    int AGE=99;


     void add(String name);

     void delete(String name);
    void update(String name);
    void query(String name);

 }
/**
 * 1 约束
 * 2 定义一些方法,让不同的人实现
 * 3 public abstract
 * 4 public static final
 * 5 接口不能被实例化,接口中没有构造方法
 *6 implements可以实现多个接口
 * 7 必须重写接口中的方法
 * 总结博客
 */

package com.kuang.oop.demo10;

public class Test {
    //没有名字初始化类,不用将实列保存到变量中
    public static void main(String[] args) {
        new Apple().eat();
        //UerService uerService = new UerService() {
            //@Override
            //public void hello(){

            //}
        //};

    }
}
class Apple{
    public void eat(){
        System.out.println("1");

    }
}
interface UserService{
    
}

package com.kuang.oop;

import com.kuang.oop.demo06.Person;
import com.kuang.oop.demo06.Student;
import com.kuang.oop.demo10.Outer;
import com.sun.org.apache.xpath.internal.operations.String;
import org.omg.CORBA.Object;

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

    //Object > Person >Student
    Object object= (Object) new Student();

    System.out.println(object instanceof Student);//true
    System.out.println(object instanceof Person);//true
    System.out.println(object instanceof String);//False




    /*********************************************
        //demo10
        //new
        Outer outer =new Outer();
        //通过这个外部类来实例化内部类
        //outer.new Inner();
        Outer.Inner inner =outer.new Inner();
        inner.in();
        inner.getID();






    }




}
//System.out.println(x instanceof y )能不能编译通过
//关键看x和y是不是存在父子关系,同级关系
//类型之间的转化,父,子
//子类转换为父类,可能丢失自己的本来的一些方法
package com.exception.demo01;

public class Test {
    public static void main(String[] args) {
        int a= 1;
        int b =0;
        //假设要捕获取多个异常,从小到大!
        //new Test(),test(1,0);

        try{
            //try监控区域
            if(b==0){//主动的抛出异常 throw  throws
                throw new ArithmeticException();//主动抛出异常


            }

            System.out.println(a/b);
                new Test().a();
        }catch(ArithmeticException e){
            //catch捕获异常
            System.out.println("程序出现异常,变量b不能为0");
        }

        catch(Exception e){
            System.out.println("程序出现异常,变量b不能为0");
        }
        catch(Throwable t){
            System.out.println("程序出现异常,变量b不能为0");
        }
            finally{
            //处理善后能力
            System.out.println("finally");
        }


        //finaly 可以不要finally 假设io 资源,关闭


    }
    public void a(){
        b();
    }
    public void b(){
        a();
    }
    //假设这个方法中,处理不了这个异常,在方法上抛出异常
    public void test(int a,int b){
        if(b==0){//throw throws
            throw new ArithmeticException();//主动的抛出异常,一般在方法中使用


        }
        System.out.println(a/b);
    }

}

package com.exception.demo01;

public class Test2 {
    public static void main(String[] args) {
        int a=1;
        int b= 0;
        //ctrl+alt+t

        try {
            System.out.println(a/b);
        } catch (Exception e) {
            System.exit(1);
            e.printStackTrace();//打印错误的栈信息
        }finally{

        }
    }


}

package com.exception.demo02;
//自定义的异常类
public class MyException extends Exception {
    //传递数字

    private int detail;
    public MyException(int a){
        this.detail=a;
    }


    //toString 异常的打印信息


    @Override
    public String toString() {
        return "MyException{" +
                "detail=" + detail +
                '}';
    }
}

package com.exception.demo02;

public class Test {
    //可能会存在异常的方法
    static void test(int a) throws MyException{
        System.out.println("传递的参数为"+a);
        if (a>10){
            throw new MyException(a);//抛出,
        }
        System.out.println("ok");
    }
    public static void main(String[] args){
        try {
            test(10);
        } catch (MyException e) {
            //增加一些异常处理的代码

            System.out.println("MyException=>" + e);
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值