Java_详解_1、==和Equal

总结

1= =操作符比较的是操作符两端的操作数是否是同一个对象;另外= =操作符两边的操作数必须是同一类型的(可以是父子类之间)才能编译通过。

2Stringequals()方法比较的是两个String对象的内容是否一样

3= =比较的是地址,如果是具体的阿拉伯数字的比较,值相等则为TRUE,如:

int a=10 与 long b=10L 与 double c=10.0都是相同的(为true),因为他们都指向地址为10的堆栈;如下题111

String s= "hello";

String t = "hello";

char c[] = {'h','e','l','l','o'} ;

Which return true?

A. s.equals(t);

B. t.equals(c);

C. s==t;

D. t.equals(new String("hello"));

E. t==c.

答案:(acd)

题目:哪些返回true。

这个在前面第10题的equals()方法和==操作符的讨论中论述过。==操作符比较的是操作符两端的操作数是否是同一个对象,而Stringequals()方法比较的是两个String对象的内容是否一样,其参数是一个String对象时才有可能返回true,其它对象都返回假。需要指出的是由于st并非使用new创建的,他们指向内存池中的同一个字符串常量,因此其地址实际上是相同的(这个可以从反编译一个简单的测试程序的结果得到,限于篇幅不列出测试代码和反编译的分析),因此答案c也是正确的。

Given the following class:

public class Sample{

long length;

public Sample(long l){ length = l; }

public static void main(String arg[]){

Sample s1, s2, s3;

s1 = new Sample(21L);

s2 = new Sample(21L); 

s3 = s2;

long m = 21L;

}

}

Which expression returns true?

A. s1 == s2;

B. s2 == s3;

C. m == s1;

D. s1.equals(m).

答案:(b)//D不对,只有String的equals()方法才比较值;

题目:给出下面的类:  

哪个表达式返回true。

前面已经叙述过==操作符和Stringequals()方法的特点,另外==操作符两边的操作数必须是同一类型的(可以是父子类之间)才能编译通过。

17. float f=4.2F; 

Float g=new Float(4.2F); 

Double d=new Double(4.2); 

Which are true? 

A. f= =g   B. g= =g   C. d= =f   D. d.equals(f)  E d.equals(g)  F. g.equals(4.2); 

答案:B

93. Click the exhibit button:

1. public class X { 

2. public static void main (String[]args)  { 

3. String s1 = new String (“true”); 

4. Boolean b1 = new Boolean (true); 

5. if (s2.equals(b1))   { 

6. System.out.printIn(“Equal”);

 7.       } 8.      } 9.     }   

What is the result?

A. The program runs and prints nothing.

B. The program runs and prints “Equal.”

C. An error at line 5 causes compilation to fail.

D. The program runs but aborts with an exception.

答案:A

比较下题,小心使用equals 和 = =的区别;

93. Click the exhibit button:

1. public class X { 

2. public static void main (String[]args)  { 

3. String s1 = new String (“true”); 

4. Boolean b1 = new Boolean (true); 

5. if (s2 = = b1) { //= =操作符两边的操作数必须是同一类型的(可以是父子类之间)才能编译通过

6. System.out.printIn(“Equal”);

 7.       } 8.      } 9.     }   

What is the result?

A. The program runs and prints nothing.

B. The program runs and prints “Equal.”

C. An error at line 5 causes compilation to fail.

D. The program runs but aborts with an exception.

答案:C

111. Given:

1. public class Foo {

2. private int val;

3. public foo(int v) (val = v;)  }

4. public static void main (String [] args)  {

5. Foo a = new Foo (10);

6. Foo b = new Foo (10);

7. Foo c = a;

8. int d = 10;

9. double e = 10.0;

10. }

11. }

Which three logical expressions evaluate to true? (Choose Three)  

A.(a ==c)

B.(d ==e)

C.(b ==d)

D.(a ==b)

E.(b ==c)

F.(d ==10.0)

答案:ABF //= =比较的是地址,他们都指向地址为10的堆栈;

Given the following code, what test would you need to put in place of the comment line? 

//place test here to result in an output of the string Equal 

public class EqTest{
 public static void main(String argv[]){
 EqTest e=new EqTest();
  }

EqTest(){
 String s="Java";
 String s2="java";//小心大小写
 //place test here {
 System.out.println("Equal");
 }else
 {
 System.out.println("Not equal");
 }
 }
}

1) if(s==s2) 
2) if(s.equals(s2) 
3) if(s.equalsIgnoreCase(s2)) 
4)if(s.noCaseMatch(s2))

答案:3//小心大小写

请结合以上经验对以下代码进行优化,并给出优化思路结果 package com.x.kvm.cl.kvm; import com.x.base.ioc.AnnoBeanAttachToIoc; import com.x.base.ioc.AnnoIocBean; import com.x.base.ioc.Ioc; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; @AnnoIocBean( beanName = "Encode" ) @AnnoBeanAttachToIoc public class Encode { private Ioc ioc = null; static final byte CMD_CO = 0; static final byte CMD_CL = 32; static final byte CMD_CA = 64; static final byte CMD_MS = 96; static final byte CMD_MP = -128; static final byte MS_MF = -128; static final byte MS_FIRST_MF = 16; static final byte STATE_CO = 1; static final byte STATE_CL = 2; static final byte STATE_CA = 4; static final byte STATE_MS = 8; static final byte STATE_MP = 16; static final byte PIXEL_SHRESHOLD = 2; static final byte BLOCK_WIDTH = 32; static final int BLOCK_LEN = 1024; static final int MAX_FILE_LEN = 6291456; byte[] frame_header = new byte[54]; int[] pos_map = new int[1024]; int co_total_num = 0; int ca_total_num = 0; int cl_total_num = 0; int ms_total_num = 0; int mp_total_num = 0; int total_num = 0; FRAME_ENCODE_STAT frame_encode_stat; FRAME_INFO p_frame_info; boolean IS_CO_ENABLE(byte s) { return 0 == s || (s & 1) != 0; } boolean IS_CL_ENABLE(byte s) { return 0 == s || (s & 2) != 0; } boolean IS_CA_ENABLE(byte s) { return 0 == s || (s & 4) != 0; } boolean IS_MS_ENABLE(byte s) { return 0 == s || (s & 8) != 0; } int GETPOS(int s, int i) { return s + this.p_frame_info.pos_map[i]; } int co_equal(int bpos, int index, FRAME_INFO p_frame_info) { int real_index = this.GETPOS(bpos, index); return Math.abs(p_frame_info.src_buf[real_index] - p_frame_info.prev_buf[real_index]) > 2 ? 0 : 1; } int cl_equal(int bpos, int index, FRAME_INFO p_frame_info) { return Math.abs(p_frame_info.src_buf[this.GETPOS(bpos, index)] - p_frame_info.src_buf[this.GETPOS(bpos, index - 1)]) > 2 ? 0 : 1; } int ca_equal(int bpos, int index, FRAME_INFO p_frame_info) { return Math.abs(p_frame_info.src_buf[this.GETPOS(bpos, index)] - p_frame_info.src_buf[this.GETPOS(bpos, index - 32)]) > 2 ? 0 : 1; } int is_ms_fit_first_pixel() { return 0; } int is_ms_fit_first_line() { return 0; } int is_ms_fit() { return 0; } int block_encode(int block_start_index, int dst_count, FRAME_INFO p_frame_info) { int index = 0; int co_num = 0; int ca_num = 0; int cl_num = 0; byte prev_state = 0; byte cur_state; for(byte ret; index < 1; prev_state = cur_state) { cur_state = 0; int real_index = this.GETPOS(block_start_index, index); ret = (byte)this.is_ms_fit_first_pixel(); if (ret != 1) { if (ret > 1) { cur_state = 1; co_num = ret; index += ret; } else { if (this.IS_CO_ENABLE(prev_state) && this.co_equal(block_start_index, index, p_frame_info) == 1) { ++co_num; cur_state = (byte)(cur_state | 1); } if (0 == cur_state) { p_frame_info.dst_buf[dst_count++] = (byte)(-128 | p_frame_info.src_buf[real_index] >> 8); p_frame_info.dst_buf[dst_count++] = (byte)(p_frame_info.src_buf[real_index] & 255); ++this.mp_total_num; ++this.frame_encode_stat.mp_total_count; p_frame_info.color_set[1] = p_frame_info.color_set[0]; p_frame_info.color_set[0] = p_frame_info.src_buf[real_index]; ++index; co_num = 0; } else { ++index; } } } } for(; index < 32; prev_state = cur_state) { cur_state = 0; int real_index = this.GETPOS(block_start_index, index); if (prev_state == 0) { byte var26 = (byte)this.is_ms_fit_first_line(); if (var26 != 1) { if (var26 > 1) { cur_state = 1; co_num = var26; index += var26; var26 = 0; } else { if (this.IS_CO_ENABLE(prev_state) && this.co_equal(block_start_index, index, p_frame_info) == 1) { ++co_num; cur_state = (byte)(cur_state | 1); } if (0 != cur_state) { ++index; } else { p_frame_info.dst_buf[dst_count++] = (byte) (-128 | p_frame_info.src_buf[real_index] >> 8); p_frame_info.dst_buf[dst_count++] = (byte)(p_frame_info.src_buf[real_index] & 255); ++this.mp_total_num; ++this.frame_encode_stat.mp_total_count; p_frame_info.color_set[1] = p_frame_info.color_set[0]; p_frame_info.color_set[0] = p_frame_info.src_buf[real_index]; ++index; } } } } else { if (this.IS_CO_ENABLE(prev_state) && this.co_equal(block_start_index, index, p_frame_info) == 1) { ++co_num; cur_state = (byte)(cur_state | 1); } if (this.IS_CL_ENABLE(prev_state) && this.cl_equal(block_start_index, index, p_frame_info) == 1) { ++cl_num; cur_state = (byte)(cur_state | 2); } if (0 != cur_state) { ++index; } else { if ((prev_state & 1) != 0) { this.co_total_num += co_num; do { ++this.frame_encode_stat.co_total_count; p_frame_info.dst_buf[dst_count++] = (byte)(co_num & 31); co_num >>= 5; } while(0 != co_num); } else if ((prev_state & 2) != 0) { this.cl_total_num += cl_num; do { ++this.frame_encode_stat.cl_total_count; p_frame_info.dst_buf[dst_count++] = (byte)(32 | cl_num & 31); cl_num >>= 5; } while(0 != cl_num); } else { p_frame_info.dst_buf[dst_count++] = (byte)(-128 | p_frame_info.src_buf[real_index] >> 8); p_frame_info.dst_buf[dst_count++] = (byte)(p_frame_info.src_buf[real_index] & 255); ++this.mp_total_num; ++this.frame_encode_stat.mp_total_count; p_frame_info.color_set[1] = p_frame_info.color_set[0]; p_frame_info.color_set[0] = p_frame_info.src_buf[real_index]; ++index; } co_num = 0; cl_num = 0; } } } for(; index < 1024; prev_state = cur_state) { cur_state = 0; int real_index = this.GETPOS(block_start_index, index); if (prev_state == 0) { byte var28 = (byte)this.is_ms_fit(); if (var28 != 1) { if (var28 > 1) { cur_state = 1; co_num = var28; index += var28; } else { if (this.IS_CO_ENABLE(prev_state) && this.co_equal(block_start_index, index, p_frame_info) == 1) { ++co_num; cur_state = (byte)(cur_state | 1); } if (this.IS_CL_ENABLE(prev_state) && this.cl_equal(block_start_index, index, p_frame_info) == 1) { ++cl_num; cur_state = (byte)(cur_state | 2); } if (this.IS_CA_ENABLE(prev_state) && this.ca_equal(block_start_index, index, p_frame_info) == 1) { ++ca_num; cur_state = (byte)(cur_state | 4); } if (0 != cur_state) { ++index; } else { p_frame_info.dst_buf[dst_count++] = (byte) (-128 | p_frame_info.src_buf[real_index] >> 8); p_frame_info.dst_buf[dst_count++] = (byte)(p_frame_info.src_buf[real_index] & 255); ++this.mp_total_num; ++this.frame_encode_stat.mp_total_count; p_frame_info.color_set[1] = p_frame_info.color_set[0]; p_frame_info.color_set[0] = p_frame_info.src_buf[real_index]; ++index; } } } } else { if (this.IS_CO_ENABLE(prev_state) && this.co_equal(block_start_index, index, p_frame_info) == 1) { ++co_num; cur_state = (byte)(cur_state | 1); } if (this.IS_CL_ENABLE(prev_state) && this.cl_equal(block_start_index, index, p_frame_info) == 1) { ++cl_num; cur_state = (byte)(cur_state | 2); } if (this.IS_CA_ENABLE(prev_state) && this.ca_equal(block_start_index, index, p_frame_info) == 1) { ++ca_num; cur_state = (byte)(cur_state | 4); } if (0 != cur_state) { ++index; } else { if ((prev_state & 1) != 0) { this.co_total_num += co_num; do { ++this.frame_encode_stat.co_total_count; p_frame_info.dst_buf[dst_count++] = (byte)(co_num & 31); co_num >>= 5; } while(0 != co_num); } else if ((prev_state & 2) != 0) { this.cl_total_num += cl_num; do { ++this.frame_encode_stat.cl_total_count; p_frame_info.dst_buf[dst_count++] = (byte)(32 | cl_num & 31); cl_num >>= 5; } while(0 != cl_num); } else if ((prev_state & 4) != 0) { this.ca_total_num += ca_num; do { ++this.frame_encode_stat.ca_total_count; p_frame_info.dst_buf[dst_count++] = (byte)(64 | ca_num & 31); ca_num >>= 5; } while(0 != ca_num); } else { p_frame_info.dst_buf[dst_count++] = (byte)(-128 | p_frame_info.src_buf[real_index] >> 8); p_frame_info.dst_buf[dst_count++] = (byte)(p_frame_info.src_buf[real_index] & 255); ++this.mp_total_num; ++this.frame_encode_stat.mp_total_count; p_frame_info.color_set[1] = p_frame_info.color_set[0]; p_frame_info.color_set[0] = p_frame_info.src_buf[real_index]; ++index; } co_num = 0; cl_num = 0; ca_num = 0; } } } if ((prev_state & 1) != 0) { this.co_total_num += co_num; do { ++this.frame_encode_stat.co_total_count; p_frame_info.dst_buf[dst_count++] = (byte)(co_num & 31); co_num >>= 5; } while(0 != co_num); } else if ((prev_state & 2) != 0) { this.cl_total_num += cl_num; do { ++this.frame_encode_stat.cl_total_count; p_frame_info.dst_buf[dst_count++] = (byte)(32 | cl_num & 31); cl_num >>= 5; } while(0 != cl_num); } else if ((prev_state & 4) != 0) { this.ca_total_num += ca_num; do { ++this.frame_encode_stat.ca_total_count; p_frame_info.dst_buf[dst_count++] = (byte)(64 | ca_num & 31); ca_num >>= 5; } while(0 != ca_num); } return dst_count; } void init_pos_map(FRAME_INFO p_frame_info) { int block_index = 0; int pos = 0; for(int index_y = 0; index_y < 32; ++index_y) { for(int index_x = 0; index_x < 32; ++index_x) { p_frame_info.pos_map[block_index++] = pos++; } pos += p_frame_info.width - 32; } } void encode_pre_process(FRAME_INFO p_frame_info) { byte[] src_buf = p_frame_info.src_origin_buf; p_frame_info.width = p_frame_info.real_width; int real_height = p_frame_info.real_height; if (0 == real_height % 32) { p_frame_info.height = real_height; } else { int comp_height = 32 - real_height % 32; p_frame_info.height = real_height + comp_height; for(int index_y = real_height; index_y < p_frame_info.height; ++index_y) { Arrays.fill(src_buf, index_y * p_frame_info.width * 3, index_y * p_frame_info.width * 3 + p_frame_info.width * 3 - 1, (byte)0); } } p_frame_info.block_x_num = (short)(p_frame_info.width / 32); p_frame_info.block_y_num = (short)(p_frame_info.height / 32); p_frame_info.len = p_frame_info.width * p_frame_info.height * 3; this.init_pos_map(p_frame_info); p_frame_info.color_depth = 0; p_frame_info.block_threshold = 0; p_frame_info.pixel_threshold = 1; } void convert_to_15bit(FRAME_INFO p_frame_info) { byte[] src_origin_buf = p_frame_info.src_origin_buf; short[] src_buf = p_frame_info.src_buf; int index = 0; for(int dst_index = 0; index < p_frame_info.len; ++dst_index) { src_buf[dst_index] = (short)((src_origin_buf[index + 2] & 248) << 7 | (src_origin_buf[index + 1] & 248) << 2 | (src_origin_buf[index] & 248) >> 3); index += 3; } } int frame_encode(FRAME_INFO p_frame_info) { int block_start_index; int block_index = 0; int start_count; int end_count; int dst_count = 0; this.frame_encode_stat.ca_total_count = 0; this.frame_encode_stat.cl_total_count = 0; this.frame_encode_stat.co_total_count = 0; this.frame_encode_stat.mp_total_count = 0; this.frame_encode_stat.ms_total_count = 0; p_frame_info.color_set[0] = 0; p_frame_info.color_set[1] = 0; this.convert_to_15bit(p_frame_info); for(int index_y = 0; index_y < p_frame_info.block_y_num; ++index_y) { for(int index_x = 0; index_x < p_frame_info.block_x_num; ++index_x) { block_start_index = index_y * 32 * p_frame_info.width + index_x * 32; if (block_index % 16 == 0) { start_count = dst_count + 8; p_frame_info.dst_buf[dst_count++] = 79; p_frame_info.dst_buf[dst_count++] = 78; p_frame_info.dst_buf[dst_count++] = 76; p_frame_info.dst_buf[dst_count++] = (byte)(index_x + 1); p_frame_info.dst_buf[dst_count++] = (byte)(index_y + 1); p_frame_info.dst_buf[dst_count++] = 0; p_frame_info.dst_buf[dst_count++] = 0; p_frame_info.dst_buf[dst_count++] = 0; } else { start_count = dst_count; } end_count = this.block_encode(block_start_index, start_count, p_frame_info); dst_count = end_count; ++block_index; } } return dst_count; } int encode_init() { this.p_frame_info = new FRAME_INFO(); this.p_frame_info.src_buf = new short[3145728]; this.p_frame_info.prev_buf = new short[3145728]; this.p_frame_info.dst_buf = new byte[6291456]; this.p_frame_info.src_origin_buf = new byte[6291456]; this.p_frame_info.prev_origin_buf = new byte[6291456]; this.frame_encode_stat = new FRAME_ENCODE_STAT(); return 0; } int send_frame(String filePath, int dst_count, FRAME_INFO p_frame_info) throws FileNotFoundException, IOException { File file = new File(filePath); try (DataOutputStream dout = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) { dout.write(p_frame_info.dst_buf, 0, dst_count); } return 0; } public IFrame outIFrame(int Width, int Height, byte[] src_buf) { int dst_count; this.encode_init(); this.p_frame_info.real_height = Height; this.p_frame_info.real_width = Width; this.p_frame_info.src_origin_buf = src_buf; this.encode_pre_process(this.p_frame_info); long time = System.currentTimeMillis(); dst_count = this.frame_encode(this.p_frame_info); System.out.println("编码 I 帧时间=" + (System.currentTimeMillis() - time) + "(ms)"); IFrame frame = new IFrame(); frame.data = this.p_frame_info.dst_buf; frame.length = dst_count; return frame; } private static int byte2Int(byte[] b) throws IOException { return b[3] & -16777216 | (b[2] & 255) << 16 | (b[1] & 255) << 8 | b[0] & 255; } static class FRAME_INFO { int real_width; int real_height; int width; int height; int len; short block_x_num; short block_y_num; short[] src_buf; short[] prev_buf; byte[] dst_buf; byte[] src_origin_buf; byte[] prev_origin_buf; byte color_depth; byte pixel_threshold; short block_threshold; short[] color_set = new short[2]; int[] pos_map = new int[1024]; } static class FRAME_ENCODE_STAT { int co_total_count; int ca_total_count; int cl_total_count; int mp_total_count; int ms_total_count; } public static class IFrame { public byte[] data = null; public int length; } }
最新发布
11-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值