java day2

‘0’ - - - -48

‘A’ ------65

'a'------97

前++,变量立刻+1,然后使用该结果使用

后++,先使用变量,再让变量+1

&& , ||, 具有短路效果

三元运算符

数据类型  变量名称 = 条件判断?表达式A:表达式B

int a = 10;
int b = 11;
int max = a < b ? b : a;

方法

修饰符  返回值类型 方法名(参数列表){

代码-----

return;

}

eg,public static  +     viod   +  方法名(参数列表)

public static void main(String[] args) {
    farmer();
    sale();
    cook();
    me();
}
  public static void farmer() {
    System.out.println("种菜");
}
  public static void sale() {
    System.out.println("卖菜");
}
  public static void cook() {
    System.out.println("做菜");
}
  public static void me() {
    System.out.println("吃菜");
}

if 判断

if(关系语句){

语句1;

}else{

语句2;

}

if(判断条件1){

语句1;

}else if(判断条件1) {

语句2;

}

else if(判断条件2) {

语句3;

} else{

语句4;

}

eg。

public static void main(String[] args) {


  int score =60;
if( 90<= score && score <= 100) {
  System.out.println("excellent");

}  else if( 80<= score && score < 90) {
  System.out.println("great");
}
  else if(70<= score && score < 80) {
  System.out.println("good");
}
  else if(60<= score && score < 70) {
    System.out.println("pass");
  }else {
    System.out.println("failed pass");
  }
}

switch语句

switch(表达式){

case 常量1:

语句1;

break;

case 常量2:

语句2;

break;

case 常量3:

语句3;

break;

default:

语句

break;

}

1 .switch后只能跟byte/char/short/int    string字符串  enum枚举

2. 顺序可颠倒,break可省略(匹配到那个case就向下执行,直到遇到break或结束)


public static void main(String[] args) {

int day = 2;

switch(day) {
  case 1:
    System.out.println("Monday");
    break;
  case 2:
    System.out.println("Tuseday");
    break;
  case 3:
    System.out.println("Wenseday");
    break;
  case 4:
    System.out.println("Turseday");
    break;
  case 5:
    System.out.println("Friday");
    break;
  case 6:
    System.out.println("Saturday");
    break;
  default :
    System.out.println("Wrong data type");
}
}

循环

1.for

for (初始化表达式;布尔表达式;步进表达式){

  循环体;

public static void main(String[] args) {

  for(int a = 1;a <=100;a++) {
    System.out.println("wo cuo le " + a);
  }
    System.out.println("stop");
}

}

2 while

while(条件){

循环体;

步进表达式;

}

public static void main(String[] args) {
  int a = 0;
  while(a<=100){
    System.out.println("wo cuo le " + a);
    a+=1;
  }
    System.out.println("stop");
}

3 do  while

do{

循环体;

步进表达式;

}while(条件判断);

public static void main(String[] args) {
  int a = 0;

  do{
    System.out.println("wo cuo le " + a);
    a+=1;
  }while(a<=100);
    System.out.println("stop");

ht6
}

如果从未满足过条件, for,while循环执行0次,但是do-while至少执行一次


方法中return作用

1. 停止当前方法

2.将后面的返回值还给调用处

void方法只能进行单独调用,不能进行打印调用后赋值调用

public class method {

  public static void main(String[] args) {
    int abc = sum(111, 522);
    System.out.println(abc);

}

  public static int sum(int a, int b) {

    int result = a+b;
    return result;
}

}

方法练习一:判断两数是否相同

public class method {

  public static void main(String[] args) {
    boolean result = isSame(6,6);
    System.out.println(result);
}

  public static boolean isSame(int a, int b) {
    if(a == b) {
      return true;
    }else {
      return false;
}
}
}

方法练习二:求1-100的累和

public class method {

  public static void main(String[] args) {
    System.out.println(getSum());
}

  public static int getSum() {
    int result = 0;
    for(int i =1;i<=100;i++) {
      result += i;
}
    return result;
}

}

方法练习三:打印指定次数信息


public class method {

  public static void main(String[] args) {
    printInfo(5);
}

  public static void printInfo(int a) {
    for(int i = 0;i<a;i++) {
    System.out.println("helloWorld");
}
}
}

1.方法定义在类中,不可定义在方法中;

2.void方法无返回值,但可以返回自己;

3.若果方法有返回值,则必须写上return返回值

4.方法中的最后一行return可以省略;

5.一个方法可以有多个return,但确保同时只有一个会被执行到

方法重载于下列相关

1,参数个数

2,参数类型

3,参数的多类型顺序

无关

1,与参数名称无关

2,与方法的返回值类型无关

public static void open(){}       正确
public static void open(int a){}   正确
static void open(int a,int b){}  代码错误,与8行冲突
public static void open(double a,int b){}  正确
public static void open(int a,double b){}  代码错误,与6冲突
public void open(int i,double d){}  代码错误,与5冲突
public static void OPEN(){}  代码正确,非有效重载
public static void open(int i,int j){}  代码错误,与3行冲突

数组

引用数据类型,长度在程序运行期间不可改变

1.静态初始化(指定长度)

数据类型[] 数组名 = new 数据类型[]{元素1,元素2,元素3...};

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

省略模式

数据类型[] 数组名 = {元素1,元素2,元素3...};

2 .动态初始化(指定内容)

数据类型【】 数组名称  = new 数据类型【数组长度】

int[] arr = new int[3];

直接打印数组名称,得到的是数组内存地址的哈希值

JAVA内存划分

1,栈;存放方法中的局部变量

2, 堆;new出来的都放在堆中

堆里存放的,16进制

3,方法区;存放class相关信息

4,本地方法栈:操作系统相关

5,寄存器;cpu相关

数组遍历

public static void main(String[] args) {
  int[] arr = { 1, 2, 3, 4, 5 };
  for (int i = 0; i < arr.length; i++) {
    System.out.println(arr[i]);
}
}

数组获取最大值元素

public class array {

  public static void main(String[] args) {

    int[] arr1 = {1,5,7,6,8,1,9,11};
    int maxNum = arr1[0];
      for(int i = 0;i < arr1.length;i++) {

      if(arr1[i]>=maxNum) {
        maxNum = arr1[i];
}
}
    System.out.println(maxNum);

}

}

数组反转

public class array {

  public static void main(String[] args) {

    int[] arr = {1,5,7,6,8,1,9,11};

    for(int i = 0,j = arr.length-1;i<=j;i++,j--) {
      int temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;

}
    for(int i = 0;i<arr.length;i++)
{
    System.out.println(arr[i]);
}

}

}

数组作为方法参数

public class array {

  public static void main(String[] args) {

    int[] arr1 = {1,2,5,3,6,8,9};
    ptArray(arr1);

}
  public static void ptArray(int[] arr) {
    for(int i = 0;i<arr.length;i++) {
    System.out.println(arr[i]);
}
}

}

数组可以作为方法的参数,当调用方法时,向方法的小括号传参,传递进去的其实是数组的地址值、

数组作为方法的返回值,返回的也是数组的资地址

public class array {

  public static void main(String[] args) {
    int[] result = caculate(10, 20, 30);
    for(int i = 0;i<result.length;i++)
{
    System.out.println(result[i]);
}



}

    public static int[] caculate(int a, int b, int c) {
      int sum = a+b+c;
      int avg = sum / 3;

      int[] result = new int[2];
      result[0] = sum;
      result[1] = avg;
      return result;

}

}

 

方法的参数为基本类型时,传递的是数据值. 方法的参数为引用类型时,传递的是地址值.

原文地址:https://www.cnblogs.com/njuwyx/p/12648354.html