要创建一个新的Integer对象,该对象在Java 1中保存该值,以下其中一项是正确的,并且以下方法的区别到底是什么,因为所有方法都将打印该值?
方法1:
Integer p = new Integer(1);
Run Code Online (Sandbox Code Playgroud)
方法2:
Integer p = 1;
Run Code Online (Sandbox Code Playgroud)
方法3:
Integer p = new Integer("1");
Run Code Online (Sandbox Code Playgroud)
使用方法三,我得到以下警告:
Note: HelloWorld.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details
Run Code Online (Sandbox Code Playgroud) import java.util.Scanner;
public class ShortToByte{
public static void main (String args[]){
int i=0;
while (i<6){
Scanner sinput = new Scanner (System.in);
short a = sinput.nextShort();
byte b = (byte) a;
System.out.println("Short value : " + a + ",Byte value : " + b);
i++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试图理解不同数据类型之间的转换,但我很困惑 128 = -128 字节的短值如何以及短值 1000 = -24 字节的值如何?
我一直在使用以下逻辑将 Short 转换为 byte :
十进制 1000 -> 二进制 = 0000 0011 1110 1000
转换为字节时: xxxx xxxx 1110 1000 相当于: 232
我确实注意到正确的答案是二进制值的二进制补码,但是什么时候我们使用二进制补码进行转换,什么时候不使用二进制补码将 3450 从短转换为字节时我没有使用二进制补码但达到了预期的结果。 …
我正在尝试为不同类型的语句编写haskell交错器。一个这样的语句是switch语句。到目前为止,我已经完成了以下操作,但仍然遇到问题,在case表达式中,对于(_-> if length)行,我不断收到多余的模式匹配警告。如果第一个case表达式正确,则通过测试,如果不是,则测试失败。任何帮助表示赞赏,谢谢
interpret :: Program -> Memory -> Either Err Memory
interpret [] memory = Right memory
interpret (SwitchStmt var c:p) memory = let case1 = fst(c!!0)
case2 = snd(c!!0)
in do
val <- evaluate var memory
case val of case1 -> (interpret (case2++p) memory)
_ -> if length c > 1 then interpret ((SwitchStmt var (tail c)):p) memory
else interpret p memory
Run Code Online (Sandbox Code Playgroud)
我已经定义了这样的数据类型:
data Stmt = SwitchStmt{
switchVar :: Expr,
switchCase :: [(Expr,[Stmt])]
}
Run Code Online (Sandbox Code Playgroud)