JAVA的基本数据类型

Java的基本数据类型

1.基本数据类型

 byte, short, int, long, float, double, char, boolean

2.进制

二进制:0b开头
八进制:0开头
十六进制:0x开头

3.float类型与double类型不要用来判断两个数是否“相等”!使用BigDecimal类。

4.转义字符:

\n, \t

5.示例代码

import java.math.*;

public class HelloWorld {
public static void main(String[] args){

/*The primary types of variables in Java are:
byte:
short:
int:
float:
double:
char:
boolean:
long:
*/
byte b = 4;
short s = 120;
int i = 30000;
float f = 3245.89f;
double d = 234242.9984;
char c1 = 'a';
char c2 = '%';
boolean b2 = true;
long l;
l = 994944477;

float f1 = 0.01f;
double f2 = (double)1/100;
if (f1 == f2){
System.out.println("f1 is equal to f2.");
}
else{
System.out.println("f1 is not equal to f2.");
}

System.out.println(f1);
System.out.println(f2);

int i2 = 010;
int i3 = 0x1a;
int i4 = 0b101;

System.out.println(i2);
System.out.println(i3);
System.out.println(i4);

System.out.println(l);

float f3 = 3213131313f;
float f4 = f3 + 1;

System.out.println(f3 == f4);

char c3 = '阿';
System.out.println(c3);
System.out.println((int)c3);

char c4 = '\u0061';
System.out.println(c4);

System.out.println((char)38463);

String sa = "Hello, world!";
String sb = "Hello, world!";
System.out.println(sa == sb);

String sc = new String("Hello, world");
String sd = new String("Hello, world");
System.out.println(sc == sd);

System.out.println(sa == sd);
}
}

posted @ 2020-11-02 23:16  浪淘  阅读(95)  评论(0)    收藏  举报