Strindex ('Index of String;2,'Str")=
时间: 2024-01-25 07:02:02 浏览: 9
这个表达式是错误的,因为它缺少一个完整的函数调用,无法执行。正确的函数调用应该是类似于这样的形式:
```
Strindex("Index of String", "Str", 2)
```
这个函数的作用是在字符串 "Index of String" 中从左往右查找第二次出现字符串 "Str" 的位置,并返回该位置的索引值(从 0 开始计数)。因此,该函数的返回值取决于字符串中第二次出现 "Str" 的位置,如果不存在,则返回 -1。
相关问题
String str1 = "hello"; String str2 = new String("hello"); String str3 = "hello"; String str4 = str2.intern(); String str5 = str1 + str3;
这段代码一共创建了4个String对象。具体来说:
1. `String str1 = "hello";` 这行代码创建了一个String对象,存储在常量池中。
2. `String str2 = new String("hello");` 这行代码创建了两个String对象:一个存储在堆内存中,另一个存储在常量池中(因为 "hello" 已经在常量池中存在了,所以不需要再创建一个)。注意,这行代码只会在堆内存中创建一个String对象,不会在常量池中创建新的对象。
3. `String str3 = "hello";` 这行代码并没有创建新的String对象,而是将常量池中已经存在的对象赋值给了`str3`。
4. `String str4 = str2.intern();` 这行代码创建了一个String对象,存储在常量池中。因为 `str2` 是在堆内存中创建的,调用 `intern()` 方法后会将其放入常量池中,并返回常量池中的对象。
5. `String str5 = str1 + str3;` 这行代码创建了一个新的String对象,存储在堆内存中。因为字符串的拼接操作会创建一个新的String对象。
String str=i与 String str=new String(“i”)一样吗?
不完全一样。
在Java中,使用“=”运算符创建对象时,它将引用现有的对象。因此,当您执行以下操作时:
```
String str1 = "i";
String str2 = "i";
```
它们引用相同的字符串对象,因为字符串常量是在编译时创建的,并在内存中重用。
另一方面,当您使用new运算符创建字符串对象时,它会在堆中创建一个新的字符串对象。因此,当您执行以下操作时:
```
String str1 = new String("i");
String str2 = new String("i");
```
它们引用不同的字符串对象,即使它们包含相同的字符序列。
需要注意的是,如果您在创建字符串时使用new运算符,则会创建一个新的字符串对象,即使已经存在具有相同字符序列的字符串常量。因此,以下代码会创建两个不同的字符串对象:
```
String str1 = "i";
String str2 = new String("i");
```
因此,str1和str2引用不同的字符串对象。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)