您的位置:首页 > 职场人生

[黑马程序员](第1-5天)基础部分

2015-07-27 23:17 531 查看
------- android培训java培训、期待与您交流! ----------day1:

环境变量的配置:

将javac命令文件所在目录的路径放入path路径中。(D:\java\jdk1.6.0_21\bin)

小技巧:把jdk所在的目录单独放在一个环境变量里(JAVA_HOME:D:\java\jdk1.6.0_21)

引用的时候用双%号

day2:

关键字:被JAVA语言赋予了特殊意义的单词

用于定义数据类型的关键字(11个):

class,interface,byte,short,int,long,float,double,char,boolean,void

用于定义数据类型值的关键字(3个):

true,false,null

用于定义流程控制的关键字(11个):

if,else,switch,case,default,for,return,break,contiune,do,while

用于定义访问权限修饰符的关键字(3个):

protected,private,public

用于定义类,变量,函数修饰符的关键字(4个):

abstract,static,final,synchronized

用于定义类与类之间关系的关键字:(2个)

extends,implements

与实例相关的关键字(4个):

this,new,super,instaceof

与异常处理相关的关键字(5个):

try,catch ,finally,throw,throws

用于包的关键字(2个):

package,import

其他(5个):

native,strictfp,transient,volatile,assert

标识符:

26个英文字母

0-9数字

_%特殊字符

命名规范:

包名:

xxxyyyzzz:全部小写

类名及接口名:

XxxYyyZzz:首字母大写

变量及函数名:

xxxYyyZzz:第一个单词全小写后面的首字母大写

常量名:

XXX_YYY_ZZZ:全大写

注释:

//单行注释

/*xxx*/多行注释

/**xxx*/文档注释

进制的基本转换:

十进制转八进制:

三位分割法

十进制转十六进制:

四位分割法

负数的二进制就是对应的正数的二进制的取反加一

java语言的数据类型:

(共11种)

基本数据类型:

数值型:byte,short,int,long

float,double

字符型:char

布尔型:boolean

引用数据类型:

类:class

接口:interfa

数组:[]

class VarDemo {

public void static main(String[] args){

int x = 5;

byte y = 1;

x = x + y ;

System.out.println(x);

}

}

自动的数据类型转换(小---》大)

强制的数据类型转换(大---》小)

class VarDemo {

public void static main(String[] args){

byte y = 10;

y = (byte) y + 100;

System.out.println(y);

}

}

结果并不是110,而是一个负数,因为要先换算成占一个字节的二进制数,然后因其第一位为1,故为负数,除符号位外,其余部分取反加一即可

字符也可以和数字相加

class VarDemo{

public void static main(String[] args){

System.out.println('a'+1);

System.out.println((char)('b'+2));

}

}

运算符:

算数运算符(12个:2+4+4+2)

2:+ - 正号,负号

4:+ - * / 加减乘除

4:++ ++ -- -- 自增前后 自减前后

2:+ 字符连接号 %取模

s+=4和s = s + 4的区别:

前者自动进行了数据类型的转换,后者不能

比较运算符(7个)2+4+1:

2:== !=

4:> >= < <=

1:instanceof

for循环嵌套:

class ForForDemo{

public void static main(String[] args){

for(int x = 0; x <= 4 ; x ++){//外循环控制的是行数

for(int y = 0 ; y <= 4 ; y ++){//内循环控制的是每行的个数

System.out.print("*");

}

System.out.println( );

}

}

}

*****

****

***

**

*

class ForForDemo{

public void static main(String[] args){

int z = 5;

for(int x = 0; x <= 4; x ++){

for(int y = 0 ; y <= 5-x ; y ++){

System.out.print("*");

}

System.out.println( );

z --;

}

}

或者:

class ForForDemo{

public void static main(String[] args){

for(int x = 1; x <= 5; x ++){

for(int y = x ; y <= 5 ; y ++){

System.out.print("*");

}

System.out.println( );

}

}

*

**

***

****

*****

class ForForDemo2{

public void static main(String[] args){

for(int x = 1 ;x <= 5 ;x ++){

for(int y = 1 ; y < = x; y ++){

System.out.print("*");

}

System.out.println( );

}

}

}

54321

5432

543

54

5

class ForForDemo3{

public void static main(String[] args){

for(int x = 1 ; x <= 5 ; x ++){

for( int y = 5 ; y >= x ; y-- ){

System.out.print(y);

}

System.out.println( );

}

}

}

1

22

333

4444

55555

class ForForDemo4{

public void static main(String[] args){

for(int x = 1; x <= 5 ; x ++){

for(int y = 1 ; y <= x ; y++){

System.out.print(x);

}

System.out.println( );

}

}

}

打印九九乘法表:

class ForForDemo5{

public void static main(String[] args){

for(int x = 1 ; x <= 5 ; x ++){

for(int y = 1; y <= x ;y ++){

System.out.print(x + "*" + y + "=" + (x*y) + "/t");

}

}

}

}

数组的表示方法:

int[] arr = new int[3];

int[] arr = new int[3]{1,2,3};

int[] arr = {1,2,3};

逻辑运算符(6)4 +2 :

4:& | && ||

2:! ^

位运算符:

位运算是直接对二进制位的运算

3+2+2:

<< >> >>>

& |

~ ^

~取反:就是对二进制数每一位0变1,1变0

对两个整数的值进行互换(可以使用第三方变量):

class OperatorDemo{

public static void main(String[] args){

int a = 3 , b = 4;

int c;

c = a ;

a = b;

b = c;

}

}

}

对两个整数的值进行互换(不可以使用第三方变量)

class OperatorDemo{

public static void main(String[] args){

int a =3 , b = 4;

a = a ^ b;

b = a ^ b;

a = a ^ b;

}

}

三元运算符:

class Operator {

public void static main(String args[]){

int x = 3,y;

y = (x >1)?100:200;

System.out.println(y);

}

}

获取三个数中较大的数:

class Operator {

public void static main(String args[]){

int x = 1,y = 2, z = 4,temp,max;

temp = (x > y)?x:y;

max = temp > z ? temp : z;

}

}

if条件语句:

class IfDemo{

public void static main(String[] args){

int month = 9;

if(month <1 || month > 12)

System.out.println(month+"月没有对应的季节");

else if (month >= 3 && month <= 5)

System.out.println(month+"月对应春季");

else if (month >=6 && month <= 8)

System.out.println(month+"月对应夏季");

else if (month >= 9 && month <= 11)

System.out.println(month+"月对应秋季");

else

System.out.println(month+"月对应冬季");

}

}

数组:

找寻最值:

class ArrayDemo {

public void static main(String[] args){

int[] arr = {-11,22,44,93,532};

int max = getMax(arr);

System.out.println("数组的最大值是" + max);

}

public int static getMax(int[] arr){

int maxElement = 0;

if(int x = 0 ; x < arr.length ; x ++){

if(arr[x] > maxElement)

maxElement = arr[x];

}

return maxElement;

}

}

或者:

class ArrayDemo {

public void static main(String[] args){

int[] arr = {22,,23,25,56,76,435,345,34,234,15};

int maxIndex = getMaxIndex(arr);

System.out.println("数组的最大值是"+arr[maxIndex]);

}

public int static getMaxIndex(int[] arr){

int maxIndex = 0;

for(int x = 0 ; x < arr.length; x ++){

if(arr[x]> arr[maxIndex]){

maxIndex = x;

}

}

return maxIndex;

}

}

选择排序:

冒泡排序:

每列一直相邻比较,且较大者右移

class ArrayDemo {

public void static main(String[] args){

int[] arr = {22,44,55,66,77,88,99,9999};

System.out.print("排序之前的数组 = ”);

printArray(arr);

bubbleSort(arr);

System.out.print("排序之后的数组 = ");

}

public void bubbleSort(int[] arr){

for(int x = 0 ; x <= arr.length - 1 ; x ++){

for(int y = 0 ; y <= arr.length - 1 - x ; y ++){

if(arr[y] >arr[y+1]){

int temp = arr[y];

int arr[y] = arr[y +1];

int arr[y+1] = temp;

}

}

}

}

public void printArray(int[] arr){

System.out.print("[");

for(int x ; x <= arr.length ; x ++){

if(x != arr.length - 1 )

System.out.print(","+arr[x]);

else

System.out.println(arr[x] + "]");

}

}

}

二分法(用于有序数列的查找)

class ArrayDemo {

public void static main(String[] args){

int[] arr = {21,90,490,1892,2900};

int index = binarySearch(arr, 490);

System.out.println("index = "+ index);

}

public int static binarySearch(int[] arr, int key){

int min = 0;

int max = arr.length- 1 ;

int mid = (min +max)/2;

while(arr[mid] != key){

if(arr[mid] < key)

min = mid +1;

else if (arr[mid] >key)

max = mid -1;

if(max < min){

return -1;

}

mid = (max +min)/2;

return mid;

}

}

}

或者(位运算符):

class ArrayDemo{

public void static main(String[] args){

int[] arr = {21,33,33,14,4546,23,56,20,09};

int index = binearySearch(arr,23);

System.out.println("index = " + index);

}

public int static binearySearch(int[] arr,int key ){

int max,min,mid;

max = arr.length - 1;

min = 0;

while(min < max) {

mid = (max +min)>>1;

if(arr[mid]> key )

max = mid - 1;

else if(arr[mid] < key)

min = mid + 1 ;

else

return mid;

}

return -1;

}

}

二分法在jdk中当然api了:

class ArrayDemo{

int[] arr = {32,11,89,33,190}

int index = Array.binarySearch(arr,22);

System.out.println("index = " + index );

}

但返回值是个负值,因为要索引的数字不包含在数组中的话,返回值为:(-(插入点)-1)

获取一个十进制数字的2,8,16进制的表现方式:

二位数组:

求和:

class Array2Demo {

public void static main(String[] args){

int sum;

int[][] arr = {{3,1,8},{8,2,1},{90,222,124,24}};

for(int x = 0 ; x <= arr.length ; x ++ ) {

for (int y = 0 ; y <= arr[x].length ; y ++){

sum += arr[x][y];

}

}

System.out.println("sum = " + sum);

}

}

以及所谓的面向对象:(匿名对象的使用)

class Car {

String color = "yellow ";

String price = 1000,000;

public void run(){

System.out.println("wow!!Its so cool to drive such a good car!");

}

}

class CarDemo {

public void static main(String[] args){

//当对对象仅调用一次时,可以用匿名对象

new Car().run();

//匿名对象可以作为实际参数进行传递

show (new Car());

}

public void show(Car car){

ajfjsdjflasj

}

}

封装:

将不需要对外提供的内容都隐藏起来,并提供公共方法对其访问,如getXXX,setXXX

构造函数:

函数名与类名相同

没有返回值

多个构造函数的重载,差别在于参数不同

static关键字的特点:

随着类的加载而加载

优先于对象存在

被所有对象所共享

可以直接被类名所调用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: