首页 > 编程语言 > 详细

Java语言基础

时间:2021-06-30 21:13:53      阅读:50      评论:0      收藏:0      [点我收藏+]

1.变量

1.1定义方式

先声明,再赋值:
数据类型 变量名;
变量名 = 值;
声明并赋值:
数据类型 变量名 = 值;

1.2整型

面试题:byte的取值范围和原理
取值范围:-128~127
因为一个byte占8位,每一位可以存储一个0或者1,计算机以首位(最高位)为符号位,0表示正数,1表示负数,所以byte最大的数值为0111 1111转换为十进制为127,最小值为1000 0000转换为十进制为-128
技术分享图片

public class Test2{
	public static void main(String [] args){
		// 整型数据类型 byte short int long
		// byte 取值范围 -128 ~ 127
		byte b1 = 120;
		System.out.println(b1);
		// byte b2 = 128; 超过范围不能存储 报错 
		// System.out.println(b2);
		
		// short 取值范围 -32768 ~ 32767 
		short s1 = 200;
		System.out.println(s1);
		// short s2 = 50000; 超过范围不能存储 报错
		// System.out.println(s2);
		
		// short s3 = -40000;
		// System.out.println(s3);
		
		// int 取值范围 -2147483648 ~ 2147483647
		System.out.println(Integer.MAX_VALUE);
		System.out.println(Integer.MIN_VALUE);
		
		int i1 = 2000;
		System.out.println(i1);
		
		int i2 = -20000;
		System.out.println(i2);
		
		// int i3 = 21474836470; 过大的整数 报错
		// System.out.println(i3);		
		
		// long 取值范围  -9223372036854775808 ~ 9223372036854775807
		System.out.println(Long.MAX_VALUE);
		System.out.println(Long.MIN_VALUE);
		
		long l1 = 56897L;
		long l2 = -784512L;
		System.out.println(l1);
		System.out.println(l2);
		// 给long类型赋值 如果取值范围超过了int的取值范围必须加L  大小写都可以 要求大写
		long l3 = 21474836470l;
		System.out.println(l3);
		
	}
}

1.3浮点类型

技术分享图片

public class Test3{
	public static void main(String [] args){
		// float 取值范围  负数-3.4E+38 ~ -1.4E-45  正数1.4E-45 ~ 3.4E+38
		// 在给float类型赋值 必须在末尾加上F 大小写都可以 推荐大写
		float f1 = 3.14F;
		System.out.println(f1);
		float f2 = 20F;
		System.out.println(f2);
		
		float f3 = -340000000000000000000000000000000000000F;
		System.out.println(f3);
		
		float f4 = -0.0000000000000000000000000000000000000000000014F;
		System.out.println(f4);
		
		float f5 = 0.0000000000000000000000000000000000000000000014F;
		float f6 = 340000000000000000000000000000000000000F;
		System.out.println(f5);
		System.out.println(f6);
		
		// double类型取值范围 负数 -1.7E+308 ~ -4.9E-324   正数 4.9E-324 ~ 1.7E+308

		double d1 = 2.5;
		System.out.println(d1);
		double d2 = 20;
		System.out.println(d2);
		
	}
}

1.4布尔类型

技术分享图片

public class Test4{
	public static void main(String [] args){
		// boolean 布尔类型 取值只有 真true/假 false
		int a = 20;
		int b = 30;
		System.out.println(a > b);
		System.out.println(a < b);
		
		boolean bl1 = true;
		boolean bl2 = false;
		System.out.println(bl1);
		System.out.println(bl2);
	}
}

1.5字符类型

技术分享图片

char类型可以使用三种赋值方式
字符赋值:char c1 = ‘A‘;(通过 ‘‘ 描述为字符类型)
整数赋值:char c2 = 65;(通过十进制数65在字符集中对应的字符赋值)
进制赋值:char c3 = ‘\u0041‘;(通过十六进制数41在字符集中所对应的字符赋值)

public class Test5{
	public static void main(String [] args){
		// char 字符类型 任何被英文单引号包括的内容 且只能有一个 
		// char取值范围 0 ~ 65535 无符号
		char ch1 = ‘a‘;
		char ch2 = ‘B‘;
		char ch3 = ‘3‘;
		char ch4 = ‘中‘;
		char ch5 = ‘,‘;
		System.out.println(ch1);
		System.out.println(ch2);
		System.out.println(ch3);
		System.out.println(ch4);
		System.out.println(ch5);
		
		// char ch6 = ‘15‘; 编译报错 因为字符只能存一个内容
		// System.out.println(ch6);
		// 当我们直接赋值0~65535 范围的数值将根据ASCII码表 或者 Unicode码表(万国码)来找到对应的内容
		// ASCII码表 美国标准信息交换码
		char ch7 = 97;
		System.out.println(ch7);
		
		char ch8 = 98;
		System.out.println(ch8);
		
		char ch9 = 99;
		System.out.println(ch9);
		
		char ch10 = 20013;
		System.out.println(ch10);
		
		char ch11 = 20320;
		System.out.println(ch11);
		
		// char类型也可以直接以unicode编码赋值
		// Unicode记录了世界大多数国家的语言 中文的取值范围是 \u4e00 ~ \u9fa5
		char ch12 = ‘\u4f60‘;
		System.out.println(ch12);
		
		char ch13 = ‘\u4e00‘;
		System.out.println(ch13);
		
		char ch14 = ‘\u9fa5‘;
		System.out.println(ch14);
	
	}
}

2转义字符

技术分享图片

public class Test6{
	public static void main(String [] args){
		// 转义字符 表示用于处理一些特殊的符号 作为字符文本使用 不再具有特殊含义
		char ch1 = ‘\‘‘;
		System.out.println(ch1);
		
		char ch2 = ‘\\‘;
		System.out.println(ch2);
		
		// \n 换行
		System.out.print("hello world \n 世界你好\n");
		
		// \t 制表位 符
		System.out.println("床前明月光\t疑似地上霜");
		System.out.println("举头望明月\t低头思故乡");
		
		
		// \"
		char ch3 = ‘\"‘;
		System.out.println(ch3);	
	}
}

3.String类型

技术分享图片

public class Test7{
	public static void main(String [] args){
		String str1 = "abcdefg";
		String str2 = "hel   lo ,;‘[‘.p[.,./‘  ";
		String str3 = "中国";
		String str4 = "                1";
		System.out.println(str1);
		System.out.println(str2);
		System.out.println(str3);
		System.out.println(str4);
			
		String str5 = "\"";
		System.out.println(str5);
		
		String str6 = "\u4e00 \u4e01";
		System.out.println(str6);
		
		short s1 = -65;
		char ch1 = (char)s1;
		System.out.println(ch1);
	}
}

Java语言基础

原文:https://www.cnblogs.com/hourglas/p/14956069.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!