学习JAVA的第二天

时间:2021-02-18 13:18:03   收藏:0   阅读:0

三.第一个java程序

public class hello{
    public static void main(String []args){
        System.out.println("HelloWorld");
        
    }
    
}

public :访问修饰符,用于控制程序的其他部分对这段代码的访问级别

class:表明java程序中的全部内容都包含在类中,class后面紧跟类名

定义类名规则

注释

JAVA中注释有三种:

字节

数据类型

Java是一种强类型语言,这就意味着必须为每一个变量声明一种类型,在Java中一共有8中基本类型,其中有4种整型,2种浮点类型,1种字符类型。

整型

类型 存储需求 取值范围
byte 1字节 -128—127
short 2字节 -32768—32767
int 4字节 -2147483648—2147483647
long 8字节 -9223372036854775808—9223372036854775807

在通常情况下,int类型最常用,如果想要表示整个地球的居住人口就需要使用到long类型了。

byte和short类型主要用于特定的应用场合,例如底层的文件处理或者存储空间很宝贵时的大数组。

长整型数值有一个后缀L,如(10000000000L),二进制前缀0b ,八进制前缀0 ,十六进制前缀0x。

还可以为数字字面量加下划线如(100_0000),表示100万,这些下划线只是为了让人更容易读,Java编译器会去除这些下划线。

浮点类型

类型 存储需求 取值范围
float 4字节 大约+-3.40282347E+38F(有效位数为6-7位)
double 8字节 大约+-1.79769313486231570E+308(有效位数为15位)

double这种类型的数值精度是float的两倍

float类型的数值有一个后缀F或f,如(3.14F),如果没F的浮点数值如(3.14)会被默认为double类型,当然也可以在浮点数后面添加后缀D或d.

public class Demo03 {
    public static void main(String[] args) {
        // 整数拓展  进制 二进制0b  八进制0 十六进制0x
        int i = 10;
        int i2 = 010;
        int i3 = 0x10;
        int i4 = 0b10;
        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println(i4);
        /*=============================================
        浮点数拓展 银行业务怎么表示 用类 BigDecimal数学工具类
        float  有限 离散 舍入误差 接近但不等于
        double
        最好完全使用浮点数比较*/
        float f = 0.1f;
        double d = 1.0/10;
        System.out.println(f==d);//返回false
        System.out.println(f);
        System.out.println(d);
        float d1 = 258458456451f;
        double d2 = d1 + 1;
        System.out.println(d1==d2);//返回true
        }
}
        
public class Demo06 {
    public static void main(String[] args) {
        //操作比较大的数的时候,注意溢出问题
        int money = 10_0000_0000;
        System.out.println(money);
        int years = 20;
        long total = years * (long)money;
        System.out.println(total);

    }
}

Char类型

char类型的字面量要用单引号括起来,如‘A‘是编码值为65的字符常量,它与“A”不同,“A”是包含一个字符A的字符串,所有字符本质都是数字

转义序列 名称 Unicode值
\b 退格 \u0008
\t 制表 \u0009
\r 回车 \u000d
\n 换行 \u000a
\ " 双引号 \u0022
\ ’ 单引号 \u0027
\ \ 反斜杠 \u005c
public class Demo04{
    public static void main(String []args){
       char c1 = ‘a‘;
        char c2 = ‘中‘;
        System.out.println(c1);
        System.out.println((int)c1);//强制转换
        System.out.println(c2);
        System.out.println((int)c2);//强制转换 
        
        System.out.println("Hello\tWorld");
        System.out.println("Hello\nWorld");
    }
    
}

boolean类型

boolean(布尔)类型有两个值:false和true,用来判定逻辑条件。整型值和布尔值之间不能进行相互转换

public class Demo05 {
    public static void main(String[] args) {
        int i = 128;
        double b = i;
        System.out.println(i);
        System.out.println(b);
        //1.不能把布尔值进行转换
        //2.不能把对象类型转换为不相干的类型
        //3.在把高容量转换到低容量的时候,强制转换
        //4.转换的时候可能存在内存溢出,或者精度问题
        System.out.println((int)23.7);
        System.out.println((int)-45.89f);

        char c = ‘a‘;
        int d = c+1;
        System.out.println((char) d);
    }
}

变量

Type VarName [=value];

数据类型 变量名 = 值;

注意事项

public class Demo07 {
    public static void main(String[] args) {
        //int a,b,c;//程序可读性
        int a = 1;
        int b = 2;
        int c = 3;
        String name = "shuwang";
        char x = ‘x‘;
        double pi = 3.14;
    }
}

变量的命名规则

变量的作用域

public class Demo08{
   static int allClicks=0;//类变量
    String str="Hello World";//实例变量
   public void method(){
        int i=0;//局部变量
    }
}
public class Demo08 {

    static  double salary = 2500;
    //属性:变量
    //main 方法
    //实例变量:从属于对象 如果不自行初始化,这个类型的默认值0 0.0  u0000
    //布尔值:默认是false
    //除了基本类型,其余都是null;
    String name;
    int age;

    public static void main(String[] args) {
    //局部变量:必须声明和初始化值
    int i = 10;
    System.out.println(i);

    //变量类型  变量名字 = new com.shuwang.base.Demo08;
    Demo08 demo08 = new Demo08();
    System.out.println(demo08.age);
    System.out.println(demo08.name);

    //类变量 static
        System.out.println(salary);
    }
    public void add(){

    }
}
public class Demo09 {
    //修饰符,不存在先后顺序
    //final static  double PI = 3.14;
    static final double PI = 3.14;
    public static void main(String[] args) {
        System.out.println(PI);
    }
}

常量

final 产量名=值;
final double PI=3.1415296;
    

类型转换

public class Demo04 {
    public static void main(String[] args) {
        int i = 128;
        byte b = (byte) i;//强制转换
        System.out.println(i);
        System.out.println(b);
    }
}
public class Demo02 {
    public static void main(String[] args) {
        long a = 5484754156456474L;
        int b = 123;
        short c = 10;
        byte d = 8;

        System.out.println(a+b+c+d);//long
        System.out.println(b+c+d);//int


    }
}

运算符

优先级()

public class Demo01 {
    public static void main(String[] args) {
        //二元运算符
        //ctrl + d :复制当前到下一行
        int a = 10;
        int b = 20;
        int c = 10;
        int d = 10;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);
        System.out.println(a%b);
    }
}
public class Demo02 {
    public static void main(String[] args) {
        long a = 5484754156456474L;
        int b = 123;
        short c = 10;
        byte d = 8;

        System.out.println(a+b+c+d);//long
        System.out.println(b+c+d);//int


    }
}
public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回的结果:正确,错误 布尔值
        int a = 10;
        int b = 20;
        int c = 21;
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
        System.out.println(c%a);//1
    }
}
public class Demo04 {
    public static void main(String[] args) {
        //++ -- 自增 自减 一元运算符
        int a = 3;
        int b = a++;//执行完这行代码后,先给b赋值,在自增
        int c = ++a;//执行完这段代码前,先自增,在给c赋值
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        //幂运算 2^3 2*2*2=8
        double pow = Math.pow(2,3);
        System.out.println(pow);
    }
}
public class Demo05 {
    public static void main(String[] args) {
        //与(and)或(or)非(取反)
        boolean a = true;
        boolean b = false;
        System.out.println("a && b:"+(a&&b));//
        System.out.println("a || b:"+(a||b));
        System.out.println("! (a && b):"+!(a&&b));


        //短路运算测试
        int c = 5;
        boolean d = (c<4)&&(c++<4);
        System.out.println(d);
        System.out.println(c);
    }
}
public class Demo06 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101

      A&B = 0000 1100 两个都为1,才得1
      A|B = 0011 1101 两个都为0得0,否则都是1
      A^B = 0011 0001 相同为0,不相同为1
      ~B  = 1111 0010 和B完全相反
         */
        /*
        位运算,效率极高
         << 左移  *2
         >> 右移  /2
         */
        System.out.println(2<<3);

    }
}
public class Demo07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        a+=b;//a=a+b
        a-=b;//a=a-b
        System.out.println(a);

        //字符串连接 + ,String类型 字符串类型
        System.out.println(""+a+b);//1020 字符串在前面 拼接
        System.out.println(a+b+"");//30   字符串在后面 依旧运算
    }
}
public class Demo08 {
    public static void main(String[] args) {
        //条件运算符
        // x ? Y : Z
        // 如果x==true,则结果为y,否则结果为z
        int score = 50;
        String type = score < 60 ? "不及格":"及格" ;
        System.out.println(type);



    }
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!