小编kum*_*nny的帖子

实习方法概念混淆作为输出随着不同版本而改变

对于Java 1.6版,输出是false true,但是对于版本1.8,输出更改为true true.

有人可以解释为什么会这样吗?

Intern方法用于引用堆中创建的对象的相应字符串常量池,如果该对象不存在,则它将创建String常量池.如果我的理解是错误的,请纠正我.

public class Intern_String2 {

 public static void main(String[] args) {

  String s1 = new String("durga");  //object created in heap

  String s2 = s1.concat("software");
  //object durga software created in heap at runtime

  String s3 = s2.intern();
  // create durga software object in string constant pool as none exist.

  System.out.println(s2==s3);//should be false but print true in 1.8 version.

  String s4 = "durgasoftware";
  System.out.println(s3==s4);//prints true in both version..      
 }
}
Run Code Online (Sandbox Code Playgroud)

java string

7
推荐指数
1
解决办法
275
查看次数

在java中格式化具有由冒号标识的变量的字符串

我有一个Stringid 值的占位符

"Input url -> "/student/:id/"

我需要插入这样一个值以使结果看起来像

Output url" -> /student/230/"

我们可以使用 String 的 format() 方法吗,我不想在我的 url 中使用 %d,只是想要一种替换 :id 变量的方法。

java string format

3
推荐指数
1
解决办法
358
查看次数

RestTemplate getForObject 无法映射自定义类

自定义类结构 DocumentListVO 与 JSON 响应一对一映射,但是在使用时 DocumentListVO dv1 = restTemplate.getForObject(uri,DocumentListVO.class),它抛出错误并显示以下堆栈跟踪:

GET request for
"http://aaa.ddd.com:8081/bpi1/service/aa/documen1t/list23"
resulted in 200 ()
    [2018-05-28 12:49:46,397]-DEBUG org.springframework.web.client.RestTemplate  - Reading [class
com.fascorp.isis.ejb.documentVO.DocumentListVO1] as
"application/json;charset=UTF-8" using
[org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@58206f8]
    [2018-05-28 12:49:46,399]-DEBUG org.apache.http.wire  - http-outgoing-0 << "0[\r][\n]"
    [2018-05-28 12:49:46,400]-DEBUG org.apache.http.wire  - http-outgoing-0 << "[\r][\n]"
    [2018-05-28 12:49:46,402]-DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  -
Connection [id: 0][route: {}->http://aaa.ddd.com:8081] can be kept
alive indefinitely
    [2018-05-28 12:49:46,404]-DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  -
Connection released: [id: 0][route:
{}->http://aaa.ddd.com:8081][total kept alive: 1; route allocated:
1 of 2; total allocated: 1 of 10]
    org.springframework.http.converter.HttpMessageNotReadableException:
Could …
Run Code Online (Sandbox Code Playgroud)

spring json

2
推荐指数
1
解决办法
1505
查看次数

我们如何在没有类名的情况下访问静态变量

我们如何访问没有类名的静态变量?静态变量总是用类名限定,但在这种情况下,我可以在没有类名的情况下使用它。怎么可能?

    class Student
    {
    String  email;
    String name;
    long phone;
    static String school="jlc"; 

    public static void main(String[] args)
    {

    Student st= null;
    System.out.println(school);//this should be Student.school but its working.
    }


    }
Run Code Online (Sandbox Code Playgroud)

在创建学生对象后的下面程序中,变量已经加载到内存中,但我不能不使用对象引用直接访问它。但我们可以为静态做。

class Student
{
String  email;
String name;
long phone;
static String school="jlc"; 

public static void main(String[] args)
{

Student st= new Student();
System.out.println(email);
}


}
Run Code Online (Sandbox Code Playgroud)

java static

1
推荐指数
1
解决办法
5390
查看次数

标签 统计

java ×3

string ×2

format ×1

json ×1

spring ×1

static ×1