0x002 基本数据类型、数组和语句

2016-04-12  本文已影响424人  卖梳子的鲤鱼

0x001 标识符和关键字


标识符

关键字

abstract assert boolean break byte case catch char class constcontinue default do double else enum extends final finally float for goto if implements import instanceof int interface long native new package private protected public return strictfp short static super switch synchronized this throw throws transient try void volatile while

注意:java使用的是Unicode字符集,所以汉字作为标识符也是可以的,但是并不推荐这种方式。

public class Main {

public static void 中文函数(int 参数A,int 参数B){
    System.out.println("参数A:"+参数A+"      参数B:"+参数B);
}
public static void main(String[] args) {
    中文函数(1,2);
    Scanner scanner=new Scanner(System.in);
    scanner.next();
}
}

基本数据类型


8种基本数据类型

逻辑类型

整数类型

类型 位数(字节) 常量 变量 范围
byte 1 12 byte x=12 -27~27-1
short 2 12 short x=12 -215~215-1
int 4 12 int x=12 -231~231-1
long 5 12 long x=12L -263~263-1

注意:java是没有无符号类型的

字符类型

浮点类型

|类型|位数(字节)|常量|变量|有效数字|
|:---:|:---:|:---:|:---:|:---:|:---:|
|float|4|12.0f|float x=12.0f|8|
|double|8|12.0|short x=12.0|16|

注意需要指明数据类型,默认是double,+f是float。

0x002类型转换

精度从低到高:
byte short char int long float double

0x003 输入输出数据

输入

输入使用的是Scanner

    Scanner scanner=new Scanner(System.in);
    boolean isRight=scanner.nextBoolean();//输入一个字符串
    int a=scanner.nextInt();//输入一个int
    float b=scanner.nextFloat();//输入一个float
    String c=scanner.next();//输入一个字符串

输出使用的是

    System.out.println("a:"+a);

println表示输出后回车,无需回车可以使用print函数。
格式化输出:

  System.out.printf("格式化控制部分",表达式a,表达式b,表达式c);

示例:

  System.out.printf("%d,$f",a,b);

0x004数组

声明:

一次声明多个

char[] a,b;==>char a[],b[];
int[] a,b[];==>char a[],b[][];

分配元素

数组名=new 数组元素类型[数组元素个数];
例如:

boy=new int[4];

或者在声明的时候分配

int[] boy=new int[4];

使用int型指定数组大小:

int size=10;
intboy[]=new int[size];

初始化:

int boy[]=new int[4];
boy[0]=1;
boy[1]=2;
boy[2]=3;
boy[3]=4;

或者

int boy[]={1,2,3,4};      
System.out.print(boy[1]); 

数组的长度

boy.length;

注意:

结果:


Paste_Image.png

0x005 运算符、表达式和语句

算术运算

自增自减

算术混合运算精度

关系运算符

逻辑运算符和逻辑表达式

位运算符

instanceof运算符

-判断对象是否属于某一个类

0x006 逻辑语句

if条件语句

  //单条件单分支    
  if(表达式){
       语句
  }
  //单条件双分支
  if(表达式){
       语句
  }else{
      语句
  }
  //多条件多分支
    if(表达式){
       语句
  }else if(条件){
      语句
  }

switch语句

  switch(条件)
  {
      case 常量值1:
            语句
            break;
      case 常量值2:
            语句
            break;
      case 常量值2:
            语句
            break;
      default:
  }

for循环

  for(表达式1;表达式2;表达式3){
        语句
  }

while循环

  while(表达式){
        语句
  }

do-while循环

  do{
        语句
  }while(表达式);

break和continue

上一篇 下一篇

猜你喜欢

热点阅读