小编chr*_*ke-的帖子

所有不可变对象都可以重用吗?

从有效的Java书中可以看出," 如果一个对象是不可变的,那么它总是可以被重用 ".

String s = "shane";
String p = "shane";
Run Code Online (Sandbox Code Playgroud)

此版本使用a single String instance,而不是每次执行时都创建一个新版本.此外,保证对象将被在同一虚拟机中运行的任何其他代码重用,这些代码恰好包含相同的字符串文字.

下面的最终课程也是不可改变的呢?点对象可以重复使用吗?

public final class Point {
  private final int x, y;

  public Point(int x, int y) {
   this.x = x;
   this.y = y;
  }

  public int getX() { return x; }
  public int getY() { return y;
 }
Run Code Online (Sandbox Code Playgroud)

任何人都可以提供一个上面的例子immutable class,它的对象/实例可以重复使用吗?我只是对可重用性如何发生感到困惑?

我能够StringIntegerClasses 相关联,但不能与用户定义的类相关联.

java oop immutability value-objects

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

为什么在Python中打印需要三个撇号?

我在Python 3.3.2中制作了这个毕达哥拉斯定理计算器.

我在几行上打印,以便我可以制作一个图表:

print("Welcome to the Pythagoras Theorem Calculator, powered by Python!")
print("Below are the values a, b and c. You will need to input these values after.")
print('''
      | .
      |   .
      |     .
side a|       . side c
      |         .
      |           .
      |_____________.
          side b
      ''')
Run Code Online (Sandbox Code Playgroud)

如上所示,需要三个撇号而不是语音标记.为什么会这样?这是逃脱角色吗?(我尝试在Google上搜索:http://bit.ly/15a4zes)

python

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

如何设置多个int等于相同的数字?Java的

我有代码int index, r, g, b;,我想将它们都设置为等于某个数字,我不想单独写index = 0; r = 0;...等等.

我怎么在java中这样做? index,r,g,b = 0;不适合我.

java initialization

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

了解内存映射文件

我已经开始阅读有关内存映射IO的内容,而且我在理解这些概念时遇到了一些困难

这是我到目前为止所理解的:

每个进程都有一个虚拟地址空间.内存映射文件在虚拟地址空间中分配一个特定的地址范围,映射到物理内存上的相同地址.这样,磁盘控制器在
内存上完成的所有写操作(通过DMA)都将反映到进程中,无需任何额外的复制.(在非内存映射文件的情况下,CPU必须将内容复制到进程的缓冲区).

我怀疑:

  • 我的理解是否正确?

  • 如果有多个进程试图对文件进行mmap并且没有可用于直接映射的连续内存块,会发生什么?

io operating-system mmap virtual-memory dma

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

Springboot依赖项:找到两个依赖项并且忽略了限定符

这是我的课:

@Repository
@RequiredArgsConstructor
@Slf4j
public class ServeiTerritorialCatalegsClientRepositoryImpl implements ServeiTerritorialCatalegsClientRepository {

    @Qualifier("catalegsMarshaller") private final Marshaller marshaller;

    //...
}
Run Code Online (Sandbox Code Playgroud)

我的 bean 定义是:

@Bean
public Marshaller oidMarshaller() throws JAXBException {
   //...
}

@Bean
public Marshaller catalegsMarshaller() throws JAXBException {
  //...
}
Run Code Online (Sandbox Code Playgroud)

我收到这条消息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 3 of constructor in cat.catsalut.hes.mpi.hazelcast.loader.repository.ServeiTerritorialCatalegsClientRepositoryImpl required a single bean, but 2 were found:
    - oidMarshaller: defined by method 'oidMarshaller' in class path resource [cat/catsalut/hes/mpi/hazelcast/loader/configuration/ServeiTerritorialConfiguration.class]
    - catalegsMarshaller: defined by method 'catalegsMarshaller' in class path …
Run Code Online (Sandbox Code Playgroud)

java spring lombok spring-boot

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

为什么Java在运行时需要classpath

在Java中,我们在编译时设置类路径来编译Java文件,但为什么我们需要在运行时设置类路径?JVM需要类路径运行.class文件的任何具体原因?

java jvm classpath

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

Array.empty == Array()给出false但两者看起来都是类型和值相等的?

在审查代码更改时,我发现Array.empty并不是Array()说我会考虑更多的惯用语.然后我惊讶地发现==他们给出false了他们看起来相同的类型和价值方面.

scala> Array.empty == Array()
res1: Boolean = false

scala> Array.empty
res2: Array[Nothing] = Array()

scala> Array()
res3: Array[Nothing] = Array()
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么会这样?

scala

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

为什么我的自绑定泛型类型与方法调用不匹配?

我正在为如下所示的命令外壳编写代码:

interface Context<C extends Context<C>> {}

interface RecursiveContext<C extends RecursiveContext<C>> extends Context<C> {
  Shell<C> getShell();
  default Result execute(Command cmd) { return getShell().execute(cmd, this); }
}

interface Shell<C extends Context<C>> {
  C newContext();
  Result execute(Command cmd, C context);
}
Run Code Online (Sandbox Code Playgroud)

我在默认方法中遇到错误说

The method execute(Command, C) in the type Shell<C> is not applicable for the arguments (Command, RecursiveContext<C>)
Run Code Online (Sandbox Code Playgroud)

我希望这能奏效,因为Shell<C> getShell()保证能够C在其execute调用中接受并且因为this实际上保证是相同自绑定类型的子类型C,但编译器似乎不同意我的观点。哪里不匹配,在默认方法中执行强制转换是否安全?如果这不安全,你能提供一个类型不匹配的反例吗?

我也试过在 shell 中引入一个中间类型<D extends C> execute(Command, D),但这似乎没有改变任何东西。

(大多数建议的问题都涉及原始类型的中间步骤,但我认为我没有错过。)

java generics

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

使用固定宽度的列格式化文本表

我有一个包含商店信息的文件,我想将其排序为另一个文件,例如表格(在Excel中)。

文件 :

001     Tablets                        5      3
002     pens                           4      1
005     Computeres                     3      0
003     Bages                          2      1
004     USB                            4      0
Run Code Online (Sandbox Code Playgroud)

我写这段代码:

import java.util.*;
import java.io.*;

public class Sort {
public static void main(String [] args) throws IOException {
    FileInputStream fileinput = new FileInputStream("input.txt");
    FileOutputStream fileoutput = new FileOutputStream("output.txt");
    Scanner infile = new Scanner(fileinput);
    PrintWriter pw = new PrintWriter(fileoutput);
    int id, quantity, soldQuantity;
    String title;
    pw.println("ID\tTitle\t\t\tQuantity\tSoldQuantity");
    pw.println("");
    while(infile.hasNext()){
        id = infile.nextInt();
        title = infile.next();
        quantity = infile.nextInt();
        soldQuantity …
Run Code Online (Sandbox Code Playgroud)

java file

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

我可以在我的 java 应用程序上指定 config.yml 的默认值吗?

我目前正在使用 dropwizard 处理我的 java 应用程序。

它工作正常,我运行它并发送完整的 yml 文件或参数。

例如,其中之一是:

reporting:
  enabled: false
Run Code Online (Sandbox Code Playgroud)

所以,我想做的就是将此 yml 文件设置为默认值,并在需要时(用于将来的功能)发送此值var

所以我想做这样的事情

reporting:
  enabled: ${REPORTING_FLAG:false}
Run Code Online (Sandbox Code Playgroud)

然后我可以将 REPORTING_FLAG 作为环境参数(docker)发送,并且应该可以正常工作......

问题是我发现我的应用程序无法识别该模型。

我有什么办法可以做到这一点吗?是否需要考虑额外的配置?我之前用 Spring 应用程序做过,但这似乎有所不同。

现在我收到一个错误,预期的布尔值无效(将整行作为字符串)

有想法吗?

java yaml dropwizard

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