为什么2==2是false 2L==2L是true
时间: 2023-08-13 17:02:34 浏览: 72
2==2是false是因为在Java中,==比较两个对象的引用是否相等,而2是一个基本数据类型,不是对象。因此,2==2比较的是两个基本数据类型的值,结果为true。
而2L==2L是true是因为2L是一个long类型的字面量,也就是一个对象,所以==比较的是两个对象的引用,因为2L==2L比较的是同一个对象,所以结果为true。
相关问题
为什么用get方法得到的相同的值判断==是false,但是在set时赋值以2L时get判断==为true
这个问题的答案跟具体的代码实现有关系,但是一般情况下,如果在set方法中给属性赋值的是基本数据类型,那么每次调用set方法时,都会创建一个新的包装类对象来存储这个基本数据类型的值,所以在使用get方法获取属性值时,每次返回的都是一个新的对象。因此,如果在set方法中给属性赋值为2L,那么每次使用get方法获取属性值时,都会返回同一个值为2L的Long类型对象,因为Long类型是不可变对象,它的值一旦被创建,就不能改变。
因此,如果在使用get方法获取属性值时,得到的是同一个对象,那么使用`==`比较两个对象时,结果就会为true。而如果在使用get方法获取属性值时,得到的是不同的对象,那么使用`==`比较两个对象时,结果就会为false,因为它们的引用是不同的。
需要注意的是,这只是一般情况下的情况,具体的实现还要看代码的具体实现。如果使用了对象池等技术,可能会影响对象的创建和回收,从而影响结果。
d2l.plt.scatter()是什么函数
d2l.plt.scatter() 是 Deep Learning - The Straight Dope (D2L) 书中定义的一个函数,用于绘制散点图。它是基于 Matplotlib 库实现的,具有以下特点:
- 支持自定义点的颜色、大小、形状等属性。
- 支持添加标题、坐标轴标签等注释。
- 支持同时绘制多个散点图。
该函数的定义如下:
```python
def set_figsize(figsize=(3.5, 2.5)):
"""Set matplotlib figure size."""
use_svg_display()
d2l.plt.rcParams['figure.figsize'] = figsize
def use_svg_display():
"""Use svg format to display plot in jupyter."""
d2l.plt.rcParams['svg.fonttype'] = 'none'
def plot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None,
ylim=None, xscale='linear', yscale='linear', fmts=None,
figsize=(3.5, 2.5), axes=None):
"""Plot multiple lines."""
if legend is None:
legend = []
set_figsize(figsize)
axes = axes if axes else d2l.plt.gca()
# Return True if X (tensor or list) has more than 1 dimension
def has_one_dim(X):
if isinstance(X, np.ndarray) or isinstance(X, list):
return len(X.shape) == 1
else:
return len(X.size()) == 1
# Convert Y to list so that len(Y) is always defined
if Y is None:
Y = []
if not isinstance(Y, list):
Y = [Y]
if not isinstance(X, list):
X = [X] * len(Y)
if len(X) != len(Y):
X = X * len(Y)
# Format lines and points
if fmts is None:
fmts = ['-'] * len(X)
elif isinstance(fmts, str):
fmts = [fmts] * len(X)
# Plot
for x, y, fmt in zip(X, Y, fmts):
if has_one_dim(x):
x = np.arange(len(y)) + 1
axes.plot(x, y, fmt)
axes.set_xlabel(xlabel)
axes.set_ylabel(ylabel)
axes.set_xscale(xscale)
axes.set_yscale(yscale)
if xlim:
axes.set_xlim(xlim)
if ylim:
axes.set_ylim(ylim)
if legend:
axes.legend(legend)
axes.grid()
d2l.plt.show()
def show_images(imgs, num_rows, num_cols, scale=2):
"""Plot a list of images."""
figsize = (num_cols * scale, num_rows * scale)
_, axes = d2l.plt.subplots(num_rows, num_cols, figsize=figsize)
for i in range(num_rows):
for j in range(num_cols):
axes[i][j].imshow(imgs[i * num_cols + j])
axes[i][j].axes.get_xaxis().set_visible(False)
axes[i][j].axes.get_yaxis().set_visible(False)
return axes
def plot_learning_curves(train_acc, test_acc, train_loss, test_loss):
"""Plot the learning curves."""
d2l.plt.plot(train_acc, linestyle='-', color='blue', label='train accuracy')
d2l.plt.plot(test_acc, linestyle='--', color='red', label='test accuracy')
d2l.plt.xlabel('epochs')
d2l.plt.ylabel('accuracy')
d2l.plt.legend(loc='upper right')
d2l.plt.twinx()
d2l.plt.plot(train_loss, linestyle='-', color='green', label='train loss')
d2l.plt.plot(test_loss, linestyle='--', color='orange', label='test loss')
d2l.plt.ylabel('loss')
d2l.plt.legend(loc='upper left')
d2l.plt.show()
def use_svg_display():
"""Use svg format to display plot in jupyter."""
d2l.plt.rcParams['svg.fonttype'] = 'none'
def set_figsize(figsize=(3.5, 2.5)):
"""Set matplotlib figure size."""
use_svg_display()
d2l.plt.rcParams['figure.figsize'] = figsize
def plot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None,
ylim=None, xscale='linear', yscale='linear', fmts=None,
figsize=(3.5, 2.5), axes=None):
"""Plot multiple lines."""
if legend is None:
legend = []
set_figsize(figsize)
axes = axes if axes else d2l.plt.gca()
# Return True if X (tensor or list) has more than 1 dimension
def has_one_dim(X):
if isinstance(X, np.ndarray) or isinstance(X, list):
return len(X.shape) == 1
else:
return len(X.size()) == 1
# Convert Y to list so that len(Y) is always defined
if Y is None:
Y = []
if not isinstance(Y, list):
Y = [Y]
if not isinstance(X, list):
X = [X] * len(Y)
if len(X) != len(Y):
X = X * len(Y)
# Format lines and points
if fmts is None:
fmts = ['-'] * len(X)
elif isinstance(fmts, str):
fmts = [fmts] * len(X)
# Plot
for x, y, fmt in zip(X, Y, fmts):
if has_one_dim(x):
x = np.arange(len(y)) + 1
axes.plot(x, y, fmt)
axes.set_xlabel(xlabel)
axes.set_ylabel(ylabel)
axes.set_xscale(xscale)
axes.set_yscale(yscale)
if xlim:
axes.set_xlim(xlim)
if ylim:
axes.set_ylim(ylim)
if legend:
axes.legend(legend)
axes.grid()
d2l.plt.show()
def show_images(imgs, num_rows, num_cols, scale=2):
"""Plot a list of images."""
figsize = (num_cols * scale, num_rows * scale)
_, axes = d2l.plt.subplots(num_rows, num_cols, figsize=figsize)
for i in range(num_rows):
for j in range(num_cols):
axes[i][j].imshow(imgs[i * num_cols + j])
axes[i][j].axes.get_xaxis().set_visible(False)
axes[i][j].axes.get_yaxis().set_visible(False)
return axes
def plot_learning_curves(train_acc, test_acc, train_loss, test_loss):
"""Plot the learning curves."""
d2l.plt.plot(train_acc, linestyle='-', color='blue', label='train accuracy')
d2l.plt.plot(test_acc, linestyle='--', color='red', label='test accuracy')
d2l.plt.xlabel('epochs')
d2l.plt.ylabel('accuracy')
d2l.plt.legend(loc='upper right')
d2l.plt.twinx()
d2l.plt.plot(train_loss, linestyle='-', color='green', label='train loss')
d2l.plt.plot(test_loss, linestyle='--', color='orange', label='test loss')
d2l.plt.ylabel('loss')
d2l.plt.legend(loc='upper left')
d2l.plt.show()
def set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):
"""Set the axes for matplotlib."""
axes.set_xlabel(xlabel)
axes.set_ylabel(ylabel)
axes.set_xscale(xscale)
axes.set_yscale(yscale)
if xlim:
axes.set_xlim(xlim)
if ylim:
axes.set_ylim(ylim)
if legend:
axes.legend(legend)
axes.grid()