Java语言基础1

Java语言基础

1. 引入

生活中的一件小事儿:

比如你需要大宝剑,发现钱不够,找你的同桌借0.5元钱。

时间:2021年11月17号 上午9点42分

事件:借同桌钱

人物:同桌(赵四)

地点:2114教室

金额:5毛 0.5元

生活中我们需要记录数据,途径有很多,纸笔,拍照,录音,视频等等

在程序中也依然需要记录数据,因为程序就是用来解决现实生活中的问题。那么我们程序中记录数据,使用变量。

经过分析我们发现现实生活中我们记录的数据是有不同的数据类型的,程序中也一样。

2. Notepad++使用

  • 在Notepad上方菜单栏找到设置---->首选项,进行如下设置即可

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8hAgSZnL-1652196042233)(.\img\notepad设置.png)]

3. 变量的概念

4. 变量的定义方式

  • 有三种
  • 先声明,再赋值:【常用】
    数据类型 变量名;
    变量名 = 值;
  • 声明并赋值:【常用】
    数据类型 变量名 = 值;
  • 多个同类型变量的声明与赋值:【了解】
    数据类型 变量1 , 变量2 , 变量3 = 值3 , 变量4 , 变量5 = 值5;
/**
 *	这个类用来讲解变量的定义3种方式 
 *
 */
public class Test1{
	public static void main(String [] args){
		// print() 不会换行
		// println()  会换行  ln 表示 line 
		
		// 变量的定义方式
		
		// 方式1  先声明  再赋值 
		int a;
		a = 100;
		System.out.println(a);
		
		
		// 方式2  连声明 带赋值
		int b = 99;
		System.out.println(b);
		
		
		
		// 方式3 同时声明多个  同类型的 变量 赋值个别变量  
		
		int h,i,j,k = 20 , f,l = 33;
		
		System.out.println(k);
		
		System.out.println(l);
		
		// Java中未赋值的局部变量 不能使用
		
		// System.out.println(h);
		
		// System.out.println(i);
		
	}
}

5. 单位换算

1TB = 1024GB

1GB = 1024MB

1MB = 1024KB

1KB = 1024byte(字节)

1byte = 8bit(位)

6. 数据类型

6.1 整型

注意:

int为整数的默认类型,如需为long类型赋值,
当取值范围超过int的情况,必须在值的后面追加“L” 大小写都可以 要求大写
当取值范围没有超过int的情况 可加可不加

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XVybpW1y-1652196042234)(.\img\整型.png)]

/**
 *	整数类型
 */
public class Test1{
	public static void main(String [] args){
		// byte short(短整型) int long(长整型)


		// byte 取值范围 -128 ~ 127
		byte b1 ;
		b1 = 20;
		
		byte b2 = 22;
		
		System.out.println(b1);
		System.out.println(b2);
		
		System.out.println("=========文字使用英文的双引号包括,表示字符串==========");
		
		// short 取值范围 -32768 ~ 32767
		short s1 = 5623;
		short s2 = 5689;
		System.out.println(s1);
		System.out.println(s2);
		
		System.out.println("=========文字使用英文的双引号包括,表示字符串==========");
		// int 取值范围  -2147483648 ~ 2147483647
		System.out.println(Integer.MAX_VALUE);
		System.out.println(Integer.MIN_VALUE);
		
		int i1 = 777445;
		int i2 = 20;
		
		System.out.println(i1);
		System.out.println(i2);
		
		System.out.println("=========文字使用英文的双引号包括,表示字符串==========");
		// long 取值范围 -9223372036854775808 ~ 9223372036854775807
		System.out.println(Long.MAX_VALUE);
		System.out.println(Long.MIN_VALUE);
		
		// 注意:int为整数的默认类型,如需为long类型赋值,
		// 当取值范围超过int的情况,必须在值的后面追加“L” 大小写都可以 要求大写
		// 当取值范围没有超过int的情况   可加可不加

		
		long l1 = 2356L;
		long l2 = 1122334455L;
		System.out.println(l1);
		System.out.println(l2);	
		
		long l3 = 2147483648L;
		System.out.println(l3);	
	}
}
6.2 浮点类型

double为小数的默认类型,如需给float类型赋值,必须在末尾加上F

如果给double类型赋值 超过了float的取值范围 需要在末尾加上D

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LyMb7dqg-1652196042235)(.\img\浮点类型.png)]

public class Test1{
	public static void main(String [] args){
		// 浮点类型 
		// float
		// 负数的取值范围   -3.4E+38 ~ -1.4E-45
		
		float f1 = -340000000000000000000000000000000000000F;
		System.out.println(f1);
		
		
		float f2 = -0.0000000000000000000000000000000000000000000014F;
		System.out.println(f2);
		
		// 正数的取值范围  1.4E-45 ~ 3.4E+38
		float f3 = 0.0000000000000000000000000000000000000000000014F;
		System.out.println(f3);
		
		float f4 = 340000000000000000000000000000000000000F;
		System.out.println(f4);
		
		
		
		// double
		double d1 = 20;
		double d2 = 20.5;
		System.out.println(d1);
		System.out.println(d2);
		
		double d3 = 3400000000000000000000000000000000000001D;
		System.out.println(d3);
		
		// 如果给double类型赋值 超过了float的取值范围 需要在末尾加上D
	}
}
6.3 布尔类型

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-K63DaZnm-1652196042235)(.\img\布尔类型.png)]

public class Test1{
	public static void main(String [] args){
		// 布尔boolean类型 取值只有 true  或者 false
		// 也可以赋值一个取值为布尔类型的表达式 
		boolean bl1 = true;
		boolean bl2 = false;
		
		System.out.println(bl1);
		System.out.println(bl2);
		
		int a  = 20;
		int b  = 30;
		boolean bl3 = a > b;
		System.out.println(bl3);
		
	}
}
6.4 字符类型

Unicode码表中文取值范围是\u4e00 ~ \u9fa5

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E5olBeqq-1652196042235)(.\img\字符类型.png)]

public class Test1{
	public static void main(String [] args){
		// char类型 字符类型 所占2个字节  
		char ch1 = 'a';
		char ch2 = 'B';
		char ch3 = '1';
		char ch4 = '中';
		System.out.println(ch1);
		System.out.println(ch2);
		System.out.println(ch3);
		System.out.println(ch4);
		
		
		// char ch5 = '20'; char类型只能存储一个个位数的 数值
		// System.out.println(ch5);
		// ASCII 表  美国标准信息交换码
		
		char ch6 = 65;
		System.out.println(ch6);
		
		char ch7 = 66;
		System.out.println(ch7);
		
		// unicode 万国码  记录了世界上大多数国家的语言文字
		// 实现了字符和二进制数值的映射 
		
		char ch8 = 20013;
		System.out.println(ch8);
			
		char ch9 = 20320;
		System.out.println(ch9);
		
		char ch10 = '\u4f60';
		System.out.println(ch10);
		
		char ch11 = (char)-1;
		System.out.println(ch11);

	}
}

7. 转义字符

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ebh8byAE-1652196042235)(.\img\转义字符.png)]

public class Test1{
	public static void main(String [] args){
		// 转义字符  
		char ch1 = '\'';
		System.out.println(ch1);
	
		// \n 换行符
		System.out.print("hello\n world\n 2114\n 世界你好1\n");
		System.out.print("hello world 2114 世界你好2\n");
		
		// \t 制表符 制表位 可以在大多数情况保证上下行对齐
		
		System.out.println("学Java\t到千锋\t郑州千锋找帅栋");
		System.out.println("学IT\t到千锋\t郑州千锋圆你梦");
		
		char ch2 = '\\';
		System.out.println(ch2);
				
		char ch3 = '\"';
		System.out.println(ch3);
	}
}

8. String类型

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0ZUU0s8E-1652196042236)(.\img\String类型.png)]

public class Test1{
	public static void main(String [] args){
		// String类型 字符串类型  任何使用英文的双引号包括的内容都是字符串
		String str1 = "abcd";
		String str2 = "hello world 中国";
		String str3 = "230";
		String str4 = "a 					b			c 				";
		String str5 = "\"\"";
		
		System.out.println(str1);
		System.out.println(str2);
		System.out.println(str3);
		System.out.println(str4);
		System.out.println(str5);
	}
}

符串类型 任何使用英文的双引号包括的内容都是字符串
String str1 = “abcd”;
String str2 = “hello world 中国”;
String str3 = “230”;
String str4 = “a b c “;
String str5 = “”””;

	System.out.println(str1);
	System.out.println(str2);
	System.out.println(str3);
	System.out.println(str4);
	System.out.println(str5);
}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值