首页 > 编程语言 >3.Java基础学习

3.Java基础学习

时间:2024-04-18 21:23:31浏览次数:26  
标签:Java String int 基础 System 学习 println public out

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文档学习

标签:Java,String,int,基础,System,学习,println,public,out
From: https://www.cnblogs.com/zuojiawang/p/18144427

相关文章

  • Java题目集1-3总结
    (1)前言:第一次大作业的知识点包括类与对象正则表达式以及数组对象和不同类之间的关系;题量较小,难度不大,关键是理清question,test-paper,answer-paper之间的关系。第二次大作业的知识点增加了List,ArrayList,HashMap,三种变长数组的使用,增加了正则表达式的难度,题量增加,难度上升,试卷中的......
  • Docker学习记录
    docker官方文档https://docs.docker.com/engine/install/ubuntu/docker全球镜像仓库https://hub.docker.com/1、docker的安装1.1、卸载旧版首先如果系统中已经存在旧的Docker,先卸载:但是不同的系统,卸载方式不一样!!!Ubuntu系统:apt-getautoremovedockerdocker-cedocker-......
  • Web前端基础
    HTML&CSS基础HTML:结构(页面元素和内容)css:表现(网页元素的外观和位置等页面样式)行为:JavaScript:行为(网页模型定义与页面交互)排版标签排版标签标题标签:h系列标签重要程度依次递减特点:独占一行、h1-h6文字逐渐减小段落标签:p特点:段落之间存在间隙、独占一行文本格式化标签场景......
  • [02] JS-基础语法(一)
    1.几个概念本文我们讲解一下JS中的几个简单的概念,包括标识符、关键字、保留字、大小写和字面量。这些基本概念虽然不能直接提升我们的编程能力,但它们是JS的基本组成元素。1.1标识符所谓标识符(Identifier),就是名字。JavaScript中的标识符包括变量名、函数名、参数名、属性......
  • Flask基础
    一、简介1、框架介绍Flask是一个基于Python并且依赖于Jinja2模板引擎和WerkzeugWSGI服务的一个微型框架WSGI:WebServerGatewayInterface(WEB服务网关接口),定义了使用python编写的webapp与webserver之间接口格式其他类型框架:Django:比较“重”的框架,同时也是最出名的P......
  • [03] JS-基础语法(二)
    1.判断、循环1.1if-elseif语句ifelse语句ifelseifelse语句e.g.编写一个程序,获取一个用户输入的整数,然后显示这个数是奇数还是偶数。//编写一个程序,获取一个用户输入的整数//letnum=+prompt("请输入一个整数")letnum=parseInt(prompt("请输入一个整数"......
  • 模拟电路学习笔记——晶体管电流放大作用
    基本共射放大电路△u1为输入电压信号,接入基极——发射极回路,称为输入回路;放大后的信号在集电极——发射极回路,称为输出回路;因发射极是两个回路的公共端,故称该电路为共射放大电路晶体管工作在放大状态的外部条件:发射结正向偏置,集电结反向偏置输入回路中基极电......
  • RTK基础知识
    GPSGNSSRTK区别上面这三个缩写经常会遇到,当然也经常会混淆GPS(GlobalPositioningSystem)即全球定位系统,是美国从20世纪70年代开始研制,历时20年,耗资200亿美元,于1994年全面建成,具有在海、陆、空进行全方位实时三维导航与定位功能的新一代卫星导航与定位系统。能够RTK(RealTi......
  • java-collections-map t
    MapMap常用子类HashMap:HashMap是最常用的Map实现之一,它基于哈希表实现,提供了O(1)时间复杂度的插入、删除和查找操作。Hashtable:Hashtable是较早期的Java集合类,它是线程安全的,但性能通常比HashMap差,因为它的方法是同步的。TreeMap:TreeMap是基于红黑树实现的有序Ma......
  • 模拟电路学习笔记——晶体三极管(一)
    1.晶体三极管*晶体三极管中有两种带有不同极性电荷的载流子参与导电,故称为双极型晶体管(BJT:BipolarJunctionTransistor),又称半导体三极管,简称晶体管*根据不同的掺杂方式,在同一硅片上制造出三个掺杂区域,并形成两个PN结,就构成晶体管2.几张常见晶体管外形3.晶体管的结......