java学习资料.doc_第1页
java学习资料.doc_第2页
java学习资料.doc_第3页
java学习资料.doc_第4页
java学习资料.doc_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JAVA vs C+Contents Simple Example Java Program o Test Yourself #1 Files o Test Yourself #2 Java Types o C+ Arrays vs Java Arrays Test Yourself #3 Test Yourself #4 o C+ Classes vs Java Classes o Aliasing Problems in Java Test Yourself #5 o Type Conversion Test Yourself #6 Answers to Self-Study Questions In Java, every variable, constant, and function (including main) must be inside some class. Heres a simple example program: class Test public static void main( String args ) System.out.println(Hello world!);Things to note: 1. There is no final semi-colon at the end of the class definition. 2. Function main is a member of class Test. 3. In general, main must: o Be inside some class (there can be more than one main function - there can even be one in every class!) o Be public static void. o Have one argument: an array of String. This array contains the command-line arguments. You can use args.length to determine the number of arguments (the number of Strings in the array). 4. To write to standard output, you can use either of the following: 5. System.out.println( . )6. System.out.print( . ) The former prints the given expression followed by a newline, while the latter just prints the given expression. Like the C+ AddToEnd(.) / call the AddToEnd function of the List pointed to by p JAVAList L; / L is a pointer to a List; no List object exists yetL = new List(); / now storage for a List has been allocated / and the constructor function has been called;/ note that you must use parentheses even when you are not/ passing any arguments to the constructor functionL.AddToEnd(.) / no - operator in Java - just use .Aliasing Problems in JavaThe fact that arrays and classes are really pointers in Java can lead to some problems: Problem 1: Simple assignment causes aliasing: Java code conceptual picture (all empty boxes contain zeros) +-+ +-+-+-+int A = new int3, A: | -|- | | | | +-+ +-+-+-+ B = new int2; +-+ +-+-+ B: | -|- | | | +-+ +-+-+ +-+ +-+-+-+ A0 = 5; A: | -|- | 5 | | | +-+ +-+-+-+ +-+ +-+-+-+ B = A; A: | -|- | 5 | | | +-+ +-+-+-+ +-+ | B: | -|-+ +-+ +-+ +-+-+-+ * NOTE * B0 = 2; A: | -|- | 2 | | | the value of A0 +-+ +-+-+-+ changed, too! +-+ | B: | -|-+ +-+Problem 2: In Java, all parameters are passed by value, but for arrays and classes the actual parameter is really a pointer, so changing an array element, or a class field inside the function does change the actual parameters element or field. For example: void f( int A ) A0 = 10; / change an element of parameter A A = null; / change A itselfvoid g() int B = new int 3; B0 = 5; f(B); * B is not null here, because B itself was passed by value * however, B0 is now 10, because function f changed the first element * of the arrayIn C+, similar problems can arise when a class that has pointer data members is passed by value. This problem is addressed by the use of copy constructors, which can be defined to make copies of the values pointed to, rather than just making copies of the pointers. In Java, the solution is to use the arraycopy operation, or to use a classs clone operation. Cloning will be discussed later. TEST YOURSELF #5 For each of the following Java code fragments, say whether it causes a compile-time error, a run-time error, or no error. If there is an error, explain why. 1. int A5; 2. int A, B; B = 0; 3. int A = 1,2,3; int B; B = A; 4. int A; A0 = 0; 5. int A = new int20; int B = new int10; A = B; A15 = 0;solution Type ConversionJava is much more limited than C+ in the type conversions that are allowed. Here we discuss conversions among primitive types. Conversions among class objects will be discussed later. Booleans cannot be converted to other types. For the other primitive types (char, byte, short, int, long, float, and double), there are two kinds of conversion: implicit and explicit. Implicit conversions: An implicit conversion means that a value of one type is changed to a value of another type without any special directive from the programmer. A char can be implicitly converted to an int, a long, a float, or a double. For example, the following will compile without error: char c = a;int k = c;long x = c;float y = c;double d = c;For the other (numeric) primitive types, the basic rule is that implicit conversions can be done from one type to another if the range of values of the first type is a subset of the range of values of the second type. For example, a byte can be converted to a short, int, long or float; a short can be converted to an int, long, float, or double, etc. Explicit conversions: Explicit conversions are done via casting: the name of the type to which you want a value converted is given, in parentheses, in front of the value. For example, the following code uses casts to convert a value of type double to a value of type int, and to convert a value of type double to a value of type short: double d = 5.6;int k = (int)d;short s = (short)(d * 2.0);Casting can be used to convert among any of the primitive types except boolean. Note, however, that casting can lose information; for example, floating-point values are truncated when they are cast to integers (e.g., the value of k in the code fragment given above is 5), and casting among integer types can produce wildly different values (because upper bits, possibly including

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论