Java语言基础1

Java语言基础

1. 引入

举例:你的同桌需要大宝剑,找你借5毛钱

记录:

时间:2022/03/09

人物:你的同桌

地点:2205教室

事件:借钱,大宝剑

金额:0.5元

生活中我们通常会记录数据,有各种方式,在程序中,我们可以使用变量来记录数据。

变量就是用来记录数据的。

观察以上我们记录的内容,我们发现这些数据是有不同的数据类型的,同样,在程序中,也有不同的数据类型来记录不同的数据。

2. 设置NotePad

设置 ---》 首选项

进行如下设置即可

3. 变量的定义方式

声明变量的3种方式: 先声明,再赋值:【常用】 数据类型 变量名; 变量名 = 值;

声明并赋值:【常用】 数据类型 变量名 = 值;

多个同类型变量的声明与赋值:【了解】 数据类型 变量1 , 变量2 , 变量3 = 值3 , 变量4 , 变量5 = 值5;

// 变量的3种定义方式
​
public class Test1{
    public static void main(String [] args){
        // 方式1  先声明 再赋值
        int a;
        a = 200;
        System.out.println(a);
        
        // 方式2  连声明带赋值写为一条语句
        int b = 100;
        System.out.println(b);
        
        
        // 方式3  同时声明多个 同类型的 变量 赋值 个别变量
        // 这里声明了5个变量 其中e和h是有值的 其他三个未赋值
        // 未赋值的变量不能使用
        int c,d,e = 55 , f,h = 66; 
        System.out.println(e);
        System.out.println(h);
        
        /*
        System.out.println(c);
        System.out.println(d);
        System.out.println(f);
        */
        
    }
}

4. 单位换算

1TB = 1024GB

1GB = 1024MB

1MB = 1024KB

1KB = 1024byte

1byte = 8bit(位)

5. 数据类型

5.1 整型

如需为long类型赋值 如果取值范围超过了int 则必须在末尾加上L 大小写都可以 推荐大写

如果没有超过int的取值范围 加不加都可以

字符串与任何内容相加 实为拼接字符串 不做数学计算

public class Test1{
    public static void main(String [] args){
        // 整型  byte short int long 
        byte b1 = 20;
        System.out.println("b1的取值是:" + b1); // 字符串与任何内容相加 实为拼接字符串 不做数学计算
    
        short s1 = 666;
        System.out.println("s1的取值是:" + s1);
        
        
        int i1 = 7788;
        System.out.println("i1的取值是:" + i1);
        
        // 如需为long类型赋值 如果取值范围超过了int  
        // 则必须在末尾加上L  大小写都可以 推荐大写
        // 如果没有超过int的取值范围 加不加都可以
        long lon1  = 55662233;
        System.out.println("lon1的取值是:" + lon1);
        
        long lon2 = 23222222222L;
        System.out.println("lon2的取值是:" + lon2);
    
    }
}

5.2 浮点类型

double为浮点数的默认类型,如需为float类型赋值,需要在值的后面追加“F”

浮点类型默认为double类型 如果double类型的变量取值超过了float的取值范围 必须在默认加上D

public class Test1{
    public static void main(String [] args){
        // 浮点类型
        // float 单精度浮点数 
        // 负数取值范围 -3.4E+38 ~ -1.4E-45
        float f1 = -340000000000000000000000000000000000000F;
        float f2 = -0.0000000000000000000000000000000000000000000014F;
        System.out.println(f1);
        System.out.println(f2);
        
        // 正数的取值范围 1.4E-45 ~ 3.4E+38
        float f3 = 0.0000000000000000000000000000000000000000000014F;
        float f4 = 340000000000000000000000000000000000000F;
        System.out.println(f3);
        System.out.println(f4);
        
        // 浮点类型默认为double类型 如果double类型的变量取值超过了float的取值范围
        // 必须在默认加上D 
        double d1 = 2300.5;
        System.out.println(d1);
        
        double d2 = 3400000000000000000000000000000000000000D;
        System.out.println(d2);
        // double 双精度浮点数
    }
}

5.3 布尔类型

boolean 类型 取值只能为 true 或者false

public class Test1{
    public static void main(String [] args){
        // boolean 类型 取值只能为 true 或者false
        boolean bl1 = false;
        boolean bl2 = true;
        int a = 20;
        int b = 30;
        boolean bl3 = a > b;
        
        System.out.println(bl1);
        System.out.println(bl2);
        System.out.println(bl3);
    }
}

5.4 字符类型

Unicode字符集支持ASCII编码(美国标准信息交换码)。 Unicode中每个字符都对应一个十进制整数,从而可以使用多种方式赋值。

字符赋值:char c1 = 'A';(通过''描述为字符赋值)

整数赋值:char c2 = 65;(通过十进制数65在字符集中对应的字符赋值)

进制赋值:char c3 = '\u0041';(通过十六进制数41在字符集中所对应的字符赋值)

public class Test1{
    public static void main(String [] args){
        // char 类型取值范围  0 ~ 65535  无符号数
        // 三种赋值方式
        // 方式1 
        char ch1 = 'A';
        char ch2 = '中';
        char ch3 = '2';
        
        System.out.println(ch1);
        System.out.println(ch2);
        System.out.println(ch3);
        
        // 方式2 
        char ch4 = 65;
        System.out.println(ch4);
        
        char ch5 = 66;
        System.out.println(ch5);
        
        char ch6 = 67;
        System.out.println(ch6);
        
        char ch7 = 97;
        System.out.println(ch7);
        
        char ch8 = 20013;
        System.out.println(ch8);
        
        char ch9 = 20320;
        System.out.println(ch9);
        
        // 方式3  赋值unicode编码
        // Unicode编码属于万国码 记录了世界上大多数国家的语言
        // 中文的取值范围   \u4e00 ~ \u9fa5
        
        char ch10 = '\u4f60';
        System.out.println(ch10);
        
    }
}

6. 转义字符

转义字符用于解决使用char类型保存一些特殊的符号

或者实现一些换行,制表位等效果

public class Test1{
    public static void main(String [] args){
        // 转义字符
        char ch1 = '\'';
        System.out.println(ch1);
        
        char ch2 = '\"';
        System.out.println(ch2);
        
        // 换行 可以使用println()  或者 \n
        System.out.print("abc\ndef\nghi\njkl\nmn\n");
        
        // 制表位  大多数情况下可以保证上下几行的文字对齐
        System.out.println("学Java\t到CSDN");
        System.out.println("学IT\tCSDN\tCSDN圆你梦");
        
        
        char ch3 = '\\';
        
    }
}

7.String类型

String 引用数据类型 任何在英文的双引号之间的内容都叫字符串

public class Test1{
    public static void main(String [] args){
        // String 引用数据类型  任何在英文的双引号之间的内容都叫字符串
        String str1 = "abc";
        String str2 = "hello world";
        String str3 = "世界你好 abc   1234";
        String str4 =  "230";
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println(str4);
        
        // 任何内容和字符串相加 实为拼接 不进行数学计算
        System.out.println(str1 + str2 + str3 + str4);
        
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值