什么是字节
-
位(bit):是计算机 内部数据存储 最小单位,11001100是一个八位二进制数。
-
字节(byte):是计算机中 数据处理 的基本单位,习惯上用大写B来表示。
-
1B(byte,字节)=8bit(位)
-
字符:是指计算机中使用的字母,数字,字和符号
-
1bit表示1位
-
1Byte表示一个字节1B=2b
-
1024B=1KB
-
1024KB=1M
-
1024M=1G
-
1024G=1T
电脑的32位和64位的区别是什么?
32位支支持4G内存 64位支持128G 内存
八大数据基本类型
public class day1 { public static void main(String[] args){ //八大基本数据类型 int numl = 10; byte num2 = 20; short num3 = 30; long num4 = 30l; //long 类型数字后面加个l //小数:浮点数 float num5 = 50.1F; //Lfloat 类型后面数字加个F double num6 = 3.1415926; //字符 char name ='过'; //字符串,String不是关键字,类 //String name = "张三"; //布尔值:是非 boolean flag = true; //boolean flag = false; } }
数据类型扩展及面试题
public class day2 { public static void main(String[] args) { int i = 10; int i2 = 017; //八进制0 int i3 = 0x10;//十六进制0x 0~9 A~F 16 System.out.println(i); System.out.println(i2); System.out.println(i3); //============================= //浮点数扩展? 银行业务怎么表示?钱 //BigDecimal 数学工具类 //============================= //float //double //最好使用浮点数进行拓展 float f = 01.f; //0.1 double d = 1.0/10; //0.1 System.out.println(f==d);//false float d1 = 23123123123123f; float d2 = d1 + 1; System.out.println(d1==d2);//true //=================================================== //字符拓展 //=================================================== 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 65 = A) 2字节 65536 Excel 2 16 = 65536 // U0000 UFFFF char c3 = '\u0061'; System.out.println(c3);//a //转义字符 // \t 制表符 // \n 换行 System.out.println("hello\nworld"); // System.out.println("====================================="); String sa = new String("hello world"); String sb = new String("hello world"); System.out.println(sa==sb); String sc = "hello world"; String sd = "hello world"; System.out.println(sc==sd); //对象 从内存分析 //布尔值扩展 boolean flag = true; if (flag==true){}//新手 if (flag){}//老手 //Less is more! 代码要精简易读 } }
类型转换
lic class day4 { public static void main(String[] args){ //类型转换 //操作比较大的书的时候,注意溢出问题 //JDK7新特性,数字下划线分割 int money = 1000000000;//int 范围+-20亿 int years = 20; int total = money*years; //计算时溢出了-1474836480 long total2 = money*years; //默认是int,转换之前存在问题 long total3 = years*((long)money); System.out.println(money); System.out.println(total); System.out.println(total2); System.out.println(total3); // L l 用大写L } }
Scanner对象
我么可以通过Scanner类来获取用户的输入
基本语法
Scanner s = new Scanner(System.in)
package scanner; import java.util.Scanner; public class Demo01 { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int a = 0; float b = 0.0f; System.out.println("请输入整数:"); if (scanner.hasNextLine()){ a = scanner.nextInt(); System.out.println("整数数据"+a); }else { System.out.println("输入的不是整数"); } System.out.println("输入小数"); if (scanner.hasNextFloat()){ b = scanner.nextFloat(); System.out.println("小数数据:"); }else { System.out.println("输入的不是小数数据"+b); } scanner.close(); } }
while循环
package scanner; import java.util.Scanner; public class Demo02 { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); double sum = 0; int m = 0; while (scanner.hasNextDouble()){ double x = scanner.nextDouble(); m = m + 1; sum = sum + x; System.out.println("你输入了第"+m+"数据"); System.out.println("结果是sum="+sum); } System.out.println("个数的和为:"+sum); System.out.println("个数的平均值为:"+ (sum/m)); scanner.close(); } }
Switch多选择结构
-
多选择结构还有一个实现方式就是switch case 语句
-
switch case 语句判断一个变量与一系列值中的某个值是否相等,每个值称为一个分支。
-
package struct; public class SwitchDemo01 { public static void main(String[] args) { String name = "杨霖"; char grade = 'A'; switch (grade) { case 'A': System.out.println("a"); break; case 'B': System.out.println("b"); break; case 'C': System.out.println("c"); break; case 'D': System.out.println("d"); break; case 'd': System.out.println("杨霖"); break; case 'F': System.out.println("f"); break; } switch (name) { case "杨霖": System.out.println("帅哥"); break; } } }
for循环和while的功能比较
一、循环的结构不同
for循环的表达式为:
for(单次表达式;条件表达式;末尾循环体)
{
中间循环体;
}
while循环的表达式为:while(表达式){循环体}[5]
二、执行条件的判断方式不同
for循环执行末尾循环体后将再次进行条件判断,若条件还成立,则继续重复上述循环,当条件不成立时则跳出当下for循环。
while循环当满足条件时进入循环,进入循环后,当条件不满足时,执行完循环体内全部语句后再跳出,而不是立即跳出循环。
三、语法不同
for循环的语法为:for (变量 = 开始值;变量 <= 结束值;变量 = 变量 + 步进值) {需执行的代码 }。
while循环的语法为:while (<条件>) {需执行的代码 }。