J2SE--基础语法一

来源:互联网 发布:雅思词汇书推荐 知乎 编辑:程序博客网 时间:2024/04/28 05:35
  • 程序格式
public class HelloWorld { //并排语句之间加空格    static int j = 9;    //程序块空一行    public static void main(String[] asdfasf) { //左大括号后面缩进        System.out.println("HW");         System.out.println(123);        System.out.println(j);        int i = 8; //运算符两边加空格    }    public static void m() {    }} //成对写大括号
  • 标识符
    • 1.字母、下划线、或者美元符号$或者数字组成
    • 2.字母、下划线、或者美元$开头
    • 3.不和关键字一样
  • 关键字(特殊含义的字符串)
  • 常量
    • 整型常量 123
    • 实型常量 3.14
    • 字符常量 ‘abc’
    • 字符串常量 ‘abc’
    • 逻辑常量 true、false
  • 变量(内存中一个小区域)
public class testva {    static int j;//成员变量                //作用域:大括号内    public void m() {        int i = 0;        System.out.println(i);    }    public static void main(String arg[]) {        int i = 1;//局部变量      System.out.println(i);      System.out.println(j);                boolean b = true;      if(b) {          int c = 2;          System.out.println(c);                }    }}
  • 数据类型

    • 基本数据类型
      数值型(1.整数类型byte 1、short 2、int 4、long 8 如:long l == 88888l 2.浮点类型float 4、double 8 如:3.14e2==3.14*10^2、float f == 3.14f)
      字符型(char)
      布尔型(boolean)
    • 引用数据类型
      类(class)
      借口(interface)
      数组
  • 类型转换(容量大道容量小转换要强制转换)

    • boolean不能转换
    • 实数常量(1.1)默认为double,整型常量(1)默认为int
    • 容量小到容量大转换:char、short、byte不能互相转换,在计算时首先转换为int
    • 顺序short、byte、char->int->long->float->double
public class TestConvert {    public static void main(String arg[]) {    int Ia = 222;    int Ib = 333;    float f = (float)0.1; //也可以写成0.1f,一个是double强制转换成的,一个是一开始就是float    double D1 = (Ia+Ib)*0.1; //0.1为double型,double->double    float F1 = (float)((Ia+Ib)*0.1); //计算之后为double型,double->float    byte B1 = 65;         //不超过byte(127)的范围,int可直接给byte    byte B2 = 127;            byte B3 = (byte)(B1 + B2); //B1+B2要先转换成int型计算    float F2 = 1.23f;    //必须加f  long L1 = 123;       //int->double  long L2 = 30000000000L;//int过大,要加L    }}
  • 运算符
    1.&&短路与 ||短路或 和 & |的区别:
    boolean i = 0&&(a < 3);(a不做运算)
    boolean i = 0&(a < 3);(a和b都运算)
    2.“+”的特殊用法:
    String c = “hello” + “world”; //c = “helloworld”
    System.out.println(“c =” + c); //打印”c = c”
0 0
原创粉丝点击