C Programming vs. Java Programming
Thing | C | Java |
---|---|---|
type of language | function oriented | object oriented |
basic programming unit | function | class = ADT |
portability of source code | possible with discipline | yes |
portability of compiled code | no, recompile for each architecture | yes, bytecode is "write once, run anywhere" |
security | limited | built-in to language |
compilation | gcc hello.c creates machine language code | javac Hello.java creates Java virtual machine language bytecode |
linking in the Math library | gcc -lm calculate.c | no special flags needed |
joint compilation | gcc main.c helper1.c helper2.c | javac Main.java - any dependent files are automatically re-compiled if needed |
execution | a.out loads and executes program | java Hello interprets byte code |
hello, world | #include<stdio.h> int main(void) { printf("Hello\n"); return 0; } |
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello"); } } |
integer types | int usually 32 bit 2's complement; long usually 32 bit 2's complement |
int is 32 bit 2's complement; long is 64 bit 2's complement |
floating point types | float usually 32 bit; double usually 64 bit |
float is 32 bit IEEE 754 binary floating point; double is 64 bit IEEE 754 |
boolean type | use int: 0 for false, nonzero for true | boolean is its own type - stores value true or false |
character type | char is usually 8 bit ASCII | char is 16 bit UNICODE |
for loops | for (i = 0; i < N; i++) | for (int i = 0; i < N; i++) |
array declarations | int *a = malloc(N * sizeof(*a)); | int[] a = new int[N]; |
array size | arrays don't know their own size | a.length |
strings | '\0'-terminated character array | built-in immutable String data type |
accessing a library | #include <stdio.h> | import java.io.File; |
accessing a library function | #include "math.h" x = sqrt(2.2); all function and variables names are global |
x = Math.sqrt(2.2); functions have different namespaces |
printing to standard output | printf("sum = %d", x); | System.out.println("sum = " + x); |
formatted printing | printf("avg = %3.2f", avg); | System.out.printf("avg = %3.2f", avg) |
reading from stdin | scanf("%d", &x); | Java library support, but easier to use our library int x = StdIn.readInt(); |
memory address | pointer | reference |
manipulating pointers | *, &, + | no direct manipulation permitted |
functions | int max(int a, int b) | public static int max(int a, int b) |
pass-by-value | primitive data types, structs, and pointers are passed by value; array decays to pointer | all primitive data types and references (which includes arrays), are passed by value |
defining a data structure | struct | class - key difference is language support for defining methods to manipulate data |
accessing a data structure | a.numerator for elements | a.numerator for instance variables, c = a.plus(b) for methods |
pointer chasing | x->left->right | x.left.right |
allocating memory | malloc | new |
de-allocating memory | free | automatic garbage collection |
memory allocation of data structures and arrays | heap, stack, data, or bss | heap |
buffer overflow | segmentation fault, core dump, unpredicatable program | checked run-time error exception |
declaring constants | const and #define | final |
variable auto-initialization | not guaranteed | instance variables (and array elements) initialized to 0, null, or false, compile-time error to access uninitialized variables |
data hiding | opaque pointers and static | private |
interface method | non-static function | public method |
data type for generic item | void * | Object |
casting | anything goes | checked exception at run-time or compile-time |
demotions | automatic, but might lose precision | must explicitly cast, e.g., to convert from long to int |
polymorphism | union | inheritence |
overloading | no | yes for methods, no for operators |
graphics | use external libraries | Java library support, use our standard drawing library |
null | NULL | null |
enumeration | enum | typesafe enum |
preprocessor | yes | no |
variable declaration | at beginning of a block | before you use it |
variable naming conventions | sum_of_squares | sumOfSquares |
commenting | /* */ | /* */ or // |
file naming conventions | stack.c, stack.h | Stack.java - file name matches name of class |
callbacks | pointers to global functions | use interfaces for commmand dispatching |
variable number of arguments | varargs | String ... |
assertions | assert | assert |
exit and return value to OS | exit(1) | System.exit(1) |
C Programming vs. Java Programming的更多相关文章
- Java Programming Language Enhancements
引用:Java Programming Language Enhancements Java Programming Language Enhancements Enhancements in Jav ...
- Java Programming Test Question 3
import java.util.HashSet; public class JPTQuestion3 { public static void main(String[] args) { HashS ...
- Java Programming Test Question 2
public class JPTQuestion2 { public static void main(String[] args) { String s3 = "JournalDev&qu ...
- 文本信息“welcome to java programming!”
import javax.swing.JOptionPanepublic class welcome {public static void main(string[] arg){JOptionPan ...
- Java programming language compiler
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html\ javac - Java programming l ...
- Parallel Programming AND Asynchronous Programming
https://blogs.oracle.com/dave/ Java Memory Model...and the pragmatics of itAleksey Shipilevaleksey.s ...
- Java Programming Guidelines
This appendix contains suggestions to help guide you in performing low-level program design and in w ...
- iOS运行时编程(Runtime Programming)和Java的反射机制对比
运行时进行编程,类似Java的反射.运行时编程和Java反射的对比如下: 1.相同点 都可以实现的功能:获取类信息.属性设置获取.类的动态加载(NSClassFromString(@“clas ...
- Fast Intro To Java Programming (2)
Java局部变量 局部变量声明在方法.构造方法或者语句块中: 局部变量在方法.构造方法.或者语句块被执行的时候创建,当它们执行完成后,变量将会被销毁: 访问修饰符不能用于局部变量: 局部变量只在声明它 ...
随机推荐
- vi/vim编辑器必知必会
一.我们为什么要学习vim编辑器? Linux的命令行界面下面有非常多的文本编辑器.比如经常听说的就有Emacs.pico.nano.joe与vim等.vim可以看做是vi的高级版.我们为什么一定要学 ...
- Java-Maven(八):配置远程中央仓库的各种方法
一.远程仓库的配置 在平时的开发中,我们往往不会使用默认的中央仓库,默认的中央仓库访问的速度比较慢,访问的人或许很多,有时候也无法满足我们项目的需求,可能项目需要的某些构件中央仓库中是没有的,而在其他 ...
- ng-app与ng-app='myApp'的区别
ng-app与ng-app=""是一样的,都是没定义应用名的,例如 <div ng-app="" ng-init="firstName='ech ...
- 项目复审——Alpha阶段
Deadline: 2018-5-19 10:00PM,以提交至班级博客时间为准. 5.10实验课上,以(1.2班级,3.4班级为单位)进行项目复审.根据以下要求,完成本团队对其他团队的复审排序. 参 ...
- webpack4 自学笔记一(babel的配置)
所有代码都可以再我的github上查看,每个文件夹下都会有README.md,欢迎star: https://github.com/Jasonwang911/webpackStudyInit/tree ...
- 排查CentOS7.0的联网情况
1.ifconfig命令. 查看网络配置是否有问题 在/etc/sysconfig/network-scripts/ifcfg-ens33里面配置好网络,记住onboot=on这个选项一定要设置,不然 ...
- centos下MySQL Workbench连接时崩溃的解决方法
在centos6.5中使用MySQL Workbench 6.3.8链接数据库崩溃,如果是在终端使用命令“mysql-workbench”打开的话会有如下错误提示: /usr/libexec/mysq ...
- 百度智能小程序弹窗组件wcPop|智能小程序自定义model弹窗模板
百度智能小程序自定义弹窗组件wcPop|百度小程序model对话框|智能小程序弹窗界面模板 最近百度也推出了自己的智能小程序,如是就赶紧去试了下,官方提供的api还不是狠完整.而且官方提供的弹窗组件也 ...
- WebSerivce与WebAPI的区别
一.什么是Web Service Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应 ...
- 问题处理:PROCESS一个domain节点起不来
今早遇到一个问题,在看服务器列表时候,红框中的服务器是停机的,PSUNX4与该服务器是同一台机器,排除机器down掉的情况. 登陆服务器,在关闭psunx2节点时候,报错:can't attach t ...