java学习笔记第一章
一、Java基础知识
=======================数据类型========================
源代码 -- 》 编译 -- 》执行
javac将源代码文件,编译成jvm虚拟机可以识别的字节码 *.class
执行 java 文件名
JAVA大小写敏感;main方法,程序的入口;一个源文件对应多个class;public class 类名必须对应文件名
注释:单行注释// ; 多行注释/*..*/ ; 单字母注释/*a*/
字符集:
西欧英文 1个字节;中文2个字节:Unicode(UTF-8,UTF-16)表示2个字节,存储可变长,西欧存储1个字节,中文2个字节
JAVA是强类型语言,每个变量必须声明其类型
数据类型:
1.基本数据类型:
数值型:整数类型(byte short int long)、浮点型(float double)
字符型:char
布尔型 : true or false
2.引用数据类型:
类:class
接口:interface
数组:Array[]
整数进制:八进制 0开头;十六进制 0x开头;
表述范围:
类型 占用存储 表数范围
byte 1字节 -128~127
short 2字节 -2^16~2^16-1 (-32768~32767)
int 4字节 -2^32~2^32-1
long 8字节
float 4字节 -3.4x10^38~3.4x10^38
double 8字节
char 2字节 0~65535
boolean 1位 0,1 真或假,作判断用
jdk 7.0新增特性,二进制&下划线: 二进制数: int a = 0b0000_0000_0000_0000_0000_0000_0000_0001
转二进制、八进制、十六进制:
Integer.toBinaryString 转二进制
Integer.toOctalString 转八进制
Integer.toHexString 转十六进制
JAVA里面的字符串定义,引用String类 ; String str ='abcdefg';
浮点数不能精确表示所有小数,会有舍入误差
转义字符 \
占位符: \t tab ; \n 换行 ;
超过表述范围会损坏精度;解决方法:自动转型或强制转型
eg:
long a6 = 200; 如果数据大小没有超过byte/short/char的表述范围,则可以自动转型
long a6 = 11111111111111111111111111111111L
a6超过int范围,加L转为long型
浮点型默认是double类型,定义float f= 6.28;会编译报错,需要后面加F;float f = 6.28F;
char 类型在整数计算时会自动转换为整数;根据ansi码 a=99
char c4 = 'a';
int i = c4 + 2;
System.out.println(i); 输入结果101
强制转型 char c5 = (char)i;
基本数据类型转换 & 强制转换:容量小的数据类型可以自动转换为容量大的数据类型
容量:表述的范围
byte <-->short <--> int -->long
-->double
char<--> int
float -->double
.....
当强制转换超出了表数范围,虽然可以通过编译,但转换成无意义的值
表达式类型提升问题:
做所有的二元运算符 (+-x/),会有类型提升问题,在表数范围里的自动转,没有在表数范围,需要我们强制转换
============================变量===========================
JAVA强类型语言,每个变量都必须声明其类型(基本类型+引用类型)
变量声明后,为其分配相应长度的存储单元
java变量是程序中最基本的存储单元,其要素包含变量名、变量类型和作用域 (如街道牌号:门牌号112)
变量声明:
int a; / int x,y,z;
变量赋值:赋值操作可以在变量声明时,也可以后期赋值。 int a = 3; / a = 3;
变量类型:
--局部变量:在方法或块语句里定义,必须初始化,才能使用
--成员变量(实例变量):类的内部,方法外部
常量: 只能初始化一次
final 常量
所有变量、方法、类名:起名,见名知意
变量、方法名:
--首字母小写和驼峰原则 : run() runRun()
类名:
--首字母大写和驼峰原则 : Man() GoodMan()
常量:
--大写字母和下划线 : MAX_VALUE
============================运算符============================
算术运算符: + - * / ++ --
布尔逻辑表达式:&& || ! 逻辑与和逻辑或采用短路的方法 : 判断1 || 判断2 判断1为真,整个表达式为真,判断2不参与运算
位运算符:~取反 | 按位或 & 按位与 ^异或 <<左移一位相当于乘2 >>右移一位相当于除2取商
扩展运算符: += -= *= /= %= eg:a += b 《==》 a = a + b
字符串连接符:+ 表达式中只要有一个字符串,+就是字符串连接符 eg:System.out.println(4+"5"); 运行结果是45,不是9
三目运算符: x?y:z x为boolean值,值为true,执行y;值为false,执行z
优先级:运算符优先级太过于复杂,简单化,使用括号来组织
Eclipse是开放源代码的、基于java的可扩展开发平台,http://www.eclipse.org/downloads/,下载eclipse ide for java ee develpoers版本
================================控制语句==================================
--顺序结构
--选择结构
--循环结构
选择结构:
-单分支;if
-双分支;if - else
-多分支;if - else if - else if - else
swtich是if多分支结构语句用法,多用于等值选择
switch语法:
switch (表达式) { //表达式类型为int型,枚举,字符串(jdk7新特性)
case 1:
System.out.println();
break;
case 2 :
System.out.println();
break;
default :
System.out.println();
break;
}
无break,会发生case穿透,当前case开始,直到break,跳出
if多分支实例:
1 public class TestSwitch { 2 public static void main(String[] args) { 3 double d = Math.random(); 4 int e =1+(int)(d*6); 5 System.out.println(e); 6 7 System.out.println("测试多选择结构"); 8 if(e==6){ 9 System.out.println("运气非常好!"); 10 }else if(e==5){ 11 System.out.println("运气很不错!"); 12 }else if(e==4){ 13 System.out.println("运气还行吧"); 14 }else{ //1,2,3 15 System.out.println("运气不好!"); 16 } 17 18 System.out.println("#########################"); 19 switch(e){ //int,或者自动可以转为int的类型(byte,char,short)。枚举。//JDK7中可以放置字符串。 20 case 6: 21 System.out.println("运气非常好!"); 22 break; //一般在每个case后面都要加break,防止出现case穿透现象。 23 case 5: 24 System.out.println("运气很不错!"); 25 break; 26 case 4: 27 System.out.println("运气还行吧"); 28 break; 29 default: 30 System.out.println("运气不好!"); 31 break; 32 } 33 34 System.out.println("***************************下面例子反过来利用了case穿透现象!"); 35 char c = 'a'; 36 int rand =(int) (26*Math.random()); 37 char c2 = (char)(c+rand); 38 System.out.print(c2 + ": "); 39 switch (c2) { 40 case 'a': 41 case 'e': 42 case 'i': 43 case 'o': 44 case 'u': 45 System.out.println("元音"); 46 break; 47 case 'y': 48 case 'w': 49 System.out.println("半元音"); 50 break; 51 default: 52 System.out.println("辅音"); 53 } 54 } 55 }
循环结构:写循环时,需迭代,跳出循环,否则为死循环
while 先判断,后执行
do while 先执行,后判断(不常用)
for循环
for (初始值;表达式;迭代) {
循环体;
//迭代在循环之后执行
}
break & continue
break终止循环,跳出整个循环
continue终止当次循环,然后继续下一次
while循环实例: 计算1...100的和
1 public class TestWhile { 2 public static void main(String[] args) { 3 //计算:1+2+3+...+100 4 int b = 1; 5 int sum = 0; 6 while(b<=100){ 7 sum += b; //sum = sum + b; 8 b++; 9 } 10 System.out.println("和为:"+sum); 11 } 12 }
for循环实例: 计算1...100的奇数和,偶数和
1 public class TestFor { 2 public static void main(String[] args) { 3 int oddSum = 0; //用来保存奇数的和 4 int evenSum = 0; //用来存放偶数的和 5 for(int i=0;i<=100;i++){ 6 if(i%2!=0){ 7 oddSum += i; 8 }else{ 9 evenSum += i; 10 } 11 12 } 13 System.out.println("奇数的和:"+oddSum); 14 System.out.println("偶数的和:"+evenSum); 15 }
break & continue 用法实例,带标签的continue跳到外层循环
1 public class TestBreakContinue { 2 public static void main(String[] args) { 3 int total = 0; 4 System.out.println("Begin"); 5 while (true) { 6 total++; 7 int i = (int) Math.round(100 * Math.random()); 8 if (i == 88) { 9 break; 10 } 11 } 12 System.out.println("Game over, used " + total + " times."); 13 14 System.out.println("###########################"); 15 for (int i = 100; i < 150; i++) { 16 if (i % 3 == 0) { 17 continue; 18 } 19 System.out.println(i); 20 } 21 22 System.out.println("**************************"); 23 int count = 0; 24 outer: for (int i = 101; i < 150; i++) { 25 for (int j = 2; j < i / 2; j++) { 26 if (i % j == 0) 27 continue outer; 28 } 29 System.out.print(i + " "); 30 } 31 }
=============================方法:method,function=============================
编程,先肯定我能做,再考虑怎么做;问题拆分,进一步简化
方法,类似于函数;完成特定功能的代码封装到一块;
设计方法的原则:
功能块是实现某个功能的语句的集合,最好保证方法的原子性,即一个方法完成一个功能
方法语法:
[修饰符1 修饰符2 ..] 返回值类型 方法名 (形参列表) {
}
void 无返回值
eg: public static int add (int a,int b) {
}
方法调用: int s = add (3,5)
形参 : a b
实参: 3 5
return的2个作用: 1.返回值 ; 2.结束方法的运行
方法的实例:
1 public class TestMethod { 2 public static int add(int a,int b){ 3 int sum = a+b; 4 if(a==3){ 5 return 0; //return 两个作用:结束方法的运行、返回值。 6 } 7 System.out.println("输出"); 8 return sum; 9 } 10 11 public static void main(String[] args) { 12 int s = add(3,5); 13 System.out.println(s); 14 } 15 }
递归计算:自己调用自己
递归结构:递归头(跳出循环的一个条件),递归体
递归的实例:
1 public class TestRecursion { 2 public static long factorial(int n){ 3 if(n==1){ //递归头 4 return 1; 5 }else{ //递归体 6 return n*factorial(n-1); 7 } 8 } 9 10 public static void main(String[] args) { 11 System.out.println(factorial(10)); 12 } 13 }
交互,接受键盘的输入,需用到Scanner类,其他包里的类使用,需要导入:import java.util.Scanner;
键盘输入的实例:
1 import java.util.Scanner; 2 3 public class TestScanner { 4 5 public static void test01(){ 6 Scanner s = new Scanner(System.in); 7 String str = s.next(); //程序运行到next会阻塞,等待键盘的输入! 8 System.out.println("刚才键盘输入:"+str); 9 } 10 11 public static void test02(){ 12 Scanner s = new Scanner(System.in); 13 System.out.println("请输入一个加数:"); 14 int a = s.nextInt(); 15 System.out.println("请输入被加数:"); 16 int b = s.nextInt(); 17 int sum =a+b; 18 System.out.println("计算结果,和为:"+sum); 19 } 20 21 public static void main(String[] args) { 22 test02(); 23 } 24 }
========================API文档阅读及生成项目api文档====================
API : Application Program Interface
package:包,类似文件夹,管理类(合适类位于合适的包);
包命名:包名,域名倒着写即可,再加上模块名,并于内部管理类 com.xxx.test
jdk主要的包:java.lang , java.awt , java.net , java.io , java,util
生成项目api文档: JAVADOC
特殊注释: /**
常用注释标签:
@Author 作者
@Version 版本 ......
posted on 2016-01-18 14:33 lucifer179 阅读(258) 评论(0) 编辑 收藏 举报