3.Java基础学习

Java基础

注释

单行注释

多行注释

文档注释

public class Helloworld {
    public static void main(String[] args) {
        //单行注释  //
        //输出一个Helloworld
        System.out.println("Helloworld");
        /*
          多行注释
          多行注释
         */

        //JavaDoc:文档注释
        /**
         * @Descripthion Helloworld
         * @Author Bob
         */
    }
}
/*        .---.        .-----------
 *       /     \  __  /    ------
 *      / /     \(  )/    -----
 *     //   ' \/ `   ---
 *     / // :    : ---
 *   // /   /  /`    '--
 *  //          //..\\
 *         ====UU====UU====
 *             '//||\\`
 *               ''``
 */
/*
 *                    .-'-._
 *                   /    e<
 *               _.-''';  (
 *     _______.-''-._.-'  /
 *     ====---:_''-'     /  _  _     %%%%
 *              '-=. .-'` _(_)(_)   %%|/%%%
 *                _|_\_  (_)(_)(_) %%%%%%%%%%
 *               //\\//\\//\\//\\//\\%/_%%%%%%%
 *           ____\\//\\//\\//\\//\\// |__|/__%%%
 *  ________(___  \\/\//\\//\\//\\//__//___%%%%%%%
 *            / \  \_/ __ \___//------\--%%%%%%
 *  _________/   \____/  \____/\\%%%%%%%%%%%%
 *                              \_-%%%%%%%%
 *
 */


标识符

常见标识符:

标识符及关键字

public class Demo1 {
    public static void main(String[] args) {
        //可 标识符大小写敏感 
        String Ahello = "Bob";
        String hello = "Bob";
        String $hello = "Bob";
        String _hello = "Bob";
        String _1 = "Bob";
        String Bob = "Bob";
        String bob = "Bob";
        //中文和拼音支持,但不推荐
        String 无畏契约 = "无畏契约";
        System.out.println(无畏契约);
        //不可 不可关键字
//        String 1hello = "Bob";
//        String _1# = "Bob";
//        String #hello = "Bob";
//        String *hello = "Bob";
//        String class = "Bob";
    }
}

数据类型

强类型语言:要求变量使用严格符合规定,变量先定义再使用

java数据类型的两大分类:

  • 基本类型

  • 引用类型

数据类型

import java.math.BigDecimal;

public class Demo3 {
    public static void main(String[] args) {
        //整数拓展   进制   二进制0b   十进制  八进制0   十六进制0x
        int i1=10;
        int i2=010;
        int i3=0x10; //0~9 A~F
        System.out.println(i1);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println("-------------------------------------------------");
        //--------------------------------------
        //浮点数拓展? 银行业务咋表示? 钱
        //float 有限 离散 舍入误差 大约 接近但不等于
        //double
        //BigDecimal 数据工具类
        //最好完全避免使用浮点数进行比较
        float f = 0.1f;//0.1
        double d=1.0/10;//0.1
        System.out.println(f==d);//false?

        float d1 = 21212121212f;
        float d2 =d1+1;
        System.out.println(d1==d2); //ture?

        System.out.println("-------------------------------------------------");
        //字符拓展?
        char c1='a';
        char c2='中';
        System.out.println(c1);
        System.out.println((int)c1);//强制转换
        System.out.println(c2);
        System.out.println((int)c2);
        //所有的字符本质还是数字
        //编码问题 Unicode 表:97=a 2字节 0 - 65536 Excel 2 16 = 65536
        char c3='\u0061'; //a
        System.out.println(c3);
        /*
        //转移字符
        // \\ 反斜杠
        //\t 间隔 ('\u0009')
        //\n 换行 ('\u000A')
        //\r 回车 ('\u000D')
        //\d 数字 等价于 [0-9]
        //\D 非数字 等价于 [^0-9]
        //\s 空白符号 [\t\n\x0B\f\r]
        //\S 非空白符号 [^\t\n\x0B\f\r]
        //\w 单独字符 [a-zA-Z_0-9]
        //\W 非单独字符 [^a-zA-Z_0-9]
        //\f 换页符
        //\e Escape
        //\b 一个单词的边界
        //\B 一个非单词的边界
        */
        System.out.println("hello\twor\nld");

        //
        String sa=new String("helloworld");
        String sb=new String("helloworld");
        System.out.println(sa==sb);
        String sc="helloworld";
        String sd="helloworld";
        System.out.println(sc==sd);
        //对象 从内存分析

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

类型转换

低----------------------------------------------->高

byte,short,char --> int--->long--->float--->double

public class Demo05 {
    public static void main(String[] args) {
        int i=128;
        double b= i; //内存溢出

        //强制转换 (类型)变量名  高——>低
        //自动转换  低-->高
        System.out.println(i);
        System.out.println(b);
        /*
        注意点:
        1.不能对布尔值进行转换
        2.不能吧对象类型转换为不相干的转换
        3.再吧高容量转换为低容量,强制转换
        4.转换的时候可能存在内存溢出,或精度问题
         */
        System.out.println("=====================");
        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);

    }
}

public class Demo06 {
    public static void main(String[] args) {
        //操作数比较大的时候,注意溢出问题
        //JDK7新特性,数字之间可以用下划线分割
        int money = 10_0000_0000;
        int years = 20;
        int total = money*years;//-1474836480 ,计算的时候溢出了
        long total2 = money*years; //-1474836480 默认是int,转换之前就出问题了
        long total3 = money*(long)years;//20000000000
        System.out.println(total);
        System.out.println(total2);
        System.out.println(total3);

    }
}

变量

可以变化的量

变量作用域:

类变量

实力变量

局部变量

public class Demo07 {
    public static void main(String[] args) {
        //int a,b,c;
        //int a=1,b=2,c=3;
        int a=1;
        int b=2;
        int c=3;
        String name = "Bob";
        char x = 'X';
        double pi = 3.14;

    }
}
public class Demo08 {

    //类变量 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);
        //变量类型 变量名字 = new Demo08()
        Demo08 demo08 = new Demo08();
        System.out.println(demo08.age);
        System.out.println(demo08.name);

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

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

    }
}

命名:

类成员变量:驼峰原则

局部变量:驼峰原则

常量:大写字母和下划线

类名:首字母大写的驼峰原则

方法名:驼峰原则

常量

特殊的变量

final 常量名 = 值;

public class Demo09 {
    //修饰符,不存在先后顺序
    //常量的定义: final
    static final double PI = 3.14;
    final static double PII = 3.1415;
    public static void main(String[] args) {
        System.out.println(PI);
        System.out.println(PII);
    }
}

运算符

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

package operator;

public class Demo1 {
    public static void main(String[] args) {
        //二元运算符
        int a=10;
        int b=10;
        int c=10;
        int d=10;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);

    }
}

package operator;

public class Demo02 {
    public static void main(String[] args) {
        long a = 12312313213213L;
        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((double)c+d);//double cast:转换

    }
}

package operator;

public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回的结果: 正确,错误 布尔值
        int a=10;
        int b=20;
        int c=21;
        System.out.println(c%a);
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);

    }
}

package operator;

public class Demo04 {
    public static void main(String[] args) {
        //++ -- 自增 自减 一元运算符
        int a=3;
        int b=a++;//a=a+1;先赋值,再自增
        System.out.println(a);
        int c=++a;//先自增 ,再赋值
        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);
    }
}

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));//false
        System.out.println("a || b "+(a||b));//true
        System.out.println("!(a && b) "+!(a&&b));//true

        //短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4);//c<4 错误 后面不执行 相当于短路
        System.out.println(c);
        System.out.println(d);
    }
}

package operator;
//位运算
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

        2*8 = 16 2*2*2*2
        <<左移 *2
        >>右移 /2
        0000 0000 0
        0000 0001 1
        0000 0010 2
        0000 0011 3
        0000 0100 4
        0000 1000 8
        0001 0000 16
         */
        System.out.println(2<<3);
    }
}

package operator;

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

        //字符串连接符 + ,String +
        System.out.println(a+b);
        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);
    }
}

包机制

为了更好的组织类

如同文件夹一样

一般利用公司域名倒置作为报名:com.Bob.www

JavaDoc

package com.bob.base;

/**
 * @author bob
 * @version 1.0
 * @since 1.8
 */
public class DOC {
    String name;

    /**
     * @author bob
     * @param name
     * @return
     * @throws Exception
     */
    public String test(String name) throws Exception
    {
        return name;
    }
    //命令行 javadoc 参数 java文件
    //IDEA生产javaDoc文档
}

IDEA生成javaDoc文档学习

posted @   火星的南瓜  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示