The difference between primitive types and reference types in session

The basic type (int, String, etc.) stored in the session is different from the reference type (object).

1. basic type
session.setAttribute("test",1)
-------》Object test = session.getAttribute("test")
-------》test = 2
-------》system.out.print( session.getAttribute("test"));
-------" The print result is: 1 (not 2)



2. reference type
Test test = new Test();
test.setName("小明");
session.setAttribute("test",test)
-------》Object test2 = session.getAttribute("test")
-------》test2.setName(‘xiaoming’);
-------》system.out.print( session.getAttribute("test").getName());
-------" The print result is: xiaoming (the reference type will be automatically updated to the session field)


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324776041&siteId=291194637