0基础学java,不担心,先从基础开始

这篇博客介绍了JAVA的基础知识,包括准备阶段,如JDK、JRE和JVM的介绍,以及如何编写第一个Hello, World程序。接着详细讲解了数据类型,强调了浮点数和字符的特殊性,以及类型转换的规则。随后讨论了变量的定义、作用域和命名规范。运算符部分涵盖了算术、关系、逻辑和位运算。最后,文章介绍了包机制,它是组织和管理类的重要方式。" 4338467,602154,程序语言:分类与特性详解,"['编程语言', '汇编', '编译', '解释器', '操作系统']
摘要由CSDN通过智能技术生成

在这里插入图片描述

JAVA基础

1、准备

  • Markdown记录,blog发表

  • 基本Dos熟悉

  • 三个版本:JaveSE、JaveME、JavaEE

  • 结构

    JDK:Java Development Kit

    JRE:Java Runtime Environment

    JVM:Java Virtual Machine

  • JDK、IDE安装与卸载

  • 第一声呼喊:Hello,world!
    在这里插入图片描述

  • Java具有编译型和解释型双重功能

编译型和解释型

  • 学会规范写注释,很重要
import java.util.Arrays;
    /** JavaDoc注释:
     * @description Hello,World!
     * @author Feild
     * @version 1.0
     * */
public class HelloWorld {
    public static void main(String[] args) {
        //单行注释:输出一个Hello,World!
        System.out.println("Hello,world!");
    }
    /*
    * 块注释:注释一段文字
    * daflj
    * sdflkja
    * */
}
  • 标识符(大小写敏感)

关键字

2、数据类型

  • 强类型语言

强类型与弱类型

  • 数据类型

**浮点数扩展:**float(离散,有限,接近不等于)不要去比较,银行系统的钱数用BigDecimal的数学工具类表示

**字符扩展:**可以强制转换成数字(所有字符的本质还是数字)

转义字符:

转义字符

  • 类型转换:

    • 从小到大
    • 从整数到小数
    • 反向:强制转换
      • (强制转换类型)原有数据
      • 注意内存溢出、精度问题
    • 不能对布尔值进行转换
    • 不能转成不同类不相干的类型

3、变量

  • 变量
    • 变量类型 变量名 = 值
  • 变量作用域
    • 类变量(前有关键词)
    • 实例变量
    • 局部变量(方法里)
public class Demo01 {

    //类变量:static
    static double salary = 2500;

    //属性:变量

    //实例变量:从属于类,从属于对象
    //若没有初始化,默认值分别为:0,0.0,u0000,false
    // 除了基本类型其余都是null
    String name;
    int age;

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

        //自定义类型,实例化自定义类
        Demo01 demo01 = new Demo01();
        System.out.println(demo01.age);
        System.out.println(demo01.name);

        //类变量:static
        System.out.println(salary);
    }

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

    }
}
  • 常量
    • 初始化后不可更改,也不会变动
    • 可以理解成一种特殊的变量
    • 一般使用大写符号
public class Demo2 {

    //类变量 常量 数据类型 常量名(大写) = 值;
    static final double PI = 3.14;
    //修饰符不区分先后顺序
    final static double PI2 = 3.14;

    public static void main(String[] args) {
        System.out.println(PI);
        System.out.println(PI2);
    }
}
  • 变量的命名规范
    • 见名知意
    • 类成员变量:除了常量,其他采用驼峰原则(首字母小写,后面单词一个字母大写):lastName
    • 局部变量:同样驼峰原则
    • 常量:大写字母和下划线:MAX_VALUE
    • 类名:单词首字母都大写GoodMan
    • 方法名:同样驼峰原则

4、运算符

  • 算术运算符:+,-,*,/,%,++,–

public class Demo01 {
    public static void main(String[] args) {
        //二元运算符
        //Ctrl + D :复制当前行到下一行
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);

        //取余,模运算
        System.out.println(d%a); // d/a = 25/10=2...5(取余)
        
    }
}
public class Demo02 {
    public static void main(String[] args) {
        long a = 123123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;

        System.out.println(a+b+c+d); //Long
        System.out.println(b+c+d); //Int
        System.out.println(c+d); //Int(除非有Long,其他结果都是Int)

    }
}

public class Demo04 {
    public static void main(String[] args) {
        //一元运算符:++ 自增 ;-- 自减
        int a = 3;
        
        int b = a++; //b=3,a=3+1=4;先赋值,再自增

        int c = ++a; //a=4+1=5,c=5;先自增,再赋值

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        /*很多运算,我们会使用一些工具类操作
        * 如:幂运算
        * 2^3 --> 2*2*2 = 8
        * */
        double pow = Math.pow(2,3);
        System.out.println(pow);
    }
}
  • 赋值运算符:=

  • 关系运算符:>,<,>=,<=,==,!=,instanceof

package operater;

public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回的结果:布尔值
        int a =10;
        int b =20;

        System.out.println("a>b-->"+(a>b));
        System.out.println("a<b-->"+(a<b));
        System.out.println("a==b-->"+(a==b));
        System.out.println("a>=b-->"+(a>=b));
        System.out.println("a<=b-->"+(a<=b));
        System.out.println("a!=b-->"+(a!=b));
    }
}
  • 逻辑运算符:&&,||,!

//逻辑运算法
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:"+(a||b));   //逻辑或:任一个变量为真,结果都为真
        System.out.println("! (a && b) :"+(!(a&&b)));  //逻辑非:真变假,假变真

        //运算短路
        int c = 5;
        boolean d = (c>5) && (++c>5);
        System.out.println(d);
        System.out.println(c);
    }
  • 位运算符:&,|,^,~,>>,<<,>>>

public class Demo06 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101
        ---------------------------
        A&B 0000 1100   与
        A|B 0011 1101   或
        A^B 0011 0001   异或
        ~B  1111 0010   非

        //位运算,效率极高!
        0000 0000   0
        0000 0001   1
        0000 0010   2
        0000 0011   3
        0000 0100   4
        0000 1000   8
        0001 0000   16
        
        <<左移 *2
        >>右移 /2
        2*8=16 2*2*2*2
         */
        System.out.println(2<<3);
    }
}

  • 条件运算符:?,:

public class Demo08 {
    public static void main(String[] args) {
        //三元运算符: x ? y : z
        //若x==true,结果为y;若想==false,结果为z
        int score = 80;
        String type = score>60 ? "及格" : "不及格";
        System.out.println(type);

        score -= 40;
        String type2 = score>60 ? "及格" : "不及格";
        System.out.println(type2);

        //必须掌握
        //也可以用if实现
    }
}
  • 扩展赋值运算符:+=,-=,*=,/=

public class Demo07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        //扩展赋值运算符
        a+=b;   //a = a + b     //10+20=30
        a-=b;   //a = a - b     //30-20=10

        System.out.println(a);

        //字符串连接符+
        System.out.println(a+b);    //+就是加法:10+20=30
        System.out.println(""+a+b); //string在前,+就是拼接:1020
        System.out.println(a+b+""); //string在后,+就是加法:10+20=30

    }
}

5、包机制

  • 用于区别类名(文件)的命名空间(文件夹),为了更好的组织类(区分管理文件)

  • 一般利用公司域名倒置作为包名

  • 定义:package 包名;

  • 导入:import 包名.类名;

    • 导入包下所有类使用通配符:import pkgX.*;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值