java进制转换代码,要求以补码形式输出,有不同的类型,按下回车输出结果,切记不光是十进制转换成其他进制

博客分类:
Java数据类型划分:
Literals 字面量:
字面量表示一个固定的、不可变的值。Java中有如下字面量:
字面量说明类型Integer Literals可表示十进制、八进制(0开头,如012)、十六进制(0x开头,如oxA)、二进制整数(Java 7新加,0b开头,如0b11010)intFloating-Point Literals可表示十进制、十六进制(0x开头,如0x1.8P3)浮点数doubleBoolean Literalstrue & false,用来表示boolean类型的值booleanCharacter Literals由单引号 ' 包围,用来表示单个字符(不能是单引号',因为该literal由'包围;不能是反斜杠\,因为\用来表示转义字符)或转义字符charString Literals由双引号 " 包围,由&=0个字符(不能是双引号",因为该literal由"包围;不能是反斜杠\,因为\用来表示转义字符)组成,这些字符可以是转义字符StringThe Null Literalnull,用来表示null类型的值,代表了空引用(null reference)null说明:引用一 Java 7 中 新加了在Numeric Literals(Integer Literals & Floating-Point Literals)中使用下划线 _ 的特性。
二 关于转义字符:
转义字符以反斜杠 \ 开头。需要转义字符的理由:
1 “控制字符”我们无法用键盘打出来,比如 backspace 退格符|horizontal tab 水平制表符|linefeed 换行符|formfeed 换页符|carriage return 回车符。换行/回车/换页都来自打印机的命令。换页就不说了,换行和回车的区别是:换行是将光标移到当前列的下一行,回车是将光标移到当前行的行首。
2 很多字符很生僻,如 ? (unicode编码0108)
3 单引号 ' 在Character Literals中、双引号 " 在String Literals中都是有特殊含义的,所以想在Character Literals中使用 '、在String Literals中使用 ",就必须转义使用。
4 基于以上理由,我们需要转义字符,并做了规定转义字符以反斜杠 \ 开头,那么 \ 也就变成特殊字符了,因为它是用来界定转义字符的,所以, \ 也需要转义。
Java中有两种转义字符:
1 Unicode Escapes
格式: \u + 4位十六进制数字
可以用来转义Java代码中任何的字符。如下例子:
String \u0061 = \u0022012"; //字母a的unicode为0061,双引号的unicode为0022;这里相当于 a = "012"
S\u0079stem.out.println(\u0061); //输出 012;字母y的unicode为0079
String s = nul\u006c; //字母l的unicode为006c,相当于 s =
if(tru\u0065) \u007bSystem.out.println(s)\u003b }
//输出null;字母e的unicode为0065,{的unicode为007b,半角分号的unicode为003b. 由结果我们可以看得出来,Java对Unicode Escapes的处理是对代码中的所有Unicode Escapes做全盘的替换,并且这种替换是发生在编译前的。这就导致了控制字符中的换行和回车对应的Unicode Escopes不能用在Character & String Literals中,如String someString = "foo\u000abar";在编译前会被替换成
String someString = "foo
bar";从而导致编译无法通过。
&&& 引用这个链接里的这个例子太有趣了:public class FalseIsTrue {
public static void main(String[] args) {
if ( false == true ) { //these characters are magic: \u000a\u007d\u007b
System.out.println("false is true!");
}输出为 false is true!结合Unicode Escapes,你能看出为什么吗?&&&
另外使用Unicode Escapes注意以下三个问题:
&&&&&&& ① \u 中的u可以写多个
&&&&&&& ② \u 前还有 \ 的话:若其前\的数目为偶数,才会发生Unicode Escapes;若其前\的数目为奇数,则不会发生Unicode Escapes。如 “\\u0027=\u0027”经过Unicode Escapes后的结果为“\\u0027='”(这个结果里有两个\,如果是String Literals的一部分,会再发生一次Escape Sequences,最终输出结果变成“\u0027='”)。
&&&&&&& ③ Unicode escapes产生的字符不会再去参与更进一步的Unicode escapes。如“\u005cu005c”经过Unicode Escapes后的结果为“\u005c”(这种只有一个\的结果很明显无法作为String Literals的一部分),而不是“\”。
2 Escape Sequences for Character and String Literals
首先要说的是,除了上面说的换行和回车的Unicode Escopes之外,这种转义是可以完全用Unicode Escapes替代的;其次,这种转义只可以用在Character Literals和String Literals中。个人认为,这种转义的存在,一方面弥补Unicode Escopes的不足,另一方面是为了使用上和代码书写上的方便。具体这几种转义如下(结合加粗记忆):Escape Sequence表示的字符对应的Unicode Escapes\bbackspace 退格符 BS\u0008\thorizontal tab 水平制表符 HT\u0009\nlinefeed/newline 换行符 LF\u000a\fformfeed 换页符 FF\u000c\rcarriage return 回车符 CR\u000d\"双引号 "\u0022\'单引号 '\u0027\\反斜杠 \\u005c\[0-3]?[0-7]?[0-7]八进制转义 Octal escapes,表示Unicode中0000-00ff的字符\u0000-&\u00ff (即000oct-&377oct)Java中提供八进制转义只是为了和C的兼容(Octal escapes are provided for compatibility with C, but can express only Unicode values \u0000 through \u00FF),代码中用不用吧
三 关于null类型:
引用除了基本类型和引用类型,There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.
Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type.
The null reference is the only possible value of an expression of null type.
The null reference can always undergo a widening reference conversion to any reference type.
In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.示例代码:
//integer literal
System.out.println(012);
System.out.println(0xA);
System.out.println();
//Floating-Point literal
System.out.println(0x1.8P3); //1.8hex × 2^3 = 1.5decimal × 8 = 12
System.out.println();
//boolean literal
System.out.println(true);
System.out.println(false);
System.out.println();
* 005c 反斜杠 \ 的unocode编码
* 0027 单引号 ' 的unocode编码
* 0022 双引号 " 的unocode编码
//character literal
System.out.println('\\');
System.out.println('\u005c\');
//System.out.println('\\u005c'); //不行,因为转的是反斜杠\,最终结果是 \u005c,不能赋给只能容纳一个字符的character literal
System.out.println('\u005c\u005c');
System.out.println(\uc\u005c\u0027);
System.out.println('\'');
System.out.println('\u005c'');
//System.out.println('\\u0027'); //不行,因为转的是反斜杠\,最终结果是 \u0027,不能赋给只能容纳一个字符的character literal
System.out.println('\u005c\u0027');
System.out.println(\uc\u);
System.out.println('"'); //character literal中使用双引号不用转义
System.out.println();
//String literal
System.out.println("S\\B");
System.out.println("S\u005c\B");
System.out.println("S\\u005cB"); //转的是反斜杠\,所以最终结果是 S\u005cB
System.out.println("S\u005c\u005cB");
System.out.println(\u0022S\u005c\u005cB\u0022);
System.out.println("S\"B");
System.out.println("S\u005c"B");
System.out.println("S\\u0022B"); //转的是反斜杠\,所以最终结果是 S\u0022B
System.out.println("S\u005c\u0022B");
System.out.println(\u0022S\u005c\u0022B\u0022);
System.out.println("'"); //String literal中使用单引号不用转义
System.out.println();
//null literal
Object o =
System.out.println(o);上述字面量统称 Lexical Literals ,另外还有 Class literal,参见:
基本数据类型占用字节数、表数范围、包装类(Wrapper Class):
补码:two's complement
类型说明占用存储空间表数范围包装类byte用补码表示的有符号整数1 bytes-2^7 ~ 2^7-1 (-128 ~ 127)Byte short用补码表示的有符号整数2 bytes-2^15 ~ 2^15-1 (-32,768 ~ 32,767)Short charUnicode字符,或无符号整数2 bytes\u0000 ~ \u00ff ; 0 ~ 2^16-1(65,535)Characterint用补码表示的有符号整数4 bytes-2^31 ~ 2^31-1Integer long用补码表示的有符号整数8 bytes-2^63 ~ 2^63-1Long float遵循IEEE 754单精度浮点数标准4 bytes-.403E38 ~ 3.403E38Floatdouble遵循IEEE 754双精度浮点数标准8 bytes-1.798E308 ~ 1.798E308Doubleboolean1 bytes(only 1 bit used)Boolean 重要点说明:
一 boolean 类型的值只能是Boolean Literals定的true或false,不可以用0或非0的整数替代true和false,这点和C语言不同;
二 字节byte是系统分配内存的最小单位。java中的boolean是分配一个字节,但是只有一个bit有用,其他无效位置0;
三 切记java中Integer Literals默认为int类型,Floating-Point Literals默认为double类型:int i1 = 600; //正确
long l1 = L; //必须加l否则会出错
double d = 12345.6; //正确
float f = 12.3F; //必须加f否则会出错
四 java中没有 无符号数 这一说。
五 除boolean外,java中可以从任意基本类型转型到另外的基本类型;boolean类型不可以转换为其他的基本数据类型。“基本数字类型”具体转换规则见下面。
基本数字类型及其包装类的转换规则
一 Java中基本数字类型之间存在如下隐式转换引用5.1.2. Widening Primitive Conversion
byte -& short, int, long, float, or double
short -& int, long, float, or double
char -& int, long, float, or double
int -& long, float, or double
long -& float or double
float -& double这里需要注意的是,byte-&char不存在隐式转换,原因很简单:char的表数范围不能完全覆盖byte的表数范围。
二 基本数字类型变量及其包装类对象都可以参与算术运算。在算数运算中它们遵从如下转换规则:引用一 一元运算中存在 Unary Numeric Promotion 5.6.1:
1 如果操作数是Byte, Short, Character, or Integer,会先自动拆箱,再隐式转换为int类型
2 如果操作数是Long, Float, or Double,自动拆箱为相对应的基本类型
3 如果操作数是byte, short, or char类型,隐式转换为 int 类型。
以下面代码为例,第二句编译无法通过,原因是使用一元运算做 +b 操作后的结果为int,无法赋给byte类型变量:
// assign byte value
byte b1 = +b; // result int but required byte. Compiler error
二 二元运算中存在 Binary Numeric Promotion& JLS 5.6.2:
1 如果操作数为基本数字类型的包装类对象,则首先对其做自动拆箱
2 ① 如果其中一个操作数为double类型,则另外一个隐式转换为double类型,再进行运算
&& ② 否则,如果其中一个操作数为fllat类型,则另外一个也隐式转换为float类型,再进行运算
&& ③ 否则,如果其中一个操作数为long类型,则另外一个也隐式转换为long类型,再进行运算
&& ④ 否则,两个操作数都隐式转换为int类型,再进行运算。
这里需要注意的是,byte,short,char在算数运算中不会互相转换,他们三者在运算前时都隐式转换为int类型。
三 为变量赋值时( = 、+=等),需要注意的赋值转换规则:
1 首先会存在隐式的自动拆箱与基本数字类型的隐式转换,如:
float f = 4; //隐式转换
double ddd = new Integer(1);
//先自动拆箱,后隐式转换
2 如果被用来为变量赋值的表达式是类型为byte, short, char或int的常量表达式,则在该常量表达式的值不超出变量表数范围的前提下,可以将其直接赋给该变量,变量的类型可以是byte, short或者char及其包装类中的任何一种。这里涉及到几个概念:引用5.2. Assignment Conversion
&&& 1 constant variables:做了初始化的基本类型或String类型的final变量,称为常变量(JLS 4.12.4.)
&&& 2 Constant Expressions:由基本类型的字面量、String字面量、常变量、向基本类型及String的强制转换(Cast,即(primitive type) or (String))、加减乘除等乱七八糟一二三元运算符组成的表达式就是常量表达式(15.28. Constant Expressions)
&&& 3 Narrowing Primitive Conversion:如下几个:引用short -& byte or char
char -& byte or short
int -& byte, short, or char
long -& byte, short, char, or int
float -& byte, short, char, int, or long
double -& byte, short, char, int, long, or float
int nfi = 0; //用来赋值的表达式是常量表达式
//short test1 = //不可以,nfi不是常量表达式
final int fi = 2; //用来赋值的表达式是常量表达式
short test2 =
//可以,因为 fi 是类型为int的常量表达式
byte b = 1; //用来赋值的表达式是常量表达式
//char cb1 = //不可以
char cb2 = (byte)1; //用来赋值的表达式是常量表达式
//char cb3 = (byte)(1+b);
Character wc = (byte)1; //用来赋值的表达式是常量表达式
short s = 1; //用来赋值的表达式是常量表达式
//byte bs1 = //不可以
byte bs2 = (short)1; //用来赋值的表达式是常量表达式
//byte bs3 = (short)(1+s); // 不可以
Byte wb = (short)1; //用来赋值的表达式是常量表达式
//char cs1 = //不可以
char cs2 = (short)1; //用来赋值的表达式是常量表达式
//char cs3 = (short)(1+s); //不可以
char c = 1; //用来赋值的表达式是常量表达式
//byte bc1 =
byte bc2 = (char)1; //用来赋值的表达式是常量表达式
//byte bc3 = (char)(1+c); //不可以
int a = 4; //用来赋值的表达式是常量表达式
short sss = (int)(4+3); //可以,用来赋值的表达式是常量表达式
//short sss = (int)(a+3); //不可以
四 试图将一个超出了int表数范围的Integer Literals赋给long/float/double及其包装类时,必须在该Integer Literals后加 L|F|D (大小写均可)。注意为Integer Literals和Floating-Point Literals加类型后缀 l|f|d|L|F|D 不是强制类型转换!而是告诉编译器该字面量的类型!建议使用小写表示这三个类型后缀。
提醒自己附件里以前关于这块知识点的东西记得也时不时看看。
Primitive Data Types's Autoboxing & Unboxing(基本数据类型的自动装箱和自动拆箱):
基本数据类型的包装类(Wrapper Class)见本页上面的表格。
Java Tutorials - Autoboxing and Unboxing:
引用Converting a primitive-type value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing whenever a primitive-type value is passed as a parameter to a method that expects an object of the corresponding wrapper class.
Converting an object of a wrapper type (Integer) to its corresponding primitive-type (int) value is called unboxing. The Java compiler applies unboxing when an object of a wrapper class is:
1 Passed as a parameter to a method that expects a value of the corresponding primitive type.
2 Assigned to a variable of the corresponding primitive type.
public class AutoboxUnboxTest {
public static void main(String[] args) {
Integer a = 3; //autoboxing
int b = //unboxing
List&Integer& list = new ArrayList&Integer&();
list.add(1);
//autoboxing。list只可以放对象,可是这里放入的是基本类型int类型的值1,为什么没报错?自动装箱的原因。
int i = list.get(0); //unboxing 。list中取出的是Integer,直接赋给基本类型int变量i,为什么没报错?自动拆箱的原因。
}要点:
1 Java编译器对自动装箱的处理是通过调用基本类型对应包装类的静态方法valueOf()来做的;对自动拆箱的处理是通过调用包装类的xxxValue()来做的。以int-Integer为例,通过javap可以查看这个过程:引用
public class AutoboxingTest {
public static void main(String []args) {
Integer a = 3;
}Compiled from "AutoboxingTest.java"
public class AutoboxingTest extends java.lang.Object{
public AutoboxingTest();
invokespecial
#1; //Method java/lang/Object."&init&":()V
public static void main(java.lang.String[]);
invokestatic
#2; //Method java/lang/Integer.valueOf:(I)Ljava/lang/I
invokevirtual
#3; //Method java/lang/Integer.intValue:()I
}
2 性能的需要,Java对包装类 Byte/Short/Integer/Long 表数范围为整型-128到127的实例对象、Character表数范围为0到128的实例对象进行了缓存,并在这些包装类的静态方法valueOf()中,当传入的整型参数在这个范围内时,使用缓存里的对象。包装类Boolean有两个Boolean类型的static final成员变量分别为TRUE和FALSE,并在当你调用valueOf()时,返回的是这两个变量的其中之一。
3
因为以上原因,当你通过自动装箱,而不是new的方式创建包装类实例时,就要格外小心:如果被装箱的是true,false,byte,范围为0到128的char,范围为-128到127的short、int、long时,装箱的结果将永远是返回同一个包装类对象!
引用If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
Byte byte1 = 127;
Byte byte2 = 127;
System.out.println(byte1 == byte2); // true
Short s1 = 127;
Short s2 = 127;
System.out.println(s1 == s2); // true
Short s3 = 128;
Short s4 = 128;
System.out.println(s3 == s4); // false
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1 == i2); // true
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i3 == i4); // false
Long l1 = (long)127;
Long l2 = (long)127;
System.out.println(l1 == l2); // true
Long l3 = (long)128;
Long l4 = (long)128;
System.out.println(l3 == l4); // false
Character c1 = 127;
Character c2 = 127;
System.out.println(c1 == c2); // true
Character c3 = 128;
Character c4 = 128;
System.out.println(c3 == c4); // false
Boolean b1 =
Boolean b2 =
System.out.println(b1 == b2); //
Boolean b3 = new Boolean(false);
Boolean b4 = new Boolean(false);
System.out.println(b3 == b4); // false
Upcasting & Downcasting(向上转型 & 向下转型):
Upcasting:即父类引用指向子类对象,不需要强制类型转换。向上转型会丢失子类特有的方法 -鸟是动物
Downcasting:将指向子类对象的父类引用赋给子类引用,需要强制类型转换 -但是动物不一定是鸟,只有原来是鸟的动物才能转成鸟。
In Java, upcasting is automatic but downcasting must be explicit.
Suppose we have a object birdInstance of class Bird with superclass Animal:Bird birdInstance = new Bird();
Upcasting: treating birdInstance as a Animal:Animal animal = birdI
Downcasting:treating animal(must actually a Bird) as a Bird:Bird bird = (Bird)完整实例代码:
public class UpcastDowncastTest {
public static void main(String[] args) {
Bird birdInstance = new Bird();
//upcasting
Animal animal = birdI //鸟是动物
animal.eat();
//animal.fly(); //无法通过编译,说明upcasting丢失了子类Bird特有的方法fly()
//downcasting
Bird bird = (Bird) //这里的animal就是个鸟,所以可以强制转换成鸟
bird.eat();
bird.fly();
* 错误的向下转型认知,编译可通过,运行时会报ClassCastException
* new出来的的本来就不是鸟而是动物,硬要让它转成鸟肯定不行
Bird bird2 = (Bird)new Animal(); //ClassCastException:
class Animal {
public void eat() {
System.out.println("Animal eating...");
class Bird extends Animal {
public void eat() {
System.out.println("Bird eating...");
public void fly() {
System.out.println("Bird flying..");
下载次数: 1
浏览: 696537 次
来自: 北京
LZ你好, 用了这个方法后子页面里的JS方法就全不能用了呀
学习了,真不错,做个记号啊
我一台老机器,myeclipse9 + FB3.5 可以正常使 ...
触发时间在将来的某个时间 但是第一次触发的时间超出了失效时间, ...
找了好久,顶你
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'数制的转换(二进制、八进制、十进制、十六进制)
由于计算机内只有高低电平,只能代表0和1两种状态,就此产生了二进制,而人们习惯的数字是十进制。所以就存在十进制与二进制之间的转换,但是由于二进制表示数据起来不方面(特别是当数值比较大时),十进制转换为二进制又比较麻烦,就产生了八进制、十六进制
进制对照表
2进制与八进制、十六进制转换十分容易
每个进制都是的每一位都是有位权的vcD4KPHA+yOejujEwMDGjqLb+vfjWxqOpICAgICAgMl4zKjEgJiM0MzsgMl4yKjAgJiM0MzsgMl4xKjAgJiM0MzsgMl4wKjEgPSA5PC9wPgo8cD4gICAgMTAwMaOosMu9+NbGo6kgICAgICA4XjMqMSAmIzQzOyA4XjIqMCAmIzQzOyA4XjEqMCAmIzQzOyA4XjAqMSA9IDUxMzwvcD4KPHA+ICAgIDEwMDGjqMquvfjWxqOpICAgICAgMTBeMyoxICYjNDM7IDEwXjIqMCAmIzQzOyAxMF4xKjAgJiM0MzsgMTBeMCoxID0gMTAwMTwvcD4KPHA+ICAgIDEwMDGjqMquwfm9+NbGo6kgICAgMTZeMyoxICYjNDM7IDE2XjIqMCAmIzQzOyAxNl4xKjAgJiM0MzsgMTZeMCoxID0gNDA5NzwvcD4KPHA+0tTJz7XEtrzKx7u7y+O1vcquvfjWxrXEvMbL473hufuho7/JvPvNrNK7uPbK/dfWyOe5+86qsrvNrMr91sa087bgyv3H6b/2z8Kx7cq+tcTK/SYjMjA1NDA7tPPQocrHsrvNrLXEPC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:">#include
#define Binary
#define Octal
#define Decimal 100
#define Hex
int main()
printf("the Binary
%d\n", Binary);
printf("the Octal
%d\n", Octal);
printf("the Decimal %d\n", Decimal);
printf("the Hex
%d\n", Hex);
运行结果:
the Binary
the Decimal 100
C语言中默认数值是十进制,如果一个数值是0开头的就是八进制,如果以0x或者0X开头就是十六进制
同样可以通过printf指定不同的格式打印相应的进制
%d %u有无符号十进制打印
八进制打印
%x %X 十六进制打印,两者区别x约定使用abcedf,而X预定使用ABCDEF
二进制与八进制/十六进制转换比较简单,因为分别是2的3次方,4次方
1100(二进制)
001100 ---->
14(八进制)
1100(二进制)
----> c (十六进制)
反之也是一样
十进制与二进制转换
十进制 ------> 二进制
这里可以用十六进制过度。16 ,256 ,4096 ,65536 ,1048576 , ...(分别是就是16^1 ,16^2 ,16^3……)
先找第一个比要转的那个数小的数,然后用这个数除那个要转的数,得到的商就是那个位的数(位数就是是指数),然后找到第一个比余数小的数,)
9999 ----> 二进制?
商为2,说明十六进制的第3位为2,余数为1807。
商为7,说明十六进制的第2位为5,余数为15。
后面的直接是十六进制第一位为0,第零位为15
9999(十进制) ----> 270f(十六进制) -----> 00 1111(二进制)
这样就比9999这么大的数一直除2快许多了
二进制转十进制比较简单,如前面所说的按位权乘加就可以了。十进制转八进制、十六进制和上面的一样,只是省去了转换为二进制的步骤23:03 提问
java-输出给定整数i的二进制补码
我刚开始学习java,学完“结构化程序设计”章节后,遇到了一道题,如下:“完成下面的通用程序,要求能够输出给定整数i的二进制补码。
public class Example
public static void main(String args[])
//在这里,请写上尚未完成的程序,使得本程序在执行时能输出i的二进制补码
在网上找到一个方法是使用语句System.out.println(Integer.toBinaryString(i));
但是integer和binary string还没有在本章及之前的章节中出现过,所以想请教高手是否还有其他方法啊?谢谢啦!!!
按赞数排序
/* // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
public static void foo(int i)
if (i == 0)
foo(i / 2);
System.out.print(i % 2);
public static void main (String[] args) throws java.lang.Exception
// your code goes here
int i = 10;
if (i & 0) foo(i);
if (i == 0) System.out.print(0);
if (i & 0) foo(i +
(正数的补码直接转换为二进制,负数的,先加上2^(N-1)再变二进制,对于32bit来说,N=32)
这个是jdk提供的api,提前了解下呗Integer是int的包装类,封装了很多实用的数值操作方法。像这个转换二进制的方法,我们就可以直接调用就好了啊,很方便的。至于实现过程,你按下F3跟踪下源码
不要局限章节吧,学前班还要念一年级的书磊。正数的原码反码补码都一样,最快的方法就是Integer.toBinaryString()
其他相关推荐

我要回帖

更多关于 java实现进制转换 的文章

 

随机推荐