首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网络编程-> JSP教程
Operators and Assignments(2)-JSP教程,Java基础
作者:网友供稿 点击:11
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
objective 2)
determine the result of applying the boolean equals(object) method to objects of any combination of the classes java.lang.string java.lang.boolean and java.lang.object.

the equals method can be considered to perform a deep comparison of the value of an object, whereas the == operator performs a shallow comparison. the equals method compares what an object points to rather than the pointer itself (if we can admit that java has pointers).

the equals method applied to a string, however that string was created, performs a character by character comparison.

objective 3)
in an expression involving the operators & | && || and variables of known values state which operands are evaluated and the value of the expression.

objective 4)
determine the effect upon objects and primitive values of passing variables into methods and performing assignments or other modifying operations in that method.

unary numeric promotion
contexts:
·    operand of the unary arithmetic operators + and –
·    operand of the unary integer bit-wise complement operator ~
·    during array creation, for example new int[x], where the dimension expression x must evaluate to an int value.
·    indexing array elements, for example table[‘a’], where the index expression must evaluate to an int value.
·    individual operands of the shift operators.

binary numeric promotion
contexts:
·    operands of arithmetic operators *, / , %, + and –
·    operands of relational operators <, <= , > and >=
·    numeric operands of equality operators == and !=
·    integer operands of bit-wise operators &, ^ and |

conversion of primitives
1.    3 types of conversion – assignment conversion, method call conversion and arithmetic promotion
2.    boolean may not be converted to/from any non-boolean type.
3.    widening conversions accepted. narrowing conversions rejected.
4.    byte, short can’t be converted to char and vice versa.
5.    arithmetic promotion
5.1    unary operators
·    if the operand is byte, short or char  {
convert it to int;
           }
            else {
do nothing; no conversion needed;
}
5.2    binary operators
·    if one operand is double {
all double; convert the other operand to double;
           }
           else if one operand is float {
all float; convert the other operand to float;
}
else if one operand is long {
all long; convert the other operand to long;
}
else {
all int; convert all to int;
}
6.    when assigning a literal value to a variable, the range of the variable’s data type is checked against the value of the literal and assignment is allowed or compiler will produce an error.
char c = 3; // this will compile, even though a numeric literal is by default an int since the range of         char will accept the value
int a = 3;
char d = a; // this won’t compile, since we’re assigning an int to char
char e = -1; // this also won’t compile, since the value is not in the range of char
float f = 1.3; // this won’t compile, even though the value is within float range. here range is not  important, but precision is. 1.3 is by default a double, so a specific cast or f = 1.3f will work.
float f = 1/3; // this will compile, since rhs evaluates to an int.
float f = 1.0 / 3.0; // this won’t compile, since rhs evaluates to a double.
7.    also when assigning a final variable to a variable, even if the final variable’s data type is wider than the variable, if the value is within the range of the variable an implicit conversion is done.
byte b;
final int a = 10;
b = a; // legal, since value of ‘a’ is determinable and within range of b
final int x = a;
b = x; // legal, since value of ‘x’ is determinable and within range of b
int y;
final int z = y;
b = z; // illegal, since value of ‘z’ is not determinable

8.    method call conversions always look for the exact data type or a wider one in the method signatures. they will not do narrowing conversions to resolve methods, instead we will get a compile error.

here is the figure of allowable primitive conversion.

byte à short à int à long à float à double
                            ­
           char

casting of primitives
9.    needed with narrowing conversions. use with care – radical information loss. also can be used with widening conversions, to improve the clarity of the code.
10.    can cast any non-boolean type to another non-boolean type.
11.    cannot cast a boolean or to a boolean type.

conversion of object references
12.    three types of reference variables to denote objects - class, interface or array type.
13.    two kinds of objects can be created – class or array.
14.    two types of conversion – assignment and method call.
15.    permitted if the direction of the conversion is ‘up’ the inheritance hierarchy. means that types can be assigned/substituted to only super-types – super-classes or interfaces. not the other way around, explicit casting is needed for that.
16.    interfaces can be used as types when declaring variables, so they participate in the object reference conversion. but we cannot instantiate an interface, since it is abstract and doesn’t provide any implementation. these variables can be used to hold objects of classes that implement the interface. the reason for having interfaces as types may be, i think, several unrelated classes may implement the same interface and if there’s a need to deal with them collectively one way of treating them may be an array of the interface type that they implement.
17.    primitive arrays can be converted to only the arrays of the same primitive type. they cannot be converted to another type of primitive array. only object reference arrays can be converted / cast.
18.    primitive arrays can be converted to an object reference, but not to an object[] reference. this is because all arrays (primitive arrays and object[]) are extended from object.

casting of object references
19.    allows super-types to be assigned to subtypes. extensive checks done both at compile and runtime. at compile time, class of the object may not be known, so at runtime if checks fail, a classcastexception is thrown.
20.    cast operator, instanceof operator and the == operator behave the same way in allowing references to be the operands of them. you cannot cast or apply instanceof or compare unrelated references, sibling references or any incompatible references.


compile-time rules
·    when old and new types are classes, one class must be the sub-class of the other.
·    when old and new types are arrays, both must contain reference types and it must be legal to cast between those types (primitive arrays cannot be cast, conversion possible only between same type of primitive arrays).
·    we can always cast between an interface and a non-final object.

run-time rules
·    if new type is a class, the class of the expression being converted must be new type or extend new type.
·    if new type is an interface, the class of the expression being converted must implement the interface.

an object reference can be converted to: (java.lang.object)
·    an object reference
·    a cloneable interface reference, with casting, with runtime check
·    any class reference, with casting, with runtime check
·    any array referenece, with casting, with runtime check
·    any interface reference, with casting, with runtime check

a class type reference can be converted to:
·    any super-class type reference, (including object)
·    any sub-class type reference, with casting, with runtime check
·    an interface reference, if the class implements that interface
·    any interface reference, with casting, with runtime check (except if the class is final and doesn’t implement the interface)

an interface reference can be converted to:
·    an object reference
·    a super-interface reference
·    any interface/class reference with casting, with runtime check (except if the class is final and doesn’t implement the interface)

a primitive array reference can be converted to:
·    an object reference
·    a cloneable interface reference
·    a primitive array reference of the same type

an object array reference can be converted to:
·    an object reference
·    a  cloneable interface reference
·    a super-class array reference, including an object array reference
·    any sub-class array reference with casting, with runtime check

examples
q1 given these class definitions:
class superclass { }
class subclass1 extends superclass { }

and these objects:
superclass a = new superclass();
subclass1 b = new subclass1();
which of the following explains the result of the statement:
b = (subclass1)a;

select the one right answer.
a) illegal at compile time
b) legal at compile time but possibly illegal at runtime  // throw a classcastexception
c) definitely legal at runtime


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·利用 Java Web Start发布你用java程序-JSP教程,Java技巧及代码
·用正则表达式得到网页上的链接-.NET教程,评论及其它
·MSMQ,Enterprise Service, DotNet Remoting,Web Service 的优缺点-.NET教程,Web Service开发
·用vb编一个计算器,需要用到数组,看看下面的代码,欢迎来找碴!-.NET教程,VB.Net语言
·jsp页面中的下载功能实现-JSP教程,Jsp/Servlet
·利用Java 创建和读取Excel文档-JSP教程,Java技巧及代码
·JSP语法(8)——<jsp:forward>-JSP教程,Jsp/Servlet
·Hibernate 配置!-JSP教程,资料/其它
·java、J2EE基础问题汇总-JSP教程,Java技巧及代码
·Java手机程序设计入门 电子书开放下载(转自CSDN)-JSP教程,J2ME开发
最新文章
·photoshop制作重彩风格非主流照片效果_photoshop教程
·google补充材料没消失,内链优化很重要_seo网站优化
·个人网站建设到底怎样赚钱_网赚技巧
·英文垃圾站全功略ip日100月赚50刀_网赚技巧
·清客讲网赚思路_网赚技巧
·windear与渡虎谷密谋_站长访谈
·口碑网ceo李治国专访:独特的平衡之道_站长访谈
·密密麻麻圈网邵晨:我从“互动”中赚钱_站长访谈
·蔡文胜:站长的乐趣是享受建站的过程_站长访谈
·“鹰”之路—访著名linux内核程序员大鹰_站长访谈
相关主题
  • opera浏览器实用技巧两则_网页浏览
  • Operators and Assignments(1)-JSP教程,Java基础
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭