J2SE--基础语法一

  • 程序格式
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值