1.Java八种基本类型分类
*Java的八种数据类型:
* 1.整型
* 1.1 byte 1字节
* 1.2 int 2字节
* 1.3 short 4字节
* 1.4 long 8字节
* 2.浮点型(小数型)
* 2.1 float 4字节 精度为7位
* 2.2 double 8字节 精度为15位
* 2.3 注意:Java中的小数默认为double型
* 如果定义为 float f = 2.34 会标红报错
* 修改为 float f = 2.34f就行或者进行强制转换 float f = (float)2.34
* 3.布尔型(boolean)
* 只能取值true或false
* 4.字符型(char) 2字节 可以存放一个汉字
2.byte型
package com.zelin.test;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args){
useByte();
}
private static void useByte() {
byte maxValue = Byte.MAX_VALUE;
byte minValue = Byte.MIN_VALUE;
System.out.println("maxValue = "+maxValue);
System.out.println("minValue = "+minValue);
byte b = 1;
Byte b1 = b;
byte b2 = b1;
System.out.println("b = "+ b);
System.out.println("b1 = "+ b1);
System.out.println("b2 = "+ b2);
String str = "121";
Byte b3 = new Byte(str);
byte b4 = Byte.parseByte(str);
Byte b5 = Byte.valueOf(str);
System.out.println("b3 = "+(b3+1));
System.out.println("b4 = "+(b4+1));
System.out.println("b5 = "+(b5+1));
}
}
3.short型
package com.zelin.test;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args){
useShort();
}
private static void useShort() {
short maxValue = Short.MAX_VALUE;
short minValue = Short.MIN_VALUE;
System.out.println("maxValue = "+maxValue);
System.out.println("minValue = "+minValue);
short s1 = 12348;
Short s2 = s1;
short s3 = s2;
System.out.println("s1 = " + (s1+1));
System.out.println("s2 = " + (s2+1));
System.out.println("s3= " + (s3+1));
String str = "12345";
short s4 = new Short(str);
Short s5 = Short.parseShort(str);
Short s6 = Short.valueOf(str);
System.out.println("s4 = " + (s4+1));
System.out.println("s5 = " + (s5+1));
System.out.println("s6 = " + (s6+1));
}
}
4.int类型
package com.zelin.test;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args){
useInt();
}
private static void useInt() {
int maxValue = Integer.MAX_VALUE;
int minValue = Integer.MIN_VALUE;
System.out.println("maxValue = "+maxValue);
System.out.println("minValue = "+minValue);
int i = 189;
Integer j = i;
int k = j;
System.out.println("i = " + (i+22));
System.out.println("j = " + (j+23));
System.out.println("k = " + (k+24));
String str = "123";
int d1 = Integer.parseInt(str);
int d2 = Integer.parseInt(str);
Integer d3 = Integer.valueOf(str);
System.out.println("d1 = " + (d1+1));
System.out.println("d2 = " + (d2+1));
System.out.println("d3 = " + (d3+10));
}
}
5.long
package com.zelin.test;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args){
useLong();
}
private static void useLong() {
long maxValue = Long.MAX_VALUE;
long minValue = Long.MIN_VALUE;
System.out.println("maxValue = "+maxValue);
System.out.println("minValue = "+minValue);
long b = 1899999;
Long b1 = b;
long b2 = b1;
System.out.println("b = "+ b);
System.out.println("b1 = "+ b1);
System.out.println("b2 = "+ b2);
String str = "12166666666";
Long b3 = new Long(str);
Long b4 = Long.parseLong(str);
Long b5 = Long.valueOf(str);
System.out.println("b3 = "+(b3+1));
System.out.println("b4 = "+(b4+1));
System.out.println("b5 = "+(b5+1));
}
}
6.float
package com.zelin.test;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args){
useFloat();
}
private static void useFloat() {
float maxValue = Float.MAX_VALUE;
float minValue = Float.MIN_VALUE;
System.out.println("maxValue = "+maxValue);
System.out.println("minValue = "+minValue);
float f = 2.36f;
Float f1 = f;
float f2 = f1;
System.out.println("f = "+ (f+2.35f));
System.out.println("f1 = "+ (f1+2.35f));
System.out.println("f2 = "+ (f2+2.35f));
String str = "2.36";
float f3 = new Float(str);
float f4 = Float.parseFloat(str);
float f5 = Float.valueOf(str);
System.out.println("f3 = "+(f3+1f));
System.out.println("f4 = "+(f4+1f));
System.out.println("f5 = "+(f5+1f));
}
}
7.double
package com.zelin.test;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args){
useDouble();
}
private static void useDouble() {
double maxValue = Double.MAX_VALUE;
double minValue = Double.MIN_VALUE;
System.out.println("maxValue = "+maxValue);
System.out.println("minValue = "+minValue);
double f = 2.35;
Double f1 = f;
double f2 = f1;
System.out.println("f = "+ (f+2.35));
System.out.println("f1 = "+ (f1+2.35));
System.out.println("f2 = "+ (f2+2.35));
String str = "2.36";
double f3 = new Float(str);
double f4 = Float.parseFloat(str);
double f5 = Float.valueOf(str);
System.out.println("f3 = "+(f3+1));
System.out.println("f4 = "+(f4+1));
System.out.println("f5 = "+(f5+1));
}
}
8.boolean
package com.zelin.test;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args){
useBoolean();
}
private static void useBoolean() {
boolean b = true;
Boolean b1 = b;
boolean b2 = b1;
System.out.println("b = "+ b);
System.out.println("b1 = "+ b1);
System.out.println("b2 = "+ b2);
String str = "true0201";
Boolean b3 = new Boolean(str);
Boolean b4 = Boolean.parseBoolean(str);
boolean b5 = Boolean.valueOf(str);
System.out.println("b3 = " + b3);
System.out.println("b4 = " + b4);
System.out.println("b5 = " + b5);
}
}
9.char
package com.zelin.test;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args){
useChar();
}
private static void useChar() {
char ch = 'a';
Character ch1 = ch;
char ch2 = ch1;
System.out.println("ch = " + ch);
System.out.println("ch1 = " + ch1);
System.out.println("ch2 = " + ch2);
String str = "hello,java!";
char cc = str.charAt(0);
System.out.println("cc = " + cc);
String st = cc+"";
System.out.println("st = " + st);
useSample01();
}
private static void useSample01() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个小写字符:");
String s1 =sc.next();
char c = s1.charAt(0);
c = (char)(c- 32);
System.out.println("输出的大写字符为:" + c);
}
}