Java根本数据类型和String类总结
Java基本数据类型和String类总结
目标:
1.掌握Java的基本数据类型使用。
2.熟练使用String的每一个方法。
1.Java的 基本数据类型
Java的数据类型有两种:基本数据类型,引用数据类型;
Java的基本数据类型有8种:
数据类型 大小/位
整型 byte 8
short 16
int 32
long 64
浮点型 float 32
double 64
字符型 char 16
布尔型 boolean turn或false
注:(1)对于整型系统默认为int型,对于浮点型double型。
(2)超出相应的范围会出现溢出;
public class Jiben1 {
public static void main(String[] args){
byte A;
A=97;
byte B=(byte)129;
byte C=(byte)-129;
byte D=(byte)128;
System.out.println(""+A);
System.out.println(""+B);
System.out.println(""+C);
System.out.println(""+D);
}
}
结果为:97
-127
127
-128
(3)可以利用强制转换实现强制地从大范围数据类型到小范围类型,
Eg.
public class lesson01{
public static void main(String[] args){
float f=3.4;
int i=(int)f;
System.out.println(“i=”+i);
}}
结果为:i=3
(4)注意转转义字符的使用 \;
(5)验证某一数据类型的范围
public class lesson01{
public static void main(String[] args){
int max=Integer.MAX_VALUE;
int min=Integer.MIN_VALUE;
System.out.println(“max”+max);
System.out.println(“min”+min);
}}
max=2147483647
min=-2147483648
(6)基本数据类型的默认值
public class lesson01{
static byte a;
public static void main(String[] args){
System.out.println(“byte的默认值为”+a);
}}
2.String类
String在Java中是类。
String表示的是字符串
构造方法的使用:
结果为:13
length=28
bool=false
第8个字符是:3
索引0位置的字符为e
索引1位置的字符为u
索引2位置的字符为r
索引3位置的字符为i
索引4位置的字符为y
索引5位置的字符为u
索引6位置的字符为i
索引7位置的字符为3
索引8位置的字符为7
索引9位置的字符为4
索引10位置的字符为3
索引11位置的字符为2
索引12位置的字符为8
索引13位置的字符为9
索引14位置的字符为^
索引15位置的字符为%
索引16位置的字符为^
索引17位置的字符为&
索引18位置的字符为*
索引19位置的字符为&
索引20位置的字符为D
索引21位置的字符为J
索引22位置的字符为H
索引23位置的字符为K
索引24位置的字符为2
索引25位置的字符为3
索引26位置的字符为1
索引27位置的字符为2
51
101
117
2
5
false
3
eur
yu
3743289^%^&*&DJHK2312
3
Buriyui3743289^%^&*&DJHK2312
buweiyui3743289^%^&*&DJHK2312
euriyui3743289^%^&*&DJHK2312
xabcdabcdabcd
xxxx
xxxx
true
false
true
false
ps.这一部分是自己的练习,结果有一点乱
Java基本数据类型和String类总结
目标:
1.掌握Java的基本数据类型使用。
2.熟练使用String的每一个方法。
1.Java的 基本数据类型
Java的数据类型有两种:基本数据类型,引用数据类型;
Java的基本数据类型有8种:
数据类型 大小/位
整型 byte 8
short 16
int 32
long 64
浮点型 float 32
double 64
字符型 char 16
布尔型 boolean turn或false
注:(1)对于整型系统默认为int型,对于浮点型double型。
(2)超出相应的范围会出现溢出;
public class Jiben1 {
public static void main(String[] args){
byte A;
A=97;
byte B=(byte)129;
byte C=(byte)-129;
byte D=(byte)128;
System.out.println(""+A);
System.out.println(""+B);
System.out.println(""+C);
System.out.println(""+D);
}
}
结果为:97
-127
127
-128
(3)可以利用强制转换实现强制地从大范围数据类型到小范围类型,
Eg.
public class lesson01{
public static void main(String[] args){
float f=3.4;
int i=(int)f;
System.out.println(“i=”+i);
}}
结果为:i=3
(4)注意转转义字符的使用 \;
(5)验证某一数据类型的范围
public class lesson01{
public static void main(String[] args){
int max=Integer.MAX_VALUE;
int min=Integer.MIN_VALUE;
System.out.println(“max”+max);
System.out.println(“min”+min);
}}
max=2147483647
min=-2147483648
(6)基本数据类型的默认值
public class lesson01{
static byte a;
public static void main(String[] args){
System.out.println(“byte的默认值为”+a);
}}
2.String类
String在Java中是类。
String表示的是字符串
构造方法的使用:
public class Jibenx { //定义字符串变量 public static void main(String[] args){ String Str1="euriyui3743289^%^&*&DJHK2312"; int dd=Str1.indexOf(57); System.out.println(""+dd); String Str2="abcd"; String Strx="abcdabcdabcdabcd"; //得到一个字符串的长度 int length=Str1.length(); System.out.println("length="+length); //当且仅当Str1为0时,输出true boolean bool=Str1.isEmpty(); System.out.println("bool="+bool); //获取指定的索引位置的字符 char c=Str1.charAt(7); System.out.println("第8个字符是:"+c); //获取指定的索引位置的字符 for(int i=0;i<Str1.length();i++){ char s=Str1.charAt(i); System.out.println("索引"+i+"位置的字符为"+s); } //返回指定索引处的字符 int code=Str1.codePointAt(7); System.out.println(""+code); //返回指定索引之前的字符 int A=Str1.codePointBefore(1); System.out.println(""+A); int C=Str1.codePointBefore(2); System.out.println(""+C); //返回此 String 的指定文本范围中的 Unicode 代码点数。 int B=Str1.codePointCount(1, 3); System.out.println(""+B); //返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引。 int D=Str1.offsetByCodePoints(1, 4); System.out.println(""+D); // boolean E=Str1.equals(Str2); System.out.println(""+E); //拆分,限制 String[] F=Str1.split("i",5); System.out.println(""+F.length); System.out.println(""+F[0]); System.out.println(""+F[1]); System.out.println(""+F[2]); //拆分 String[] G=Str1.split("i"); System.out.println(""+G.length); //替换 String Str3=Str1.replace("e","B"); System.out.println(""+Str3); //全部替换 String Str4=Str1.replaceAll("eur", "buwe"); System.out.println(""+Str4); //全部替换 String Str5=Str1.replaceAll("er", "buwe"); System.out.println(""+Str5); //替换第一个 String Str6=Strx.replaceFirst(Str2, "x"); System.out.println(""+Str6); //全部替换 String Str7=Strx.replaceAll(Str2, "x"); System.out.println(""+Str7); //替换 String Str8=Strx.replace(Str2, "x"); System.out.println(""+Str8); //包含指定序列 boolean bool1=Str1.contains("eur"); System.out.println(""+bool1); // boolean bool2=Str1.contains("rur"); System.out.println(""+bool2); // boolean bool3=Str1.contains("e"); System.out.println(""+bool3); // boolean bool4=Str1.matches(Str2); System.out.println(""+bool4); } }
结果为:13
length=28
bool=false
第8个字符是:3
索引0位置的字符为e
索引1位置的字符为u
索引2位置的字符为r
索引3位置的字符为i
索引4位置的字符为y
索引5位置的字符为u
索引6位置的字符为i
索引7位置的字符为3
索引8位置的字符为7
索引9位置的字符为4
索引10位置的字符为3
索引11位置的字符为2
索引12位置的字符为8
索引13位置的字符为9
索引14位置的字符为^
索引15位置的字符为%
索引16位置的字符为^
索引17位置的字符为&
索引18位置的字符为*
索引19位置的字符为&
索引20位置的字符为D
索引21位置的字符为J
索引22位置的字符为H
索引23位置的字符为K
索引24位置的字符为2
索引25位置的字符为3
索引26位置的字符为1
索引27位置的字符为2
51
101
117
2
5
false
3
eur
yu
3743289^%^&*&DJHK2312
3
Buriyui3743289^%^&*&DJHK2312
buweiyui3743289^%^&*&DJHK2312
euriyui3743289^%^&*&DJHK2312
xabcdabcdabcd
xxxx
xxxx
true
false
true
false
ps.这一部分是自己的练习,结果有一点乱