码迷,mamicode.com
首页 > 编程语言 > 详细

Java基本语法

时间:2019-04-15 15:57:40      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:数值   prim   [1]   splay   报错   tin   nbsp   one   bre   

注释

  单行注释:       //  注释内容

  多行注释:    /* 注释内容 */

  文档注释:  /**   注释内容   */

数据类型

  整型

    byte    short    int    long

  浮点型

    float   double

  字符类型

    char

  布尔类型

    boolean

变量

  1.未初始化的变量不能使用

  2.常量使用final来指定

  3.命名规范:

    1)必须是数字,字母,下划线和美元符号组成,注意不可以数字开头

    2)首字母小写,驼峰原则

    3)见名知意,不要乱起名字

    4)常量的命名应该全部大写

运算符

  算术运算符

    +  -  *  /  %

    注意:整数除0报异常,浮点数除0,结果为NAN

  赋值运算符

    +=  -=  *=  /=  %=         =

    注意:左边操作数和右边操作数不同时,结合赋值运算符会自动将右边的操作数类型强转为左边操作数的类型

  自增和自减运算符

    ++        --

    注意:前缀是先自增(自减)再运算,后缀是先运算后自增(自减)

  逻辑运算符

    &&   ||    !

    注意:&&和||是短路的

  位运算符

    >>   >>>   <<

    注意:使用>>>时符号位有0填充

字符串:String

  1.拼接符号: +

  2.字符串相等使用equals方法检查

  3.空串:2个引号之间没有任何字符,长度为0

  4.null串:不是字符串,不可以调用方法,否则报错

  注意:要先检查字符串是不是null串再检查是不是空串

  常用api

  boolean  equals(Object other)

  boolean  startsWith(String prefix)

  boolean  endsWith(String suffix)

  int  length()

  String  substring(int  begin)

  String  substring(int  begin ,  int  end)

  String  toUpperCase()

  String  toLowerCase()

  String  trim()

字符串构建器:StringBuilder

  常用api

  StringBuilder  append(String str)

  void  setCharAt(char  ch)

  StringBuilder  insert(int  index , String str)

  String  toString()

输入输出

  输入:java.util.Scanner

  技术图片
1 Scanner sc = new Scanner(System.in);
2 String str = sc.nextLine();
3 int  num  =  sc.nextInt();
4 float f = sc.nextFloat();
5 double d = sc.nextDouble();
输入语句示例

  输出:System.out

  System.out.println("要输出的内容")

  格式化输出:System.out.printf("%8.2f",546.165)  =>    546.16

控制流程

  条件语句:if

  技术图片
if(a>b){
  System.out.println("a>b") ;
}else if(a=b){
  System.out.println("a=b") ;
}else{
  System.out.println("a<b") ;
}
条件语句示例

  循环语句: for  ,  while  , do-while

  技术图片
 1 for(int i =0;i<100;i++){
 2     System.out.println(i);  
 3 }
 4 int j = 0;
 5 while(j<100){
 6     System.out.println(j);
 7     j++;      
 8 }
 9 int k = 0;
10 do{
11     System.out.println(k);
12     k++;   
13 }while(k<100);
循环语句示例

  开关语句:switch

  技术图片
int num = 2;
switch(num){
case 1:
        System.out.println("1");
        break;  
case 2:
        System.out.println("2");
        break;  
default:
        System.out.println("没有找到");
        break;  
}
开关语句示例

  中断控制流程语句:break         continue

大数值:java.math.BigInteger ,  java.math.BigDecimal

  获取对象:BigInteger.valueOf(long x)          BigDecimal.valueOf(long x)      BigDecimal.valueOf(long x,int scale)

  使用方法来进行大数值的精确运算

  +     add(BigInteger other)      add(BigDecimal other)

  -      subtract(BigInteger other)    subtract(BigDecimal other)

  *      multiply(BigInteger other)    multiply(BigDecimal other)

  /  divide(BigInteger other)      divide(BigDecimal other)

  比较:compareTo(BigInteger other)        compareTo(BigDecimal other)  

数组

  数组的声明:

  int[]  a = {1,2,3,4}

  int[]  b = new int[4];

  数组的赋值:

  index从0开始的自然数

  a[1] = 2;  给下标为1的数组元素赋值为2

  for each循环遍历数组:

for(variable:collection){
    statement;  
}

  数组的初始化和匿名数组

  1.   创建数组的同时赋值

    int  smallPrimes  = {2 , 3 , 5 , 7 }

  2.   初始化一个匿名数组

    new  int[]{ 2 , 3 , 5 ,7}

  数组的固定属性:length

  数组一点建立,数组的长度不可变,使用数组名.length来取出数组长度

  数组的拷贝:

  1. 使用Arrays类中的copy方法

     Arrays.copy(原数组,新数组长度)

    如果新数组的长度大于原数组的长度,原数组中的所有内容赋值到新数组中,且新数组中的比原数组中多余的部分初始化为null,或者0

    如果新数组的长度小于原数组的长度,则舍去缺失的部分

  2.使用System中的arraycopy方法

    System.arraycopy(src, srcPos, dest, destPos, length);

    src :原数组

    srcPos: 原数组开始复制位置

    dest:新数组

    destPos:新数组开始接受位置

    length: 复制的长度

  数组的排序:

  Arrays.sort(数组名),将数组按照升序排列

  数组元素的查找:

  Arrays.binarySearch(数组,开始位置,结束位置,查找元素)

  返回查找元素的下标

多维数组

  int[][]  a = { { 1 , 2 , 3 } , { 3 , 4 } , { 5 , 6 , 7 , 8} }

  int[][]  b = new  int[3][]

  多维数组就是一个数组的数组,是不规则的。  

 

 

  

 

Java基本语法

标签:数值   prim   [1]   splay   报错   tin   nbsp   one   bre   

原文地址:https://www.cnblogs.com/wlzg/p/10703807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!