为什么以下打印老板而不是低音?
String boss = "boss";
char[] array = boss.toCharArray();
for(char c : array)
{
if (c== 'o')
c = 'a';
}
System.out.println(new String(array)); //How come this does NOT print out bass?It prints boss.
Run Code Online (Sandbox Code Playgroud) 你能解释一下最近2份印刷品中的内容吗?那就是我迷路的地方.
public class Something
{
public static void main(String[] args){
char whatever = '\u0041';
System.out.println( '\u0041'); //prints A as expected
System.out.println(++whatever); //prints B as expected
System.out.println('\u0041' + 1); //prints 66 I understand the unicode of 1 adds up the
//unicode representing 66 but why am I even returning an integer when in the previous statement I returned a char?
System.out.println('\u0041' + 'A'); //prints 130 I just wanted to show that adding an
//integer to the unicode in the previous print …
Run Code Online (Sandbox Code Playgroud) 所以我刚刚学会了声明Object类型的变量(即Object a;
)时,为该变量分配了32位空间.在这个变量/引用中,有一个实际Object的内存地址.
现在让我们假装我有足够的内存来做这件事.
如果我创建了超过4,294,967,296(2 32)个Object类型的变量并尝试将它们分配给不同的对象会发生什么?由于整数溢出,某些变量/引用是否会获得相同的内存地址?这意味着在内存中引用超过4,294,967,296个对象是不可能的?
首先,让我明确一下声明类型的含义。假设 SuperBoss 是 Boss 类的超类。
SuperBoss mrBond = new Boss();
Run Code Online (Sandbox Code Playgroud)
SuperBoss 是声明的类型,Boss 是实际的类型。
就我个人而言,我认为由于以下运行时异常,声明的类型在运行时发生了更改:
SuperBoss mrWayne = new SuperBoss();
((Boss)mrWayne).randomMethod();
//Exception: java.lang.ClassCastException: SuperBoss cannot be cast to Boss
Run Code Online (Sandbox Code Playgroud)
我知道这可能看起来微不足道,但我将在下个季度进行辅导,我不想教学生错误的东西。本季度我的教授和她的助手在这个问题上意见不一致。我的教授认为,强制转换确实完全改变了单个语句在运行时声明的类型。助教坚信,在运行时,仅检查强制转换,但实际上并没有更改声明的类型。
为什么以下作业不起作用?如果可能的话,我想要一个低级的解释。另外,这是我得到的编译器错误:'char*' 到 'char [20]' 赋值中的类型不兼容
class UCSDStudent {
char name[20];
public:
UCSDStudent( char name[] ) {
//this-> name = name; does not work! Please explain why not
strcopy( this -> copy, copy ); //works
}
};
Run Code Online (Sandbox Code Playgroud) int x[][] = {{1, 2}, {3, 4}};
Run Code Online (Sandbox Code Playgroud)
由于数组是对象,而二维数组是数组数组,因此这段代码中有多少个对象?
请解释为什么我使用++运算符获得segfault.显式添加1和使用++运算符有什么区别?
using namespace std;
#include <iostream>
int main() {
char* p = (char*) "hello";
cout << ++(*p) << endl; //segfault
cout << 1 + (*p) << endl; // prints 105 because 1 + 'h' = 105
}
Run Code Online (Sandbox Code Playgroud) public class Date
{
static int month;
public static void setMonth(int x)
{
this.month = x; //compiler error
}
public static int getMonth()
{
return month; //compiles just fine, no error
}
}
Run Code Online (Sandbox Code Playgroud)
编译时,我收到错误:非静态变量,这不能从静态上下文引用.但是,如果我删除"这个".没有错误.我不明白为什么当我使用关键字static清楚地声明它时,月份是非静态变量.
例如:
using namespace std;
#include <iostream>
void funcOne() {
}
void funcTwo( int x ) {
}
int main() {
void (*ptrOne)() = funcOne;
cout << ptrOne << endl; //prints 1
void (*ptrTwo)( int x ) = funcTwo;
cout << ptrTwo << endl; //prints 1
int (*ptrMain)() = main;
cout << ptrMain << endl; //prints 1
}
Run Code Online (Sandbox Code Playgroud)
有谁知道这背后的原因?起初我以为这是因为函数不存在于内存中,因为我从不调用它们,因此它们永远不会被添加到堆栈中.但即使是指向main函数的指针的值也会输出1.
更明确一点,当我在使用()创建对象时尝试访问实例变量时出现编译时错误,但是当我不这样做时,代码将按预期编译并运行.此外,此问题仅适用于默认构造函数.我想了解原因.
using namespace std;
#include <iostream>
class Student {
public:
int gpa;
Student() {
gpa = 4;
}
Student( int x ) {
gpa = x;
}
};
int main() {
Student zero;
Student sally( 2 );
Student jack();
cout << zero.gpa << endl; //prints 4
cout << sally.gpa << endl; // prints 2
cout << jack.gpa << endl; //error: request for member 'gpa' in 'jack', which is of non-class type 'Student()'
}
Run Code Online (Sandbox Code Playgroud) 我认为没有区别,但后来我遇到了这个:
public class Whatever
{
String toString()
{
//stuff
}
}
Run Code Online (Sandbox Code Playgroud)
此代码导致编译器错误:
toString()
在Whatever
不能覆盖toString()
在java.lang.Object
; 试图分配较弱的访问权限; 是public
如果我明确键入public String toString()
代码编译就好了.
我很困惑,因为我认为这是指当前调用该方法的对象.
那么为什么在调用继承方法时,我的对象中的实例变量x没有被更改?超类:
public class SuperBoss
{
int x = 50;
public void changeX()
{
this.x = 20;
}
}
Run Code Online (Sandbox Code Playgroud)
子类:
public class Boss extends SuperBoss
{
int x = 10;
public static void main(String[] args)
{
Boss b = new Boss();
b.changeX();
System.out.println(b.x); //prints 10
}
}
Run Code Online (Sandbox Code Playgroud)
为什么打印10而不是20?