1. Java基础类型
基础类型 | 封装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
doubel | Double |
char | Character |
2. String
显而易见String并不是Java的8大基础类型,String是一个类,String还有一个名字叫引用数据类型。
int a=10;
String s;
String s1="Hello";
a在栈中就拥有10的值,但是s只是一个String的管理者类似于C中的指针
例如
String s;
只是有了一个叫s的老大 管理员,但是现在s的手下还没有小弟。(而且我们的s不是很厉害,一次只能带一个小弟)
现在呢?
String s1="Hello";
s1管理员手下有了一个叫Hello的小弟,
俗话说一字符串不容二虎String,好吧这是窝自己杜撰的。
s=s1;
是不是我们的s也带了一个叫Hello的小弟呢?并不是喔,同样拿指针来理解s和s1管理的Hello是同一个人(有时候一个Boss就很蓝瘦了,现在居然来2个?Hello哥表示非常悲伤开心)
Sring无法使用foreach循环
String的methods总结
在这不得不强调一遍,Java中并没有办法对一个String本身做事情,所有的String resulting都是一个替身使者这点窝们可以看看总结表下面的2个substring Methods都会return一个String回来就很好理解这个事情了
以下method全部摘自Java SE
Return | Method | Description |
---|---|---|
char | charAT(int index) | Returns the char value at the specified value |
boolen | equals(Object anObject) | Compares this string to the specified object . |
int | length() | Returns the length of this string. |
int | indexOf(int ch) | Returns the index within this string of the first occurrence of the specified character. |
int | indexOf(int ch,int fromIndex) | Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index. |
int | indexOf(String str) | Returns the index within this string of the first occurrence of the specified substring. |
int | lastIndexOf(int ch) | Returns the index within this string of the last occurrence of the specified character. |
int | lastIndexOf(int ch,int fromIndex) | Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. |
int | lastIndexOf(String str) | Returns the index within this string of the last occurrence of the specified substring. |
int | lastIndexOf(String str,int fromIndex) | Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. |
string | substring(int beginIndex) | Returns a string that is a substring of this string.返回beginIndex后面的substring |
string | substring(int beginIndex,int endIndex) | Returns a string that is a substring of this string.返回beginIndex到endIndex前面一个位置的那个substring |