我正在读《 c ++的完整指南》这本书。我认为第252页有错字。因此,我有以下三个文件。
在文件account.h中,
// account.h
// Defining the class Account. class definition (methods prototypes) is usually put in the header file
// ---------------------------------------------------
#ifndef _ACCOUNT_ // if _ACCOUNT_ is not defined
#define _ACCOUNT_
#include <iostream>
#include <string>
using namespace std;
class Account
{
private:
string name;
unsigned long nr;
double balance;
public: //Public interface:
bool init( const string&, unsigned long, double);
void display();
};
#endif
// _ACCOUNT_
Run Code Online (Sandbox Code Playgroud)
在account.cpp文件中,
// account.cpp
// Defines methods init() and display().
// ---------------------------------------------------
#include …Run Code Online (Sandbox Code Playgroud) 让我们看一下这个例子:
String var;
while (...) {
var = ...
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
在本例中,我们创建对对象的引用String,并在循环的每次迭代中为其分配不同的对象。
现在,在另一个例子中:
while (...) {
String var = ...
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
如果我们假设编译器很幼稚,它只会在每次迭代时String分配对堆栈上对象的引用。
或者会吗?这就是我的问题 - (a?) Java 编译器是否执行此优化?我总是将对象声明保留在尽可能广泛的范围内,因为我担心这一点,但如果编译器已经这样做了,那么我的鞋子上就少了一颗鹅卵石。
先感谢您!
下面的程序:
public class ProcessExample {
public static void main(String[] args) throws IOException {
Process process1 = new ProcessBuilder("/bin/ls", "-l").directory(Path.of("/home/yapkm01").toFile()).start();
System.out.println("ls command:");
try (var in = new Scanner(process1.getInputStream())) {
while (in.hasNextLine()) {
System.out.println(in.nextLine());
}
}
Process process2 = new ProcessBuilder("/bin/java", "-version").start();
System.out.println("java command:");
try (var in = new Scanner(process2.getInputStream())) {
while (in.hasNextLine()) {
System.out.println(in.nextLine());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
ls command:
total 424
drwxr-xr-x 2 yapkm01 yapkm01 4096 Dec 27 2021 Desktop
drwxr-xr-x 2 yapkm01 yapkm01 4096 Apr 30 01:09 Documents …Run Code Online (Sandbox Code Playgroud) public void foo()
{
assert false; /* Line 5 */
assert true; /* Line 6 */
}
Run Code Online (Sandbox Code Playgroud)
断言在这做什么?我熟悉断言陈述
int *ab = (int *)5656;
cout << *ab; //Here appcrash.
int *ab;
*ab = 5656;
cout << *ab; //These block crashes the app too.
Run Code Online (Sandbox Code Playgroud)
但是如果我写这个,我可以得到指针内容的十六进制值:
int *ab = (int *)5656;
cout << ab; //Output is hex value of 5656.
Run Code Online (Sandbox Code Playgroud)
所以我想问:*是一个带有指针(?)内容的运算符,但为什么在这个(这些)示例应用程序崩溃?
如果我将代码更改为此,我可以使用该运算符:
int a = 5656;
int *aptr = &a;
cout << *aptr; //No crash.
Run Code Online (Sandbox Code Playgroud)
为什么解除引用运算符(*)带来了char的唯一第一个字符:
char *cptr = "this is a test";
cout << *cptr; // Here output = 't'
cout << cptr; // Here output = 'this is a …Run Code Online (Sandbox Code Playgroud) 实际上DNS将如何基于不同的OSI层工作?我的意思是当我们输入google.com时,它会用其域名映射google.com的IP地址.但是当我们不知道权威DNS服务器的IP地址时,它将如何移动到不同的OSI层(TL) ,NL,DLL)我们的系统到权威DNS服务器?
当我尝试运行Java类时,出现以下错误消息,但在JDK 1.7中同样可以正常工作。
我刚安装完JDK 8。如何解决这个问题呢?
Some related files cannot be found :
java.lang.NoClassDefFoundError: sunw/io/Serializable
Run Code Online (Sandbox Code Playgroud)
我正在使用加载我的课程文件
Class inv = Class.forName(ClassName);
Run Code Online (Sandbox Code Playgroud)
例外:
Caused by: java.lang.ClassNotFoundException: sunw.io.Serializable
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Run Code Online (Sandbox Code Playgroud) 运行以下代码后,输出打印:false.所以我假设i1==i2之前已经过评估b1 = i1.但是顺序是不是从左到右?
public static void main(String[] args) {
boolean b1;
int i1 = 2;
int i2 = 3;
if(b1 = i1==i2) {
System.out.println("true");
} else {
System.out.println("false");
}
}
Run Code Online (Sandbox Code Playgroud) 嘿,我在学校上编程课,但老师没有很好地解释,所以我们必须在网上寻找我所做的信息,但我无法在我的代码中找到错误,你能帮我吗?
char end='s';
do{
System.out.println("Tipo de boleto");
char boleto = (char) System.in.read();
switch (boleto){
case 'a':
System.out.println("El boleto cuesta $120.00");
System.out.println("Otro boleto (s/n)?");
end = (char) Integer.parseInt(entrada.readLine());
continue;
case 'n':
System.out.println("El boleto cuesta $75.00");
System.out.println("Otro boleto (s/n)?");
end = (char) Integer.parseInt(entrada.readLine());
continue;
case 'i':
System.out.println("El boleto cuesta $60.00");
System.out.println("Otro boleto (s/n)?");
end = (char) Integer.parseInt(entrada.readLine());;
continue;
default:
System.out.println("Error" );
break;
}
}
while (end == 'n');
Run Code Online (Sandbox Code Playgroud)
例外
run: Tipo de boleto a El boleto cuesta $120.00 Otro boleto (s/n)?
Exception …Run Code Online (Sandbox Code Playgroud)