JavaSE基础语法

@[TOC]

注释

// 单行注释
/* 多行注释 */
/**
*文档注释
*/

标识符

Java所有组成部分都需要名字,类名、变量名、方法名都被称为标识符
在这里插入图片描述


数据类型

在这里插入图片描述

public class Demo01 {
    public static void main(String[] args) {
        //八大基本数据类型

        //整数类型
        byte num1=10;
       // byte numm=200;

        short num2=10;
        int num3=10;
        long num4=103L;

        //浮点类型
        float f1=365.23F;
        double d1=3654.23655;

        //字符类型
        char a1='浪';
        // String String ="李浪";
        // System.out.println(String);  //输出:李浪
        //String 不是关键字

        //boolean类型
        boolean flag1=true;
        boolean flag2=false;

        //整数拓展:  进制  二进制0b  八进制0  十进制    十六进制0x
        int n1=0b10;
        int n2=010;
        int n3=10;
        int n4=0x11;
        System.out.println(n1);  //2
        System.out.println(n2);  //8
        System.out.println(n3);  //10
        System.out.println(n4);  //17

			 //浮点数拓展  银行业务怎么表示  钱?  BigDecimal   数学工具类
        /*
        float    有限  离散 舍入误差  大约  接近  不等于
        double
        最好完全避免使用浮点数进行比较
        最好完全避免使用浮点数进行比较
        最好完全避免使用浮点数进行比较
        */
        float f2=0.1f;
        double d2=1.0/10;
        System.out.println(f2==d2);   //false

        float f3=2356555585654545445f;
        float f4=f3+1;
        System.out.println(f3==f4);   //true

			 //字符拓展
        char ch1='a';
        char ch2='浪';
        System.out.println((int)ch1);  //97      强制转换
        System.out.println((int)ch2);  //28010   强制转换
			 //所有字符的本质还是数字
			 //编码 Unicode表(a=97, A=65)
			 
        char ch3='\u6001';
        System.out.println(ch3);  // 态

        //转义字符
        System.out.println("a\tb");  //a  b
        System.out.println("a\nb");  // a 换行 b
    }
}


什么是字节

在这里插入图片描述


类型转换

在这里插入图片描述

public class Demo02 {
    public static void main(String[] args) {
        //低到高
        // byte,short,char >int > long > float > double

        //强制转换  高 到 低
        //自动转换  低 到 高
        byte bb1=100;
        int num1=bb1;    //自动转换
        System.out.println(num1);

        int num2=100;
        byte bb2=(byte)num2; //强制转换
        System.out.println(bb2);

        int a=122;
        int b=129;
        //强制转换
        byte by=(byte)a;
        byte by2=(byte)b;   //内存溢出,不知道会变成什么值
        System.out.println(by);  //122
        System.out.println(by2);  //-127

        /*
        注意点:
        1、不能对布尔值进行转换
        2、不能把对象类型转换为不相干的类型
        3、在把高容量转换到低日容量的时候  强制转化
        4、转换的时候可能存在内存溢出,或者精度问题!
        */
        System.out.println((int)36.9);   //36
        System.out.println((int)25.365f); //25

        /*
        计算时注意内存溢出的问题
         */
        int n1=100000000;
        int mon=200;
        int tot=n1*mon;
        System.out.println(tot);   //-1474836480   内存溢出了

        long tot2=n1*mon;
        System.out.println(tot2);  //-1474836480  在复制之前已经是int类型

        long tot3=n1*(long)mon;
        System.out.println(tot3);  //20000000000  先进行强制转换long,计算的结果就是long

    }
}


变量

public class Demo03 {
     static String  s1="这是一个类变量";

    //实例变量:从属于对象   如果不初始化,则默认赋值这个类型的默认值 0  0.0  false  null
     String s2="这是一个实例变量";

    public static void main(String[] args) {
        String s3="这是一个局部变量";  //局部变量  必须声明和初始化
        System.out.println(s1);
        //System.out.println(s2);  错误示范   实例变量必须通过类调用
        System.out.println(new Demo03().s2);
        System.out.println(s3);
    }
}


常量

在这里插入图片描述

public class Demo04 {
    //修饰符不区分先后顺序
    final static double PK=3.14;
    static final double PM=3.2544;

    public static void main(String[] args) {

        System.out.println(PK);
        System.out.println(PM);
    }
}

在这里插入图片描述


运算符

基本运算符

在这里插入图片描述

public class Demo05 {
    public static void main(String[] args) {
        long a=12121245454545L;
        int b=123;
        short c=10;
        byte d=9;

        float f=36.36f;
        double dou=36454.45;


        /*
        如果有double类型参与运算,则得到double类型
        如果有int类型参与运算,则得到int类型
        如果有short和byte运算,也是得到int类型
         */
        System.out.println(a+b+c+d);
        System.out.println(b+c+d);
        System.out.println((String)(c+d));

        System.out.println((String)(f+b));
        System.out.println((String)(dou+f));
    }
}

在这里插入图片描述


逻辑运算符

package operator;

public class Demo02 {
    public static void main(String[] args) {
        double a=Math.pow(2,3);
        double b=Math.pow(3,2);
        System.out.println(a);
        System.out.println(b);

        System.out.println("===========================================================");

		/*
		逻辑与 && 两个结果为true才为true,一个结果为false,则结果为false,且不会进行后半部分的运算
		逻辑或 || 一个结果为true就为true,前半部分为true,则不会进行后半部分的运算

   
        短路运算
        &&  如果表达式的前面的部分运算为false,则后面的不会进行运算,如果为TRUE,则还会运算后面的表达式
         */
        int c=3;
        boolean d =(c<2)&& (c++<8);
        System.out.println(d);
        System.out.println(c);

					

    }

}



位运算符

public class Demo03 {
    public static void main(String[] args) {
        /*
        位运算
        A = 0011 1100
        B = 0000 1101

        A&B=00001100   两个都为1,才为1
        A|B=00111101   只要有一个1,就为1
        A^B=00110001   相同则为0,不相同则为1
        ~B=11110010    取相反的

        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);  //16
    }
}

拓展运算符

package operator;

public class Demo04 {
    public static void main(String[] args) {
        int num1=3;
        int num2=1;
			
		//拓展运算符
		/*
		-= += 	*=  /=
		*/
        num2 += num1;  // num2=num2+num1  =4
        System.out.println(num2);
        

        int a=10;
        int b=20;

        System.out.println(a+b); //30

       // 表达式中存在字符串,则后面的 + 相当于字符串连接符,字符串后面出现的所有+ 都是作为连接符使用
        System.out.println("KKK"+a+b);  //kkk1020
        System.out.println(a+b+"kkk");  //30kkk
    }
}

条件运算符(三元运算符)

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

        String str=a+b<20?"真的大于":"真的小于";
        int c=a+b<20?90:80;
        System.out.println(str);  //真的小于
        System.out.println(c);    //80
    }
}

包机制

  • 包的本质是文件夹
  • 定义包用package
  • 导入包用import

JavaDoc生成文档

在这里插入图片描述

-encoding UTF-8 -charset UTF-8 -windowtitle "哈哈,这是我的title" 

Scanner操作

package cn.lilang.www.scanner;

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘数据
        Scanner sc=new  Scanner(System.in);
        System.out.println("请输入姓名:");
        
        //判断用户有没有输入数据
        if(sc.hasNextLine()){
          String str=sc.nextLine();   //nextLine()程序会等待用户输入完毕,将输入的数据赋值给str变量
            System.out.println("欢迎您学会了Scanner类的操作,"+str+"先生!");
        }
        //关闭资源io
        sc.close();
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值