全部版块 我的主页
论坛 数据科学与人工智能 IT基础 JAVA语言开发
1730 13
2015-07-26

  • Technical Java™: Developing Scientific and Engineering Applications
  • By: Grant Palmer

  • Publisher: Prentice Hall

  • Pub. Date: April 21, 2003

  • Print ISBN-10: 0-13-101815-9

  • Print ISBN-13: 978-0-13-101815-0

  • Pages in Print Edition: 496

  • Subscriber Rating: [0 Ratings]



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2015-7-26 06:51:55
Java Classes vs. C Structs

Java is an object-oriented programming language. C is not. The C language does not support objects. The closest C comes to defining an object is the struct or union programming constructs. As you probably know, a struct is a collection of named data members. The data members can be of different types. You can declare a struct variable that is a reference to a struct. The values of the struct data members can be accessed by typing the structvariable name, a period, and the data member name.

If you are familiar with using structs, it shouldn't be too great a leap to become familiar with the concept of a class. A Java class is basically just a named block of code that defines a collection of both fields and methods. A class is more self-contained than a struct in that a class can (and should) define all the methods required to manipulate its data members. Java classes can make use of the powerful object-oriented concepts of inheritance, polymorphism, and encapsulation. There are many different types of classes according to whether one or more modifying keywords are applied to them. You can have public and private classes, abstract and final classes. You can nest a class definition inside another class definition

You can create instances of a class. A class instance is also known as an object. You can declare variables that are references to class instances. A class can define a class instance as one of its fields.


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-7-26 06:54:39
Variables

Both C and Java define a variety of data types. The types each language supports are shown in [url=]Table 3.1[/url].

Some of the types supported by C are not supported by Java and vice versa. Java does not support signed or unsigned types. All Java integral types (including char) are signed. Java does not use the long double or void type. Java does not support pointers, so there are no pointer or function pointer types in Java. The same can be said of struct and union types.

Java does provide some useful data types that are not provided by C. The boolean type has a true or false value. Using a boolean variable is more convenient than using 0 for false and 1 for true as in C. Java provides the byte type that represents a single byte of data. Because Java is object oriented, you can declare variables that are references to class instances (objects).

When a C variable is declared outside of any function definition it is a global variable and is available anywhere inside a C program. Java does not support global variables. A Java class variable is one that is declared outside of any method definitions and is available anywhere inside the class in which it is declared. Both C and Java support local variables. A local variable is declared inside a method, a for statement, or other block of code and exists only inside the block in which it is defined.



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-7-26 06:55:59
Pointers

A C program can make use of pointers to directly access locations in memory. Pointers can be used to change the value of a variable and are also routinely used to manipulate arrays. Pointers can be useful in certain situations but can also be dangerous in that pointers have the potential to do bad things such as corrupt memory.

Java does not support publicly accessible pointers. Java has been designed so that there is no reason to use pointers. A Java primitive type variable stores a single value. You can manipulate the value directly. A reference type variable holds the address where a class instance or array is stored. The reference type variable provides all the access you will need.


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-7-26 06:57:41
Functions and Methods

In C they are called functions. Java calls them methods. Both are named blocks of code that can be called from somewhere else in a program. Functions are the basic building blocks of a C program. Java methods, on the other hand, are always declared inside of a class. Java methods do not have to be prototyped.

A C function declaration includes the return type, function name, and input parameter list. The only modifying keywords that can be applied to a C function are extern, which means the function is defined outside of the current file, or static, which means the function cannot be referenced from other files. The body of the function is enclosed in braces.

Java method declarations contain the return type, name, and input parameter list of the method. The declaration may also include modifying keywords and a throws clause. Java methods are more powerful and versatile than their C counterparts. For one thing, Java methods can be given an access modifier. This keyword defines how and when the method can be accessed by other code. Java methods can be declared to be static, abstract, final, native, andsynchronized. As with C, the body of a Java method is enclosed in braces.

Input arguments are passed to a C function by value, meaning that a copy of the value of the variable is sent to the function. Any changes made to the variable inside the function will not be reflected in the original variable. To have a C function alter the value of the original variable, a pointer to the variable must be passed to the function.

Arguments to Java methods are also passed by value. For arguments representing primitive types, a copy of the argument value is sent to the method. Any changes made to the value inside the method will not be made to the original value. For arguments representing reference types (objects or arrays), a copy of the reference to the location in memory where the data for the object or array is stored is sent to the method. Any changes made to the object inside the method will also be made to the original object.

C functions are called by simply typing the function name and providing the appropriate arguments. The way that Java methods are called depends on where the method call occurs. Inside the class in which they are defined, Java methods can be called by typing the method name and providing the appropriate arguments. Outside of the class in which they are defined, Java methods are called relative to a class instance or the class name depending on whether it is an instance or static method.


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-7-26 06:59:36
Arrays

While Java and C both start array indices from zero, there is a fundamental difference in the way the two languages treat arrays. With C, an array of a given data type is treated as a sequence of members of that type stored in contiguous locations in memory. The declaration syntax is the data type, the array name, and the size of the array enclosed in brackets.

int data[5];

The members of a C array can be accessed using the array name and index or with a pointer to the array. C arrays can be given a fixed size when they are declared or you can create a dynamically allocated array using pointers and themalloc() function.

In Java, arrays are reference types just like class instances. Creating a Java array is a two-step process. You first declare an array variable by specifying its type and name followed by a pair of brackets—

int data[];

Declaring an array variable does not allocate any memory for it. The second step in the array creation process is to allocate memory for the array variable using the new keyword followed by the type and size of the array.

data = new int[5];—

The two steps can be combined to a single line

int data[] = new int[5];

Elements of a C array can be accessed either through the array index or by using a pointer to the array. Because Java does not support pointers, Java array elements are accessed only through indexes. Both C and Java use a pair of brackets around the index of the element to be accessed. For example, to assign the value 45 to the third element of an array named data you would type—

data[2] = 45;

When looking at the above syntax, keep in mind that Java array indices start at 0. The first element of the data array would be data[0].


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群